zoukankan      html  css  js  c++  java
  • 维修队列(bzoj 1500)

    Description

    Input

    输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目。
    第2行包含N个数字,描述初始时的数列。
    以下M行,每行一条命令,格式参见问题描述中的表格。
    任何时刻数列中最多含有500 000个数,数列中任何一个数字均在[-1 000, 1 000]内。
    插入的数字总数不超过4 000 000个,输入文件大小不超过20MBytes。

    Output

    对于输入数据中的GET-SUM和MAX-SUM操作,向输出文件依次打印结果,每个答案(数字)占一行。

    Sample Input

    9 8
    2 -6 3 5 1 -5 -3 6 3
    GET-SUM 5 4
    MAX-SUM
    INSERT 8 3 -5 7 2
    DELETE 12 1
    MAKE-SAME 3 3 2
    REVERSE 3 6
    GET-SUM 5 4
    MAX-SUM

    Sample Output

    -1
    10
    1
    10

    HINT

    /*
      都是splay的一下基本操作,只是看得懂,但自己写不出来。 
    */
    #include<cstdio>
    #include<iostream>
    #include<queue>
    #define N 500010
    #define inf 1000000000
    using namespace std;
    int a[N],id[N],fa[N],c[N][2];
    int v[N],size[N],sum[N],mx[N],lx[N],rx[N],n,m,rt,sz;
    int tag[N],rev[N];
    queue<int> q;
    
    void pushup(int k){
        int l=c[k][0],r=c[k][1];
        mx[k]=max(max(mx[l],mx[r]),rx[l]+lx[r]+v[k]);
        lx[k]=max(lx[l],sum[l]+lx[r]+v[k]);
        rx[k]=max(rx[r],sum[r]+rx[l]+v[k]);
        sum[k]=sum[l]+sum[r]+v[k];
        size[k]=size[l]+size[r]+1;
    }
    
    void pushdown(int k){
        int l=c[k][0],r=c[k][1];
        if(tag[k]){
            tag[k]=rev[k]=0;
            if(l) tag[l]=1,v[l]=v[k],sum[l]=v[k]*size[l];
            if(r) tag[r]=1,v[r]=v[k],sum[r]=v[k]*size[r];
            if(v[k]>0){
                if(l) mx[l]=lx[l]=rx[l]=sum[l];
                if(r) mx[r]=lx[r]=rx[r]=sum[r];
            }
            else {
                if(l) mx[l]=v[l],lx[l]=rx[l]=0;
                if(r) mx[r]=v[r],lx[r]=rx[r]=0;
            }
        }
        if(rev[k]){
            rev[l]^=1;rev[r]^=1;rev[k]^=1;
            swap(lx[l],rx[l]);swap(lx[r],rx[r]);
            swap(c[l][0],c[l][1]);swap(c[r][0],c[r][1]);
        }
    }
    
    void rotate(int x,int &k){
        int y=fa[x],z=fa[y],l,r;
        if(c[y][0]==x) l=0;else l=1;r=l^1;
        if(y==k) k=x;
        else {
            if(c[z][0]==y) c[z][0]=x;
            else c[z][1]=x;
        }
        fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
        c[y][l]=c[x][r];c[x][r]=y;
        pushup(y);pushup(x);
    }
    
    void splay(int x,int &k){
        while(x!=k){
            int y=fa[x],z=fa[y];
            if(y!=k){
                if(c[z][0]==y^c[y][0]==x) rotate(x,k);
                else rotate(y,k);
            }
            rotate(x,k);
        }
    }
    
    int find(int k,int rk){
        pushdown(k);
        if(size[c[k][0]]+1==rk) return k;
        else if(size[c[k][0]]>=rk) return find(c[k][0],rk);
        else find(c[k][1],rk-size[c[k][0]]-1);
    }
    
    int split(int k,int tot){
        int x=find(rt,k),y=find(rt,k+tot+1);
        splay(x,rt);splay(y,c[x][1]);
        return c[y][0];
    }
    
    void build(int l,int r,int f){
        if(l>r) return;
        int mid=l+r>>1,last=id[f],now=id[mid];
        if(l==r){
            sum[now]=mx[now]=a[l];size[now]=1;
            tag[now]=rev[now]=0;
            lx[now]=rx[now]=max(0,a[l]);
        }
        else build(l,mid-1,mid),build(mid+1,r,mid);
        v[now]=a[mid];fa[now]=last;c[last][mid>=f]=now;
        pushup(now);
    }
    
    void insert(int k,int tot){
        for(int i=1;i<=tot;i++){
            if(!q.empty()) id[i]=q.front(),q.pop();
            else id[i]=++sz;
            scanf("%d",&a[i]);
        }
        build(1,tot,0);int z=id[(1+tot)>>1];
        int x=find(rt,k+1),y=find(rt,k+2);
        splay(x,rt);splay(y,c[x][1]);
        c[y][0]=z;fa[z]=y;
        pushup(y);pushup(x);
    }
    
    void rec(int k){
        if(!k) return;
        int l=c[k][0],r=c[k][1];
        rec(l);rec(r);q.push(k);
        fa[k]=c[k][0]=c[k][1]=tag[k]=rev[k]=0;
    }
    
    void del(int k,int tot){
        int x=split(k,tot),y=fa[x];
        rec(x);c[y][0]=0;
        pushup(y);pushup(fa[y]);
    }
    
    void modify(int k,int tot,int c){
        int x=split(k,tot),y=fa[x];
        v[x]=c;tag[x]=1;sum[x]=v[x]*size[x];
        mx[x]=max(v[x],sum[x]);
        lx[x]=rx[x]=max(0,sum[x]);
        pushup(y);pushup(fa[y]);
    }
    
    void rever(int k,int tot){
        int x=split(k,tot),y=fa[x];
        rev[x]^=1;
        swap(lx[x],rx[x]);
        swap(c[x][0],c[x][1]);
        pushup(y);pushup(fa[y]);
    }
    
    int querysum(int k,int tot){
        int x=split(k,tot);
        return sum[x];
    }
    
    int main(){
        scanf("%d%d",&n,&m);mx[0]=a[1]=a[n+2]=-inf;
        for(int i=1;i<=n;i++) scanf("%d",&a[i+1]);
        for(int i=1;i<=n+2;i++) id[i]=i;
        build(1,n+2,0);
        rt=(n+3)>>1;sz=n+2;
        char ch[15];int k,tot,c;
        for(int i=1;i<=m;i++){
            scanf("%s",ch);
            if(ch[0]!='M'||ch[2]!='X') scanf("%d%d",&k,&tot);
            if(ch[0]=='I') insert(k,tot);
            if(ch[0]=='D') del(k,tot);
            if(ch[0]=='R') rever(k,tot);
            if(ch[0]=='G') printf("%d
    ",querysum(k,tot));
            if(ch[0]=='M'){
                if(ch[2]=='X') printf("%d
    ",mx[rt]);
                else scanf("%d",&c),modify(k,tot,c);
            }
        }
        return 0;
    }
  • 相关阅读:
    2020牛客多校第二场A.All with Pairs hash+kmp
    2020杭电多校第三场
    2020牛客多校第六场K.K-Bag (思维?)
    2020牛客多校第六场 G.Grid Coloring 构造
    2020杭电多校第一场
    2020牛客暑期多校训练营(第三场)D.Points Construction Problem 构造
    ACM模板_axiomofchoice
    关于deque的行为分析
    Codeforces Round #665 (Div. 2) 题解 (CDEF)
    Codeforces Global Round 10 题解 (DEF)
  • 原文地址:https://www.cnblogs.com/harden/p/6446519.html
Copyright © 2011-2022 走看看