Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. /** By @ZhakudaMagzhan **/
  2.  
  3. # include <bits/stdc++.h>
  4.  
  5. # define fi first
  6. # define se second
  7.  
  8. # define y1 ioi_ioi
  9. # define endl "\n"
  10. # define ioi exit(0)
  11.  
  12. # define mp make_pair
  13. # define pp pop_back
  14. # define pb push_back
  15.  
  16. # define ub upper_bound
  17. # define lb lower_bound
  18.  
  19. # define sqr(x) (x)*(x)
  20. # define sz(x) (int)x.size()
  21. # define all(x) x.begin(), x.end()
  22. # define bit(x) __builtin_popcountll(x)
  23. # define uniq(x) x.resize(unique(all(x))-x.begin())
  24. # define CLOCK clock()/(double)CLOCKS_PER_SEC
  25.  
  26. # define rep(i, a, n) for (int i = a; i <= n; ++i)
  27. # define per(i, a, n) for (int i = n; i >= a; --i)
  28.  
  29. # define arep(i, a, n, x) for (int i = a; i <= n; i += x)
  30. # define aper(i, a, n, x) for (int i = n; i >= a; i -= x)
  31.  
  32. using namespace std;
  33.  
  34. typedef long double ld;
  35. typedef long long ll;
  36. typedef unsigned long long ull;
  37.  
  38. typedef pair <ll, ll> pll;
  39. typedef pair <int,int> pii;
  40.  
  41. typedef vector <ll> vll;
  42. typedef vector <int> vi;
  43. typedef vector <pii> vpii;
  44.  
  45.  
  46. const int inf = (int) 2e9, mod = 999983ll;
  47. const ll linf = (ll) 2e18;
  48.  
  49. int kob (int x, int y) { return ((ll)x * y) % mod; }
  50. int aza (int x, int y) { x -= y; if (x < 0) return x + mod; return x; }
  51. int kos (int x, int y) { x += y; if (x >= mod) return x - mod; return x; }
  52.  
  53. const int maxn = (int) 1e5 + 666, maxn2 = (int) 1e6 + 666;
  54.  
  55. int n, a[maxn], T[maxn*4], b[maxn], top[maxn], lrpos[maxn];
  56. int cntCh[maxn]; // balalarin sanaimiz
  57. vector <int> g[maxn], ans; // graph
  58. vector <int> now;
  59. bool used[maxn];
  60. int comp = -1; /// hld-nin komponenttari
  61. vector <pii> lr; /// l-r lari saktaladi
  62. int pos[maxn]; ///
  63.  
  64.  
  65. /// LCA
  66. int tin[maxn], tout[maxn], timer;
  67. int up[maxn][25];
  68.  
  69. void preDfs (int x, int pr = -1) {
  70. tin[x] = ++ timer;
  71. up[x][0] = pr;
  72. for (int i = 1; i <= 20; ++ i)
  73. up[x][i] = up[up[x][i-1]][i-1];
  74.  
  75. if (sz (g[x]) == 1 && g[x][0] == pr) {
  76. cntCh[x] = 1;
  77. }
  78. for (auto to: g[x]) {
  79. if (to != pr) {
  80. preDfs (to, x);
  81. cntCh[x] += cntCh[to];
  82. }
  83. }
  84. cntCh[x] ++;
  85.  
  86. tout[x] = ++ timer;
  87. }
  88.  
  89. void buildDfs (int x, bool start, int pr = -1) {
  90. if (start) {
  91. used[x] = true;
  92. int l, r;
  93. if (comp != -1) {
  94. l = sz (ans)+1;
  95. }
  96. ans.insert (ans.end (), all (now));
  97. if (comp != -1) {
  98. r = sz (ans);
  99. }
  100.  
  101. if (comp != -1) {
  102. lr.pb (mp (l, r));
  103. }
  104.  
  105. now.clear ();
  106. comp ++;
  107. now.pb (x);
  108. int opt = -1;
  109. int mx = -inf;
  110. for (auto to: g[x]) {
  111. if (to != pr && cntCh[to] > mx && !used[to]) {
  112. mx = a[to];
  113. opt = to;
  114. }
  115. }
  116.  
  117. if (opt == -1) {
  118. return;
  119. }
  120. buildDfs (opt, 0, x);
  121. for (auto to: g[x]) {
  122. if (to == pr || to == opt || used[to]) {
  123. continue;
  124. }
  125. buildDfs (to, 1, x);
  126. }
  127. }
  128. else {
  129. used[x] = true;
  130. now.pb (x);
  131. int opt = -1;
  132. int mx = -inf;
  133. for (auto to: g[x]) {
  134. if (to != pr && cntCh[to] > mx && !used[to]) {
  135. mx = a[to];
  136. opt = to;
  137. }
  138. }
  139.  
  140. if (opt == -1) {
  141. return;
  142. }
  143. buildDfs (opt, 0, x);
  144. for (auto to: g[x]) {
  145. if (to == pr || to == opt || used[to]) {
  146. continue;
  147. }
  148. buildDfs (to, 1, x);
  149. }
  150. }
  151. }
  152. ///*******DO*begin******///
  153. void buildTree (int x, int tl, int tr) {
  154. if (tl == tr) {
  155. T[x] = a[b[tl]];
  156. }
  157. else {
  158. int mid = (tl + tr) / 2;
  159. buildTree (x * 2, tl, mid);
  160. buildTree (x * 2 + 1, mid + 1, tr);
  161. T[x] = max (T[x*2], T[x*2+1]);
  162. }
  163. }
  164.  
  165. int getsum (int x, int tl, int tr, int l, int r) {
  166. if (tr < l || r < tl) return -inf;
  167. if (l <= tl && tr <= r) return T[x];
  168. int mid = (tl + tr) / 2;
  169. return max (getsum (x * 2, tl, mid, l, r), getsum (x * 2 + 1, mid + 1, tr, l, r));
  170. }
  171.  
  172. void upd (int x, int tl, int tr, int pos, int val) {
  173. if (tl == tr) {
  174. T[x] += val;
  175. }
  176. else {
  177. int mid = (tl + tr) / 2;
  178. if (pos <= mid) {
  179. upd (x * 2, tl, mid, pos, val);
  180. }
  181. else {
  182. upd (x * 2 + 1, mid + 1, tr, pos, val);
  183. }
  184. T[x] = max (T[x * 2], T[x * 2 + 1]);
  185. }
  186. }
  187.  
  188. ///*******DO*end******///
  189.  
  190. void build () {
  191. preDfs (1, 1);
  192. buildDfs (1, 1);
  193.  
  194. if (sz (now)) {
  195. int l, r;
  196. if (comp != -1) {
  197. l = sz (ans)+1;
  198. }
  199. ans.insert (ans.end (), all (now));
  200. if (comp != -1) {
  201. r = sz (ans);
  202. }
  203. if (comp != -1) {
  204. lr.pb (mp (l, r));
  205. }
  206. now.clear ();
  207. }
  208.  
  209. for (int i = 0; i < sz (ans); ++ i) {
  210. b[i+1] = ans[i];
  211. }
  212.  
  213. int id = 0, cnt = 1;
  214. for (int i = 1; i <= n; ++ i) {
  215. pos[b[i]] = i;
  216. if (lr[id].fi <= i && i <= lr[id].se) {
  217. lrpos[b[i]] = id;
  218. if (top[id] == -1)
  219. top[id] = i;
  220. }
  221. else {
  222. ++ id;
  223. lrpos[b[i]] = id;
  224. top[id] = i;
  225. }
  226. }
  227.  
  228. buildTree (1, 1, n);
  229. }
  230.  
  231. int isParent (int x, int y) {
  232. return tin[x] <= tin[y] && tout[x] >= tout[y];
  233. }
  234.  
  235. int getLca (int x, int y) {
  236. if (isParent (x, y)) return x;
  237. if (isParent (x, y)) return y;
  238.  
  239. for (int i = 20; i >= 0; -- i) {
  240. if (!isParent (up[x][i], y))
  241. x = up[x][i];
  242. }
  243. return up[x][0];
  244. }
  245.  
  246. int GETANS (int a, int c) {
  247. int ans = -inf;
  248.  
  249. while (lrpos[a] != lrpos[c]) {
  250. int l = top[lrpos[a]];
  251. int r = pos[a];
  252.  
  253. ans = max (ans, getsum (1, 1, n, l, r));
  254. a = up[b[l]][0];
  255. }
  256.  
  257. int l = pos[c];
  258. int r = pos[a];
  259. ans = max (ans, getsum (1, 1, n, l, r));
  260. return ans;
  261. }
  262.  
  263. int getAns (int x, int y) {
  264. int c = getLca (x, y);
  265.  
  266. return max (GETANS (x, c), GETANS (y, c));
  267. }
  268.  
  269. void upd (int x, int val) {
  270. x = pos[x];
  271. upd (1, 1, n, x, val);
  272. }
  273.  
  274. int32_t main () {
  275. #define FN ""
  276. #ifdef GOLD
  277. freopen (FN".in", "r", stdin);
  278. freopen (FN".out", "w", stdout);
  279. #endif
  280. ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  281. memset (top, -1, sizeof (top));
  282.  
  283. cin >> n;
  284. for (int i = 1; i < n; ++ i) {
  285. int u, v;
  286. cin >> u >> v;
  287. g[u].pb (v);
  288. g[v].pb (u);
  289. }
  290.  
  291. build ();
  292.  
  293. int q;
  294. cin >> q;
  295. for (int i = 1; i <= q; ++ i) {
  296. char t;
  297. int u, b;
  298. cin >> t >> u >> b;
  299. if (t == 'G') {
  300. cout << getAns (u, b) << endl;
  301. }
  302. else {
  303. upd (u, b);
  304. a[u] += b;
  305. }
  306. }
  307. return 0;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement