[CodeVS4655]序列终结者 做题笔记

来源:互联网 发布:淘宝双十一充500红包 编辑:程序博客网 时间:2024/05/16 17:50

·· / ·– ·· ·-·· ·-·· / ·–· · ·-· ··· ·· ··· - / ··- -· - ·· ·-·· / ·· / ·– ·· -·
题目来源:http://codevs.cn/problem/4655/
不要问我为什么不写BZOJ,权限题。。。
维护max时注意负数的情况,主要的梗还是看BZOJ1500吧

#include <cstdio>#include <algorithm>#include <cstring>#define lch ch[x][0]#define rch ch[x][1]const int N=60000,inf=0x3fffffff;using namespace std;int ch[N][2],f[N];int a[N],s[N],mx[N],del[N],cnt=2,root=0;int tmp[N];bool rev[N];int n,m;void filp (int x) {    if (!x) return ;    swap(ch[x][0],ch[x][1]);    rev[x]^=1;}void deldown (int x,int v) {    if (!x) return ;    del[x]+=v; a[x]+=v; mx[x]+=v;}void pushup (int x) {    a[0]=a[1]=a[2]=mx[0]=-inf;s[0]=0;    s[x]=s[lch]+s[rch]+1;    mx[x]=max(a[x],mx[lch]);    mx[x]=max(mx[x],mx[rch]);}void pushdown (int x) {    //a,mx,del,rev    if (del[x]) {        deldown(lch,del[x]); deldown(rch,del[x]);        del[x]=0;    }    if (rev[x]) {        filp(lch); filp(rch);        rev[x]=0;    }}void rotate (int x) {    int y=f[x],opt;    if (ch[y][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);break; }        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) {    if (l>r) return 0;    int x=++cnt,mid=(l+r)>>1;    if (l==r) s[x]=1;    a[x]=del[x]=0;mx[x]=0;     rev[x]=0; f[x]=fa; tmp[x]=mid;    ch[x][0]=build(l,mid-1,x);    ch[x][1]=build(mid+1,r,x);    pushup(x);//    return x;}void init () {    s[1]=2;s[2]=1;    a[0]=mx[0]=a[1]=a[2]=mx[1]=mx[2]=-inf;    del[1]=del[2]=0;rev[1]=rev[2]=0;    ch[1][1]=2;root=1;    f[1]=0;f[2]=1;cnt=2;    ch[2][0]=build(1,n,2);    pushup(2);pushup(1);//树的结构改变一定要pushup,这里一开始忘了}void change (int l,int r,int v) {    int x=findkth(l),y=findkth(r+2);    splay(x);splay(y,x);    del[ch[y][0]]+=v;    a[ch[y][0]]+=v;mx[ch[y][0]]+=v;//这里一开始写成a[x]+=v了,注意有时会把孩子结点写成自己的问题}void reverse (int l,int r) {    int x=findkth(l),y=findkth(r+2);    splay(x);splay(y,x);    filp(ch[y][0]);}int getmax (int l,int r) {    int x=findkth(l),y=findkth(r+2);    splay(x);splay(y,x);    return mx[ch[y][0]];}int main () {    int k,l,r,v;    scanf("%d%d",&n,&m);    init();    for (int i=1;i<=m;i++) {        scanf("%d%d%d",&k,&l,&r);        if (k==1) {            scanf("%d",&v);            change(l,r,v);        }        else if (k==2) reverse(l,r);        else if (k==3) printf("%d\n",getmax(l,r));    }    return 0;}
0 0