JSOI 2008 最大数

线段树的模板,并不用动态开点

提前建树之后单点修改

下面给出代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define inf 2147483646
inline long long rd(){
    long long x=0,f=1;
    char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
inline void write(long long x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
    return ;
}
long long n,m;
struct node{
    long long maxn,l,r;
    long long f;
}tree[4000006];
inline void build(long long i,long long x,long long y){
    tree[i].l=x,tree[i].r=y;
    if(x==y){
        tree[i].maxn=-inf;
        return ;
    }
    long long mid=(x+y)>>1;
    build(i<<1,x,mid);
    build(i<<1|1,mid+1,y);
    tree[i].maxn=max(tree[i<<1].maxn,tree[i<<1|1].maxn);
    return ;
}
inline void add(long long i,long long x,long long y){
    if(tree[i].l==tree[i].r&&tree[i].r==x){
        tree[i].maxn=y;
        return ;
    }
    if(x<=tree[i<<1].r) add(i<<1,x,y);
    if(x>=tree[i<<1|1].l) add(i<<1|1,x,y);
    tree[i].maxn=max(tree[i<<1].maxn,tree[i<<1|1].maxn);
    return ;
}
inline long long solve(long long i,long long x,long long y){
    if(tree[i].l>=x&&tree[i].r<=y) return tree[i].maxn;
    long long ans=0;
    if(tree[i<<1].r>=x) ans=solve(i<<1,x,y);
    if(tree[i<<1|1].l<=y) ans=max(ans,solve(i<<1|1,x,y));
    return ans;
}
int main(){
    n=rd(),m=rd();
    build(1,1,n);
    long long set=0;
    long long last=0;
    for(long long i=1;i<=n;i++){
        char f;cin>>f;
        if(f=='A'){
            long long x=rd();
            set++;
            add(1,set,(x+last)%m);
        }
        else{
            long long x=rd();
            last=solve(1,set-x+1,set);
            write(last);
            puts("");
        }
    }
    return 0;
}

 

posted @ 2018-11-03 16:43  Bruce--Wang  阅读(161)  评论(0编辑  收藏  举报