zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    取得窗口大小和窗口位置兼容所有浏览器的js代码
    一个简单易用的导出Excel类
    如何快速启动chrome插件
    网页表单设计案例
    Ubuntu下的打包解包
    The source file is different from when the module was built. Would you like the debugger to use it anyway?
    FFisher分布
    kalman filter
    Group delay Matlab simulate
    24位位图格式解析
  • 原文地址:https://www.cnblogs.com/WWHHTT/p/9901263.html
Copyright © 2011-2022 走看看