双倍经验双倍的幸福。。。

所以另一道是300大洋的世界T_T。。。虽然题目是一样的,不过2843数据范围小了一点。。。

都是lct基本操作

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<math.h>
 4 using namespace std;
 5 const int maxn=30233;
 6 struct zs{
 7     int c[2],fa,sum,val;
 8     bool rev;
 9 }tree[maxn];
10 int i,j,x,y,n,m;
11 int stack[maxn];
12 char id[233];
13 inline bool isroot(int x){return tree[tree[x].fa].c[0]!=x&&tree[tree[x].fa].c[1]!=x;
14 }
15 inline void update(int x){tree[x].sum=tree[tree[x].c[0]].sum+tree[x].val+tree[tree[x].c[1]].sum;
16 }
17 inline void pushdown(int x){
18     if(!tree[x].rev)return;
19     int l=tree[x].c[0],r=tree[x].c[1];
20     if(l)tree[l].rev^=1;if(r)tree[r].rev^=1;tree[x].rev^=1;
21     swap(tree[x].c[0],tree[x].c[1]);
22 }
23 inline void rotate(int x){
24     int fa=tree[x].fa,gfa=tree[fa].fa;
25     if(!isroot(fa))tree[gfa].c[tree[gfa].c[1]==fa]=x;
26     int l=tree[fa].c[1]==x,r=l^1;
27     tree[fa].c[l]=tree[x].c[r];tree[x].c[r]=fa;
28     tree[fa].fa=x;tree[x].fa=gfa;tree[tree[fa].c[l]].fa=fa;
29     update(fa);update(x);
30 }
31 void splay(int x){
32     int top=0,tmp=x;stack[++top]=x;
33     while(!isroot(tmp))stack[++top]=tree[tmp].fa,tmp=tree[tmp].fa;
34     while(top)pushdown(stack[top]),top--;
35     int fa,gfa;
36     while(!isroot(x)){
37         fa=tree[x].fa,gfa=tree[fa].fa;
38         if(!isroot(fa))
39             if((tree[gfa].c[0]==fa)^(tree[fa].c[0]==x))rotate(x);
40             else rotate(fa);
41         rotate(x);
42     }
43 }
44 inline void access(int x){
45     int son=0;
46     while(x){
47         splay(x);tree[x].c[1]=son;
48         update(x);son=x;x=tree[x].fa;
49     }
50 }
51 inline void makeroot(int x){
52     access(x);splay(x);tree[x].rev^=1;
53 }
54 inline void cut(int x,int y){
55     makeroot(x);access(y);splay(y);tree[y].c[0]=tree[x].fa=0;
56 }
57 inline void link(int x,int y){
58     makeroot(x);tree[x].fa=y;
59 }
60 inline int getfa(int x){
61     access(x);splay(x);
62     while(tree[x].c[0]){x=tree[x].c[0];pushdown(x);}
63     splay(x);return x;
64 }
65 inline int query(int x,int y){
66     makeroot(x);access(y);splay(y);
67     return tree[y].sum;
68 }
69 inline void change(int x,int val){
70     makeroot(x);tree[x].val=val;update(x);
71 }
72 int main(){
73     scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&tree[i].val),tree[i].sum=tree[i].val;
74     scanf("%d",&m);
75     while(m--){
76         scanf("%s%d%d",id,&x,&y);
77         if(id[0]=='e'){
78             if(getfa(x)!=getfa(y))printf("impossible\n");else printf("%d\n",query(x,y));
79         }else if(id[0]=='b'){
80             if(getfa(x)==getfa(y))printf("no\n");
81             else printf("yes\n"),link(x,y);
82         }else if(id[0]=='p')change(x,y);
83     }
84     return 0;
85 }
View Code

相关文章: