POJ 3468 splay

来源:互联网 发布:java机房管理系统 编辑:程序博客网 时间:2024/05/17 13:45

点击打开链接

题意:给一个序列,Q是询问a到b的所有数的和,C是将a到b的所有的数的值增加c

思路:如果用线段树来写的话,十分简单,但是为了练习splay,照着kuangbin大神的做法谢了一遍,没什么特别的,就是将更新和询问的操作全部旋转到根的右儿子的左儿子

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const ll INF=0x3f3f3f3f3f3f3f3fll;const int maxn=100010;int pre[maxn],ch[maxn][2],size[maxn],root,tot1;//父节点,左右孩子,子树规模,根节点,节点数量int key[maxn],flag[maxn];//该点的值,懒惰标记int a[maxn],n,q;int s[maxn],tot2;ll sum[maxn];void treaval(int x){//debug部分    if(x){        treaval(ch[x][0]);        printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d,key=%2d add=%2d sum=%I64d\n",x,ch[x][0],ch[x][1],pre[x],size[x],key[x],flag[x],sum[x]);        treaval(ch[x][1]);    }}void debug(){    printf("root:%d\n",root);treaval(root);}void newnode(int &r,int fa,int k){    if(tot2) r=s[tot2--];    else r=++tot1;    pre[r]=fa;size[r]=1;key[r]=k;flag[r]=0;sum[r]=0;    ch[r][0]=ch[r][1]=0;}void update_add(int r,int val){    if(r==0) return ;    flag[r]+=val;    key[r]+=val;    sum[r]+=(ll)val*size[r];}void pushup(int r){    size[r]=size[ch[r][0]]+size[ch[r][1]]+1;    sum[r]=sum[ch[r][0]]+sum[ch[r][1]]+key[r];}void pushdown(int r){    if(flag[r]){        update_add(ch[r][0],flag[r]);        update_add(ch[r][1],flag[r]);        flag[r]=0;    }}void buildtree(int &x,int l,int r,int fa){    if(l>r) return ;    int mid=(l+r)>>1;    newnode(x,fa,a[mid]);    buildtree(ch[x][0],l,mid-1,x);    buildtree(ch[x][1],mid+1,r,x);    pushup(x);}void init(){    root=tot1=tot2=0;    ch[root][0]=ch[root][1]=pre[root]=size[root]=flag[root]=sum[root]=0;    key[root]=0;    newnode(root,0,-1);    newnode(ch[root][1],root,-1);    buildtree(ch[ch[root][1]][0],1,n,ch[root][1]);    pushup(ch[root][1]);pushup(root);}void Rotate(int x,int kind){    int y=pre[x];    pushdown(y);pushdown(x);    ch[y][!kind]=ch[x][kind];    pre[ch[x][kind]]=y;    if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;    pre[x]=pre[y];    ch[x][kind]=y;pre[y]=x;    pushup(y);}void splay(int r,int goal){    pushdown(r);    while(pre[r]!=goal){        if(pre[pre[r]]==goal) Rotate(r,ch[pre[r]][0]==r);        else{            int y=pre[r];            int kind=ch[pre[y]][0]==y;            if(ch[y][kind]==r) Rotate(r,!kind),Rotate(r,kind);            else Rotate(y,kind),Rotate(r,kind);        }    }    pushup(r);    if(goal==0) root=r;}int get_kth(int r,int k){    pushdown(r);    int t=size[ch[r][0]]+1;    if(t==k) return r;    if(t>k) return get_kth(ch[r][0],k);    else return get_kth(ch[r][1],k-t);}int get_min(int r){    pushdown(r);    while(ch[r][0]){        r=ch[r][0];        pushdown(r);    }    return r;}int get_max(int r){    pushdown(r);    while(ch[r][1]){        r=ch[r][1];        pushdown(r);    }    return r;}void add(int l,int r,int val){    splay(get_kth(root,l),0);    splay(get_kth(root,r+2),root);    update_add(ch[ch[root][1]][0],val);    pushup(ch[root][1]);    pushup(root);}ll query(int l,int r){    splay(get_kth(root,l),0);    splay(get_kth(root,r+2),root);    return sum[ch[ch[root][1]][0]];}int main(){    while(scanf("%d%d",&n,&q)!=-1){        for(int i=1;i<=n;i++) scanf("%d",&a[i]);        init();        while(q--){            char op[10];            int x,y,z;            scanf("%s",op);            if(op[0]=='Q'){                scanf("%d%d",&x,&y);                printf("%I64d\n",query(x,y));            }else{                scanf("%d%d%d",&x,&y,&z);                add(x,y,z);            }        }    }    return 0;}

0 0
原创粉丝点击