Splay——kuangbin自己改了一下,比较喜欢

来源:互联网 发布:更换身份证登记软件 编辑:程序博客网 时间:2024/05/11 04:14
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int MAXN=100010;int pre[MAXN],ch[MAXN][2],size[MAXN],root;//父结点、左右孩子、子树规模、根结点、结点数量int key[MAXN];//该点的值int add[MAXN];//增量的延迟标记long long sum[MAXN];//子树的和int a[MAXN];//初始的数组,建树时候用int n,q;void NewNode(int &r,int father,int num,int k)//一个是调用的时候注意变量顺序,还有r必须引用&{    r=num;    pre[r]=father;    size[r]=1;//这个不能忘记 ,一定是1,否则可能出错    key[r]=k;    add[r]=sum[r]=ch[r][0]=ch[r][1]=0;}//给r为根的子树增加值,一定把当前结点的全部更新掉,再加个延迟标记表示儿子结点没有更新void Update_Add(int r,int ADD){    if(r==0)return;    add[r]+=ADD;    key[r]+=ADD;    sum[r]+=(long long)ADD*size[r];}//通过孩子结点更新父亲结点void Push_Up(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 Push_Down(int r){    if(add[r])    {        Update_Add(ch[r][0],add[r]);        Update_Add(ch[r][1],add[r]);        add[r]=0;    }}//建树//先建立中间结点,再两端的方法void Build(int &x,int l,int r,int father){    if(l>r)return;    int mid=(l+r)>>1;    NewNode(x,father,mid+1,a[mid]);    Build(ch[x][0],l,mid-1,x);    Build(ch[x][1],mid+1,r,x);    Push_Up(x);}//初始化,前后各加一个king结点void Init(){    for(int i=1;i<=n;i++)scanf("%d",&a[i]);    root=ch[0][0]=ch[0][1]=pre[0]=size[0]=add[0]=sum[0]=key[0]=0;    NewNode(root,0,1,0);    NewNode(ch[root][1],root,n+2,0);//头尾各加入一个空位    Build(ch[ch[root][1]][0],1,n,ch[root][1]);    Push_Up(ch[root][1]);    Push_Up(root);}//旋转,0为左旋,1为右旋  该部分基本固定void Rotate(int x,int kind){    int y=pre[x];    Push_Down(y);    Push_Down(x);//先把y的标记向下传递,再把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;    Push_Up(y);//维护y结点}//Splay调整,将结点r调整到goal下面void Splay(int r,int goal){    Push_Down(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);            }        }    }    Push_Up(r);    if(goal==0)root=r;}void ADD(int l,int r,int D){    Splay(l,0);//第l个点到根结点    Splay(r+2,root);//第r+2个点到根结点的右孩子    Update_Add(ch[ch[root][1]][0],D);    Push_Up(ch[root][1]);    Push_Up(root);}int main(){    while(scanf("%d%d",&n,&q)!=EOF)    {        Init();//这个不能忘记        while(q--)        {            char op[20];            int x,y,z;            scanf("%s",op);            if(op[0]=='Q')            {                scanf("%d%d",&x,&y);                Splay(x,0);                Splay(y+2,root);                printf("%I64d\n",sum[ch[ch[root][1]][0]]);            }            else            {                scanf("%d%d%d",&x,&y,&z);                ADD(x,y,z);            }        }    }    return 0;}

0 0
原创粉丝点击