LOJ #10127. 「一本通 4.3 练习 1」最大数 题解

题目链接:LOJ

咕咕咕

#include <iostream>
#include <cstdio>
#include <cstring>
#define ls p<<1
#define int long long
#define rs p<<1|1
using namespace std;
int n,m,a[4000005],maxdat[4000005],lazy[4000005];
int last,p;
int tot;
int q;
inline int read(){
  int x=0;int f=1;char ch=getchar();
  while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
  while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
  return x*f;
}
inline void down(int p,int l,int r){
  int mid=l+r>>1;
  lazy[ls]+=lazy[p];
  lazy[rs]+=lazy[p];
  maxdat[ls]+=lazy[p];
  maxdat[rs]+=lazy[p];
  lazy[p]=0;
}
inline void update(int p,int x,int y,int l,int r,int k){
  if(x<=l&&y>=r){
    maxdat[p]+=k;
    lazy[p]+=k;
    return;
  }
  down(p,l,r);
  int mid=l+r>>1;
  if(x<=mid) update(ls,x,y,l,mid,k);
  if(y>mid) update(rs,x,y,mid+1,r,k);
  maxdat[p]=max(maxdat[ls],maxdat[rs]);
}
inline int query(int p,int x,int y,int l,int r){
  if(x<=l&&y>=r) return maxdat[p];
  int mid=l+r>>1;
  int res=-(1<<30);
  if(x<=mid) res=max(res,query(ls,x,y,l,mid));
  if(y>mid) res=max(res,query(rs,x,y,mid+1,r));
  return res;
}
signed main(){
  m=read();p=read();
  tot=0;
  for(int i=1;i<=m;i++){
    char c;
    int q,t;
    cin>>c;
    if(c=='A'){
      t=read();
      ++tot;
      int kkk=(t+last)%p;
      update(1,tot,tot,1,m,kkk);
    }
    else{
      q=read();
      last=query(1,tot-q+1,tot,1,m);
      cout<<last<<endl;
    }
  }
  return 0;
}

posted @   Miraii  阅读(164)  评论(3编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
主题色彩