笨蛋花的小窝qwq

笨蛋花的小窝qwq

“我要再和生活死磕几年。要么我就毁灭,要么我就注定铸就辉煌。如果有一天,你发现我在平庸面前低了头,请向我开炮。”

[JSOI2008]最大数

posted on 2018-04-27 11:56:02 | under 平时做题+数据结构 |

这个题是真毒瘤啊$qnq$

那么实际上,对于这个题来说,我们对于每个插入的数,将它插入线段树的尾部。插入之后再递归修改区间最大值。对于每个询问,我们就像普通的询问一样询问即可。

是时候温习温习线段树的板子了。(背不过了)

qwq

那么实际上,我们只需要一个单点修改操作和一个询问操作。

#include<iostream>
#include<cstdio>
using namespace std;
#define LL long long
LL t[800001],res=0,length=0;
void insert(LL l,LL r,LL num,LL v,LL root){
    if(l==r)t[root]=v;
    else {
        LL mid=(l+r)>>1;
        if(mid>=num)insert(l,mid,num,v,root<<1);
        if(mid<num)insert(mid+1,r,num,v,root<<1|1);
        t[root]=max(t[root<<1],t[root<<1|1]);
    }
}
LL query(LL L,LL R,LL l,LL r,LL root){
    if(L>=l&&R<=r){
        return t[root];
    }
    LL lres=0,rres=0,mid=(L+R)>>1;
    if(mid>=l)lres=query(L,mid,l,r,root<<1);
    if(mid<r)rres=query(mid+1,R,l,r,root<<1|1);
    return max(lres,rres);
}
int main(){
    LL m,d,a;
    char c;
    cin>>m>>d;
    for(int i=1;i<=m;i++){
        cin>>c>>a;
        if(c=='Q'){
            if(a==0)cout<<(res=0)<<endl;
            else cout<<(res=query(1,m,length-a+1,length,1))<<endl;
        }
        else {
            length++;
            insert(1,m,length,(a+res)%d,1);
        }
    }
}