fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. typedef long long ll;
  7. #define SZ(a) (int)(a).size()
  8.  
  9. typedef ll E;
  10. struct nd {
  11. typedef nd* ndp;
  12. E e;
  13. E lazy;
  14. ndp p;
  15. ndp c[2];
  16. nd(E e, ndp p = NULL, ndp l = NULL, ndp r = NULL) : e(e), p(p) {
  17. c[0] = l;
  18. c[1] = r;
  19. lazy = 0;
  20. }
  21. };
  22. typedef nd* ndp;
  23.  
  24. int ic(ndp x, ndp y) {
  25. return !y ? -1
  26. : x == y->c[0] ? 0
  27. : x == y->c[1] ? 1
  28. : -1
  29. ;
  30. }
  31.  
  32. void apply_lazy(ndp x) {
  33. for (int i = 0; i < 2; i++) {
  34. auto v = x->c[i];
  35. if (v) {
  36. v->lazy += x->lazy;
  37. }
  38. }
  39. x->e += x->lazy;
  40. x->lazy = 0;
  41. }
  42.  
  43. void rot(ndp y, int i) {
  44. auto z = y->p;
  45. auto x = y->c[i];
  46. auto w = x->c[i ^ 1];
  47. auto j = ic(y, z);
  48. apply_lazy(x);
  49. if (j != -1) {
  50. z->c[j] = x;
  51. }
  52. x->p = z;
  53. x->c[i ^ 1] = y;
  54. y->p = x;
  55. y->c[i] = w;
  56. if (w) {
  57. w->p = y;
  58. }
  59. x->lazy = y->lazy;
  60. y->lazy = 0;
  61. }
  62.  
  63. void splay(ndp x) {
  64. while (true) {
  65. auto y = x->p;
  66. auto i = ic(x, y);
  67. if (i == -1) break;
  68. auto z = y->p;
  69. auto j = ic(y, z);
  70. if (j == -1) {
  71. rot(y, i);
  72. break;
  73. }
  74. if (i == j) {
  75. rot(z, j);
  76. rot(y, i);
  77. }
  78. else {
  79. rot(y, i);
  80. rot(z, j);
  81. }
  82. }
  83. }
  84.  
  85. void splice(ndp x) {
  86. auto y = x->p;
  87. apply_lazy(y);
  88. y->c[0] = x;
  89. }
  90.  
  91. void splay_splice(ndp x) {
  92. for (auto y = x; y; y = y->p) {
  93. splay(y);
  94. }
  95. for (auto y = x; y->p; y = y->p) {
  96. splice(y);
  97. }
  98. splay(x);
  99. }
  100.  
  101. void link(ndp x, ndp y) {
  102. splay_splice(y);
  103. splay(x);
  104. x->p = y;
  105. auto cnt = x->e+x->lazy;
  106. y->e += cnt;
  107. auto w = y->c[1];
  108. if (w) {
  109. w->lazy += cnt;
  110. }
  111. }
  112.  
  113. void cut(ndp x) {
  114. splay_splice(x);
  115. auto w = x->c[1];
  116. x->c[1] = NULL;
  117. w->p = NULL;
  118. w->lazy -= x->e;
  119. }
  120.  
  121. vector<ndp> a;
  122. vector<vector<int>> al;
  123. vector<bool> ons;
  124. vector<int> ps;
  125.  
  126. void dfs(int u, int p) {
  127. ps[u] = p;
  128. link(a[u], a[p]);
  129. for (auto v: al[u]) if (v != p) {
  130. dfs(v, u);
  131. }
  132. }
  133.  
  134. void flip(int u) {
  135. ons[u] = !ons[u];
  136. if (ons[u]) {
  137. cut(a[u]);
  138. }
  139. else {
  140. link(a[u], a[ps[u]]);
  141. }
  142. }
  143.  
  144. E query(int u) {
  145. auto x = a[u];
  146. splay_splice(x);
  147. return x->e+x->lazy;
  148. }
  149.  
  150. int main() {
  151. cin.sync_with_stdio(false);
  152. int n;
  153. cin >> n;
  154. a.resize(n+1);
  155. a[n] = new nd(0);
  156. for (int i = 0; i < n; i++) {
  157. int e;
  158. cin >> e;
  159. a[i] = new nd(e);
  160. }
  161. al.assign(n, {});
  162. for (int i = 0; i < n-1; i++) {
  163. int u, v;
  164. cin >> u >> v;
  165. u--; v--;
  166. al[u].emplace_back(v);
  167. al[v].emplace_back(u);
  168. }
  169. ps.resize(n);
  170. dfs(0, n);
  171. ons.assign(n, false);
  172. int nq;
  173. cin >> nq;
  174. for (int iq = 0; iq < nq; iq++) {
  175. int t, u;
  176. cin >> t >> u;
  177. u--;
  178. if (t == 1) {
  179. flip(u);
  180. }
  181. else {
  182. auto ans = ons[u] ? 0 : query(u);
  183. printf("%lld\n", ans);
  184. }
  185. }
  186. return 0;
  187. }
  188.  
Time limit exceeded #stdin #stdout 5s 8388607KB
stdin
Standard input is empty
stdout
Standard output is empty