Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7658 Accepted Submission(s): 2024
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define MAXN 50010 4 struct node 5 { 6 int left,right,val; 7 }tree[MAXN]; 8 int father[MAXN],rev[MAXN],tag[MAXN],Stack[MAXN]; 9 int read() 10 { 11 int s=0,fh=1;char ch=getchar(); 12 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)fh=-1;ch=getchar();} 13 while(ch>=‘0‘&&ch<=‘9‘){s=s*10+(ch-‘0‘);ch=getchar();} 14 return s*fh; 15 } 16 int isroot(int x) 17 { 18 return tree[father[x]].left!=x&&tree[father[x]].right!=x; 19 } 20 void pushdown(int x) 21 { 22 int l=tree[x].left,r=tree[x].right; 23 if(rev[x]!=0) 24 { 25 rev[x]^=1;rev[l]^=1;rev[r]^=1; 26 swap(tree[x].left,tree[x].right); 27 } 28 if(tag[x]!=0) 29 { 30 tag[l]+=tag[x];tag[r]+=tag[x]; 31 tree[l].val+=tag[x];tree[r].val+=tag[x]; 32 tag[x]=0; 33 } 34 } 35 void rotate(int x) 36 { 37 int y=father[x],z=father[y]; 38 if(!isroot(y)) 39 { 40 if(tree[z].left==y)tree[z].left=x; 41 else tree[z].right=x; 42 } 43 if(tree[y].left==x) 44 { 45 father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y; 46 } 47 else 48 { 49 father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y; 50 } 51 } 52 void splay(int x) 53 { 54 int top=0,i,y,z;Stack[++top]=x; 55 for(i=x;!isroot(i);i=father[i])Stack[++top]=father[i]; 56 for(i=top;i>=1;i--)pushdown(Stack[i]); 57 while(!isroot(x)) 58 { 59 y=father[x];z=father[y]; 60 if(!isroot(y)) 61 { 62 if((tree[y].left==x)^(tree[z].left==y))rotate(x); 63 else rotate(y); 64 } 65 rotate(x); 66 } 67 } 68 void access(int x) 69 { 70 int last=0; 71 while(x!=0) 72 { 73 splay(x); 74 tree[x].right=last; 75 last=x;x=father[x]; 76 } 77 } 78 void makeroot(int x) 79 { 80 access(x);splay(x);rev[x]^=1; 81 } 82 void link(int u,int v) 83 { 84 makeroot(u);father[u]=v;splay(u); 85 } 86 void cut(int u,int v) 87 { 88 makeroot(u);access(v);splay(v);father[u]=tree[v].left=0; 89 } 90 int findroot(int x) 91 { 92 access(x);splay(x); 93 while(tree[x].left!=0)x=tree[x].left; 94 return x; 95 } 96 int main() 97 { 98 int n,m,p,bb,ee,k,camp,i; 99 char fh[2]; 100 while(scanf("%d %d %d",&n,&m,&p)!=EOF) 101 { 102 for(i=0;i<=n;i++)tree[i].val=tree[i].left=tree[i].right=0,tag[i]=0,father[i]=0,rev[i]=0; 103 for(i=1;i<=n;i++)tree[i].val=read(); 104 for(i=1;i<=m;i++) 105 { 106 bb=read();ee=read();link(bb,ee); 107 } 108 for(i=1;i<=p;i++) 109 { 110 scanf("%s",fh); 111 if(fh[0]==‘I‘) 112 { 113 bb=read();ee=read();k=read(); 114 makeroot(bb);access(ee);splay(ee); 115 tag[ee]+=k;tree[ee].val+=k; 116 } 117 else if(fh[0]==‘D‘) 118 { 119 bb=read();ee=read();k=read(); 120 makeroot(bb);access(ee);splay(ee); 121 tag[ee]-=k;tree[ee].val-=k; 122 } 123 else 124 { 125 camp=read(); 126 access(camp); 127 printf("%d\n",tree[camp].val); 128 } 129 } 130 } 131 fclose(stdin); 132 fclose(stdout); 133 return 0; 134 }
Hdu 3966-Aragorn's Story LCT,动态树
原文:http://www.cnblogs.com/Var123/p/5294925.html