题面
半个模版题吧,难点在这题卡读入的(雾)
单点修改最大值
加区间查询最大值
用线段树或树状数组维护就好了。二分好像可做,不过被卡掉了
挂上看到大佬的char读优
inline char readChar() {
char c;
do c = getchar(); while (isspace(c));
return c;
}
代码
#include<bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int maxn=2000000+10;
long long maxv[maxn>>2];
int len;
long long d,t;
int m;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
void pushup(int rt)
{
maxv[rt]=max(maxv[lson],maxv[rson]);
}
void update(int pos,int c,int l,int r,int rt)
{
if(l==r)
{
maxv[rt]+=c;
return;
}
int m=(l+r)>>1;
if(pos<=m)
update(pos,c,l,m,lson);
if(m<pos)
update(pos,c,m+1,r,rson);
pushup(rt);
}
void query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
t=max(t,maxv[rt]);
return;
}
int m=(l+r)>>1;
if(L<=m)
query(L,R,l,m,lson);
if(m<R)
query(L,R,m+1,r,rson);
}
int main()
{
scanf("%d%lld",&m,&d);
for(int i=1;i<=m;i++)
{
char op;int x;
cin>>op>>x;
if(op=='A')
{
update(++len,(x+t)%d,1,m,1);
}
if(op=='Q')
{
t=0;
query(len-x+1,len,1,m,1);
printf("%lld\n",t);
}
}
return 0;
}