POJ 3468 A Simple Problemwith Integers(SplayTree)
http://poj.org/problem?id=3468
题意:给你一个数列,要求你完成区间更新,区间求和的操作.
分析:本题之前用线段树做过一次.现在用SplayTree结构再做一次.
SplayTree的每个节点维护add,sum,key信息,分别表示以该节点为根节点的整棵树的增加值,和,以及本节点的值.注意add标记在x号节点上时,x节点的sum和key就立即更新,这个add只是表示x节点的儿子节点们没有更新.
具体代码如下,注释可以参考:
http://blog.csdn.net/u013480600/article/details/24468527
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=100000+100;
int a[maxn];
int n,q;
struct SplayTree
{
#define Key_Value ch[ch[root][1]][0]
int ch[maxn][2],pre[maxn],size[maxn],root,tot1;
int key[maxn];
int add[maxn];
long long sum[maxn];
void NewNode(int &r,int fa,int k)
{
r =++tot1;
pre[r]=fa;
add[r]=sum[r]=ch[r][0]=ch[r][1]=0;
size[r]=1;
key[r]=k;
}
void Update_Add(int r,int ADD)
{
if(r==0)return ;
key[r]+=ADD;
add[r]+=ADD;
sum[r]+=(long long)ADD*size[r];
}
void Push_Up(int r)
{
size[r]=1+size[ch[r][0]]+size[ch[r][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 fa)
{
if(l>r) return ;
int mid=(l+r)>>1;
NewNode(x,fa,a[mid]);
Build(ch[x][0],l,mid-1,x);
Build(ch[x][1],mid+1,r,x);
Push_Up(x);
}
void init()
{
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
root=tot1=0;
ch[root][0]=ch[root][1]=pre[root]=size[root]=key[root]=sum[root]=add[root]=0;
NewNode(root,0,-1);
NewNode(ch[root][1],root,-1);
Build(Key_Value,1,n,ch[root][1]);
Push_Up(ch[root][1]);
Push_Up(root);
}
void Rotate(int x,int kind)
{
int y=pre[x];
Push_Down(y);
Push_Down(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);
}
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;
}
int Get_Kth(int r,int k)
{
Push_Down(r);
int t=size[ch[r][0]]+1;
if(k==t) return r;
else if(k<t) return Get_Kth(ch[r][0],k);
else return Get_Kth(ch[r][1],k-t);
}
void ADD(int l,int r,int D)
{
Splay(Get_Kth(root,l),0);
Splay(Get_Kth(root,r+2),root);
Update_Add(Key_Value,D);
Push_Up(ch[root][1]);
Push_Up(root);
}
long long Query(int l,int r)
{
Splay(Get_Kth(root,l),0);
Splay(Get_Kth(root,r+2),root);
return sum[Key_Value];
}
}st;
int main()
{
while(scanf("%d%d",&n,&q)==2)
{
st.init();
while(q--)
{
char op[20];
int x,y,z;
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d%d%d",&x,&y,&z);
st.ADD(x,y,z);
}
else if(op[0]=='Q')
{
scanf("%d%d",&x,&y);
printf("%I64d\n",st.Query(x,y));
}
}
}
return 0;
}
自己写的代码二:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define KeyValue ch[ch[root][1]][0]
const int maxn=100000+10;
int a[maxn];
int n,q;
struct SplayTree
{
int ch[maxn][2],pre[maxn],size[maxn],root,tot1;
int key[maxn],add[maxn];
long long sum[maxn];
void UpdateAdd(int r,int val)//更新r节点的add
{
if(r==0) return;
key[r]+=val;
add[r]+=val;
sum[r]+=(long long)val*(size[r]);
}
void PushDown(int r)
{
if(add[r])
{
UpdateAdd(ch[r][0],add[r]);
UpdateAdd(ch[r][1],add[r]);
add[r]=0;
}
}
void PushUp(int r)
{
size[r] = 1+size[ch[r][0]]+size[ch[r][1]];
sum[r] = key[r]+sum[ch[r][0]]+sum[ch[r][1]];
}
void Rotate(int x,int kind)//0
{
int y=pre[x];
PushDown(y);//这里PushDown的顺序错了就是wrong
PushDown(x);
ch[y][kind^1]=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)//zhi
{
Rotate(r,kind^1);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
PushUp(r);
if(goal==0) root=r;
}
void NewNode(int &r,int fa,int k)
{
r=++tot1;
key[r]=k;
pre[r]=fa;
ch[r][0]=ch[r][1]=sum[r]=add[r]=0;
size[r]=1;
}
void Build(int &x,int l,int r,int fa)
{
if(l>r) return;
int mid=(l+r)>>1;
NewNode(x,fa,a[mid]);
Build(ch[x][0],l,mid-1,x);
Build(ch[x][1],mid+1,r,x);
PushUp(x);
}
void init()
{
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
root=tot1=0;
pre[root]=size[root]=sum[root]=ch[root][0]=ch[root][1]=key[root]=add[root]=0;
NewNode(root,0,0);
NewNode(ch[root][1],root,0);
Build(KeyValue,1,n,ch[root][1]);
PushUp(ch[root][1]);
PushUp(root);
}
int GetKth(int r,int k)
{
//PushDown(r);
int v=size[ch[r][0]]+1;
if(v==k) return r;
else if(k<v) return GetKth(ch[r][0],k);
else return GetKth(ch[r][1],k-v);
}
void Add(int l,int r,int val)
{
Splay(GetKth(root,l),0);
Splay(GetKth(root,r+2),root);
UpdateAdd(KeyValue,val);
PushUp(ch[root][1]);
PushUp(root);
}
long long Query(int l,int r)
{
Splay(GetKth(root,l),0);
Splay(GetKth(root,r+2),root);
return sum[KeyValue];
}
}st;
int main()
{
while(scanf("%d%d",&n,&q)==2)
{
st.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",st.Query(x,y));
}
else if(op[0]=='C')
{
scanf("%d%d%d",&x,&y,&z);
st.Add(x,y,z);
}
}
}
return 0;
}