记录编号 143946 评测结果 AAAAAAAAAA
题目名称 [ZJOI 2008]树的统计Count 最终得分 100
用户昵称 Gravatarcstdio 是否通过 通过
代码语言 C++ 运行时间 2.622 s
提交时间 2014-12-19 08:57:33 内存使用 1.12 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. const int SIZEN=30010,INF=0x7fffffff/2;
  7. inline int max(int a,int b,int c){return max(max(a,b),c);}
  8. class Node{
  9. public:
  10. int lc,rc,fa;
  11. int val,sum,mx;
  12. bool inv;
  13. void clear(void){
  14. lc=rc=fa=0;
  15. val=sum=0;mx=-INF;
  16. inv=0;
  17. }
  18. Node(void){clear();}
  19. #define lc(x) Tree[x].lc
  20. #define rc(x) Tree[x].rc
  21. #define fa(x) Tree[x].fa
  22. #define val(x) Tree[x].val
  23. #define sum(x) Tree[x].sum
  24. #define mx(x) Tree[x].mx
  25. #define inv(x) Tree[x].inv
  26. };
  27. Node Tree[SIZEN];
  28. bool Is_Root(int x){return lc(fa(x))!=x&&rc(fa(x))!=x;}
  29. void Push_Down(int x){
  30. if(!x) return;
  31. if(inv(x)){
  32. swap(lc(x),rc(x));
  33. inv(lc(x))^=1;
  34. inv(rc(x))^=1;
  35. inv(x)=false;
  36. }
  37. }
  38. void Update(int x){
  39. if(!x) return;
  40. sum(x)=sum(lc(x))+sum(rc(x))+val(x);
  41. mx(x)=max(mx(lc(x)),mx(rc(x)),val(x));
  42. }
  43. void Zig(int x){//右旋
  44. int y=fa(x),z=fa(y);
  45. if(lc(z)==y) lc(z)=x;
  46. if(rc(z)==y) rc(z)=x;
  47. fa(x)=z;
  48. lc(y)=rc(x);fa(lc(y))=y;
  49. rc(x)=y;fa(y)=x;
  50. Update(y);Update(x);
  51. }
  52. void Zag(int x){//左旋
  53. int y=fa(x),z=fa(y);
  54. if(lc(z)==y) lc(z)=x;
  55. if(rc(z)==y) rc(z)=x;
  56. fa(x)=z;
  57. rc(y)=lc(x);fa(rc(y))=y;
  58. lc(x)=y;fa(y)=x;
  59. Update(y);Update(x);
  60. }
  61. void Splay(int x){
  62. Push_Down(x);
  63. while(!Is_Root(x)){
  64. int y=fa(x),z=fa(y);
  65. Push_Down(z);Push_Down(y);Push_Down(x);
  66. if(Is_Root(y)){
  67. if(lc(y)==x) Zig(x);
  68. else Zag(x);
  69. return;
  70. }
  71. if(lc(z)==y){
  72. if(lc(y)==x) Zig(y),Zig(x);
  73. else Zag(x),Zig(x);
  74. }
  75. else{
  76. if(rc(y)==x) Zag(y),Zag(x);
  77. else Zig(x),Zag(x);
  78. }
  79. }
  80. }
  81. void Access(int x){
  82. int y=0;
  83. while(x){
  84. Splay(x);
  85. rc(x)=y;
  86. Update(x);
  87. y=x;
  88. x=fa(x);
  89. }
  90. }
  91. void Make_Root(int x){
  92. Access(x);
  93. Splay(x);
  94. inv(x)^=1;
  95. }
  96. void Link(int x,int y){
  97. Make_Root(x);
  98. fa(x)=y;
  99. }
  100. int Query_Max(int x,int y){
  101. Make_Root(x);
  102. Access(y);
  103. Splay(y);
  104. return mx(y);
  105. }
  106. int Query_Sum(int x,int y){
  107. Make_Root(x);
  108. Access(y);
  109. Splay(y);
  110. return sum(y);
  111. }
  112. void Change_Node(int x,int t){
  113. Access(x);
  114. Splay(x);
  115. val(x)=t;
  116. Update(x);
  117. }
  118. int N,Q;
  119. void Process(void){
  120. scanf("%d",&Q);
  121. char cmd[10];
  122. int a,b;
  123. for(int i=1;i<=Q;i++){
  124. scanf("%s",cmd);
  125. scanf("%d%d",&a,&b);
  126. if(cmd[0]=='C') Change_Node(a,b);
  127. else if(cmd[1]=='M') printf("%d\n",Query_Max(a,b));
  128. else if(cmd[1]=='S') printf("%d\n",Query_Sum(a,b));
  129. }
  130. }
  131. void Init(void){
  132. scanf("%d",&N);
  133. int a,b,w;
  134. for(int i=1;i<N;i++){
  135. scanf("%d%d",&a,&b);
  136. Link(a,b);
  137. }
  138. for(int i=1;i<=N;i++){
  139. scanf("%d",&w);
  140. Change_Node(i,w);
  141. }
  142. }
  143. int main(){
  144. freopen("bzoj_1036.in","r",stdin);
  145. freopen("bzoj_1036.out","w",stdout);
  146. Init();
  147. Process();
  148. return 0;
  149. }