[BZOJ1500]維修數列 做題筆記

題目來源:http://www.lydsy.com/JudgeOnline/problem.php?id=1500
自己做這題的一個非常有意思的收穫,就是pushup和pushdown千萬不能忽略“0”。一般我們會把不存在的孩子的id定爲0,這裏稱之爲void好了,按理來說這個void應該是個垃圾箱,扔進去的數據都會被吃掉。但是在pushdown時如果把不和諧的值扔了進去,pushup時是會從裏面提出來的!!之前做的題(尤其是線段樹)會因爲邊界條件而略過剛纔的鯁,但是這種splay一定要注意!
另外一個鯁,就是出現比較最大值的時候,一定一定要考慮負數的情況。
自己果然還是太弱了,無奈吃包蒟蒻果凍壓壓驚。。。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=501000,inf=0x3fffffff;
#define lch ch[x][0]
#define rch ch[x][1]
int n,m;
int ins[N];
int recycle[N],top=0;
int a[N],s[N],sum[N],mx[N],mxl[N],mxr[N];
int ch[N][2],f[N],cnt=0,root=0;
bool rev[N],ncov[N];int cov[N];

int tomax (int a,int b,int c,int d=-inf) {
    a=max(a,b);a=max(a,c);a=max(a,d);
    return a;
}

void filp (int x) {
    if (!x) return ;//防止把0修改了
    swap(ch[x][0],ch[x][1]);
    swap(mxl[x],mxr[x]);//這裏一定要加上
    rev[x]^=1;
}

//pushup前要pushdown
void pushup (int x) {
    //s[x],sum[x],mx[x],mxl[x],mxr[x]
    s[0]=sum[0]=0;mx[0]=mxl[0]=mxr[0]=-inf;//最好加上
    sum[x]=sum[lch]+sum[rch]+a[x];
    s[x]=s[lch]+s[rch]+1;
    int l=max(mxl[rch],0),r=max(mxr[lch],0);
    mxl[x]=max(mxl[lch],sum[lch]+a[x]+l);
    mxr[x]=max(mxr[rch],sum[rch]+a[x]+r);
    mx[x]=tomax(mx[lch],mx[rch],l+a[x]+r);
}

void pushdown (int x) {
    //rev[x],cov[x],s[x],sum[x],mx[x],mxl[x],mxr[x]
    if (ncov[x]) {
        if (lch) {//防止把0給修改了
            a[lch]=cov[x];
            sum[lch]=s[lch]*cov[x];
            mx[lch]=mxl[lch]=mxr[lch]=max(s[lch]*cov[x],cov[x]);//最小值不是0
            ncov[lch]=1;
            cov[lch]=cov[x];
        }
        if (rch) {
            a[rch]=cov[x];
            sum[rch]=s[rch]*cov[x];
            mx[rch]=mxl[rch]=mxr[rch]=max(s[rch]*cov[x],cov[x]);//
            ncov[rch]=1;
            cov[rch]=cov[x];
        }
        ncov[x]=0;cov[x]=0;
    }
    if (rev[x]) {
        filp(lch);filp(rch);
        rev[x]=0;
    }
}

void rotate (int x) {
    int y=f[x],opt;
    if (ch[f[x]][0]==x) opt=0;
    else opt=1;
    ch[y][opt]=ch[x][!opt];
    if (ch[x][!opt]) f[ch[x][!opt]]=y;
    f[x]=f[y];
    if (root==y) root=x;
    else if (ch[f[y]][0]==y) ch[f[y]][0]=x;
        else ch[f[y]][1]=x;
    f[y]=x,ch[x][!opt]=y;
    pushup(y),pushup(x);
}

void splay (int x,int to=0) {
    while (f[x]!=to) {
        if (f[f[x]]==to) rotate(x);
        else if ((ch[f[f[x]]][0]==f[x])
            ==(ch[f[x]][0]==x))
            rotate(f[x]),rotate(x);
        else rotate(x),rotate(x);
    }
}

int findkth (int k) {
    int x=root;
    while (x) {
        pushdown(x);
        if (k==s[ch[x][0]]+1) return x;
        if (k<s[ch[x][0]]+1) x=ch[x][0];
        else k-=s[ch[x][0]]+1,x=ch[x][1];
    }
    return 0;
}

int build (int l,int r,int fa=0) {
    if (l>r) return 0;
    int x,mid=(l+r)>>1;
    if (top) x=recycle[top--];else x=++cnt;
    if (l==r) {
        sum[x]=a[x]=ins[mid];
        mx[x]=mxl[x]=mxr[x]=ins[mid];//最小值不是0
        ch[x][0]=ch[x][1]=0;f[x]=fa;
        s[x]=1;ncov[x]=0;rev[x]=0;cov[x]=0;
        return x;
    }
    a[x]=ins[mid];
    ncov[x]=rev[x]=0;cov[x]=0;f[x]=fa;
    ch[x][0]=build(l,mid-1,x);
    ch[x][1]=build(mid+1,r,x);
    pushup(x);
    return x;
}

void init () {
    ch[1][1]=2;f[2]=1;//不能統計sum
    s[1]=2,s[2]=1;//
    a[1]=a[2]=-inf;
    mx[0]=mx[1]=mx[2]=mxl[1]=mxr[1]=mxl[2]=mxr[2]=-inf;//mx[0]一定要是-inf,而mxl[0]可以不是
    root=1;cnt=2;
    ch[2][0]=build(1,n,2);
    pushup(2);splay(2);
}

void insert (int pos,int tot) {
    for (int i=1;i<=tot;i++) scanf("%d",&ins[i]);
    int x=findkth(pos+1),y=findkth(pos+2);
    splay(x);splay(y,x);
    ch[y][0]=build(1,tot,y);
    pushup(ch[y][0]);splay(ch[y][0]);
}

void erase (int x) {
    if (!x||x==1||x==2) return;
    recycle[++top]=x;
    erase(ch[x][0]);erase(ch[x][1]);
    f[x]=ch[x][0]=ch[x][1]=0;
}

void del (int pos,int tot) {
    int x=findkth(pos),y=findkth(pos+tot+1);
    splay(x);splay(y,x);
    erase(ch[y][0]);
    ch[y][0]=0;
    pushup(y);splay(y);
}

void cover (int pos,int tot,int c) {
    int x=findkth(pos),y=findkth(pos+tot+1);
    splay(x);splay(y,x);
    a[ch[y][0]]=cov[ch[y][0]]=c;
    ncov[ch[y][0]]=1;
    pushdown(ch[y][0]);pushup(y);//
    splay(ch[y][0]);
}

void reverse (int pos,int tot) {
    int x=findkth(pos),y=findkth(pos+tot+1);
    splay(x);splay(y,x);
    int p=ch[y][0];
    filp(p);
    //pushup(p);splay(p);這裏不能更新
}

int getsum (int pos,int tot) {
    int x=findkth(pos),y=findkth(pos+tot+1);
    splay(x);splay(y,x);
    int p=ch[y][0];
    return sum[p];
}

int main () {
    int pos,tot,c,p;
    char str[30];
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%d",&ins[i]);
    init();
    for (int i=1;i<=m;i++) {
        scanf("%s",str);
        if (strcmp(str,"INSERT")==0) {
            scanf("%d%d",&pos,&tot);
            insert(pos,tot);
        }
        else if (strcmp(str,"DELETE")==0) {
            scanf("%d%d",&pos,&tot);
            del(pos,tot);
        }
        else if (strcmp(str,"MAKE-SAME")==0) {
            scanf("%d%d%d",&pos,&tot,&c);
            cover(pos,tot,c);
        }
        else if (strcmp(str,"REVERSE")==0) {
            scanf("%d%d",&pos,&tot);
            reverse(pos,tot);
        }
        else if (strcmp(str,"GET-SUM")==0) {
            scanf("%d%d",&pos,&tot);
            printf("%d\n",getsum(pos,tot));
        }
        else if (strcmp(str,"MAX-SUM")==0) {
            splay(1);splay(2,1);
            p=ch[ch[root][1]][0];
            printf("%d\n",mx[p]);
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章