[关闭]
@MLEAutoMaton 2019-04-16T12:17:06.000000Z 字数 2076 阅读 821

[COCI 2009] OTOCI / 极地旅行社

LCT


传送门

洛谷
BZOJ

Solution

随便写吧,就是一道裸的LCT模板

代码实现

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<math.h>
  5. #include<iostream>
  6. #include<queue>
  7. #include<algorithm>
  8. #include<map>
  9. #include<set>
  10. using namespace std;
  11. #define ll long long
  12. #define re register
  13. #define file(a) freopen(a".in",,"r",stdin);freopen(a".out","w",stdout)
  14. inline int gi()
  15. {
  16. int f=1,sum=0;char ch=getchar();
  17. while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
  18. while(ch>='0' && ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
  19. return f*sum;
  20. }
  21. const int N=500010;
  22. struct node
  23. {
  24. int val,ff,sum,rev,ch[2];
  25. }t[N<<1];
  26. int top,sta[N];
  27. void pushup(int x){t[x].sum=t[t[x].ch[0]].sum+t[t[x].ch[1]].sum+t[x].val;}
  28. void reverse(int o){t[o].rev^=1;swap(t[o].ch[0],t[o].ch[1]);}
  29. void pushdown(int o){if(!t[o].rev)return;if(t[o].ch[0])reverse(t[o].ch[0]);if(t[o].ch[1])reverse(t[o].ch[1]);t[o].rev^=1;}
  30. bool isroot(int o){return (t[t[o].ff].ch[0]!=o) && (t[t[o].ff].ch[1]!=o);}
  31. void rotate(int x){int y=t[x].ff,z=t[y].ff;if(!isroot(y)){t[z].ch[t[z].ch[1]==y]=x;}t[x].ff=z;int k=t[y].ch[1]==x;t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;t[x].ch[k^1]=y;t[y].ff=x;pushup(y);}
  32. void splay(int x){sta[++top]=x;for(int pos=x;!isroot(pos);pos=t[pos].ff)sta[++top]=t[pos].ff;while(top)pushdown(sta[top--]);while(!isroot(x)){int y=t[x].ff,z=t[y].ff;if(!isroot(y))(t[y].ch[0]==x)^(t[z].ch[0]==y)?rotate(x):rotate(y);rotate(x);}pushup(x);}
  33. void access(int x){for(int y=0;x;y=x,x=t[x].ff)splay(x),t[x].ch[1]=y,pushup(x);}
  34. void makeroot(int x){access(x);splay(x);reverse(x);}
  35. int findroot(int x){access(x);splay(x);while(t[x].ch[0])x=t[x].ch[0];return x;}
  36. void split(int x,int y){makeroot(x);access(y);splay(y);}
  37. void link(int x,int y){makeroot(x);t[x].ff=y;}
  38. void cut(int x,int y){split(x,y);t[x].ff=t[y].ch[0]=0;}
  39. int n,m;char s[110];
  40. int main()
  41. {
  42. n=gi();
  43. for(int i=1;i<=n;i++)t[i].val=t[i].sum=gi();
  44. int Q=gi();
  45. while(Q--)
  46. {
  47. scanf("%s",s);int x=gi();
  48. if(s[0]=='b')
  49. {
  50. int b=gi();
  51. if(findroot(x)==findroot(b))puts("no");
  52. else{puts("yes");link(x,b);}
  53. }
  54. if(s[0]=='p')
  55. {
  56. access(x);splay(x);
  57. t[x].val=gi();pushup(x);
  58. }
  59. if(s[0]=='e')
  60. {
  61. int y=gi();
  62. if(findroot(x)!=findroot(y))puts("impossible");
  63. else
  64. {
  65. split(x,y);printf("%d\n",t[y].sum);
  66. }
  67. }
  68. }
  69. return 0;
  70. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注