bzoj1012: [JSOI2008]最大数maxnumber
题目链接
bzoj1012: [JSOI2008]最大数maxnumber
题解
维护一颗长m序列的线段树
瞎打就OK了
听说可以单调栈?
luogu数据真是有毒
代码
#include<cstdio>
#include<algorithm>
const int maxn = 200007;
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c <= '9' && c >= '0')x = x * 10 + c - '0',c = getchar();
return x * f;
}
int t[maxn << 2];
int D,M,len;
#define ls x << 1
#define rs x << 1 | 1
inline void update(int x) {
t[x] = std::max(t[ls],t[rs]);
}
void modify(int x,int l,int r,int to,int w) {
if(l == r) {
t[x] = w % M; return ;
}
int mid = l + r >> 1;
if(to <= mid) modify(ls,l,mid,to,w);
else modify(rs,mid + 1,r,to,w) ;
return update(x);
}
int query(int x,int l,int r,int tl,int tr) {
if(l >= tl && r <= tr)
return t[x];
int mid = l + r >> 1;
int ret = 0;
if(tl <= mid) ret = std::max(ret,query(ls,l,mid,tl,tr));
if(tr > mid ) ret = std::max(ret,query(rs,mid + 1,r,tl,tr));
return ret;
}
int main() {
D = read(),M = read();
char c[20];
len = 0;
for(int tmp = 0,k,i = 1;i <= D;++ i) {
scanf("%s",c + 1) ;
if(c[1] == 'A') {
len ++;k = read();
modify(1,1,D,len,k + tmp);
}
else {
k = read();
tmp = query(1,1,D,len - k + 1,len) % M;
printf("%d\n",tmp % M);
}
}
return 0;
}