KiK0S

hld

Mar 9th, 2019
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <set>
  5. #include <queue>
  6. #include <algorithm>
  7. #include <string>
  8. #include <cmath>
  9. #include <cstdio>
  10. #include <iomanip>
  11. #include <fstream>
  12. #include <cassert>
  13. #include <cstring>
  14. #include <unordered_set>
  15. #include <unordered_map>
  16. #include <numeric>
  17. #include <ctime>
  18. #include <bitset>
  19. #include <complex>
  20. using namespace std;
  21.  
  22. typedef long long ll;
  23.  
  24. #ifdef DEBUG
  25.     const int MAXN = 10;
  26. #else
  27.     const int MAXN = 2e5;
  28. #endif
  29. const int MAXLOG = 18;
  30.  
  31. int n;
  32. vector<int> g[MAXN];
  33. int sz[MAXN];
  34. int tin[MAXN];
  35. int par[MAXN];
  36. int rt[MAXN];
  37. int pre[MAXN][MAXLOG];
  38. int segtree[4 * MAXN];
  39.  
  40. void upd(int v, int tl, int tr, int pos, int x) {
  41.     if (tl == tr) {
  42.         segtree[v] += x;
  43.         return;
  44.     }
  45.     int tm = (tl + tr) >> 1;
  46.     if (pos <= tm) {
  47.         upd(2 * v, tl, tm, pos, x);
  48.     }
  49.     else {
  50.         upd(2 * v + 1, tm + 1, tr, pos, x);
  51.     }
  52.     segtree[v] = max(segtree[2 * v], segtree[2 * v + 1]);
  53. }
  54.  
  55. int get(int v, int tl, int tr, int l, int r) {
  56.     if (tl > r || l > tr) {
  57.         return -1;
  58.     }
  59.     if (l <= tl && tr <= r) {
  60.         return segtree[v];
  61.     }
  62.     int tm = (tl + tr) / 2;
  63.     return max(get(2 * v, tl, tm, l, r), get(2 * v + 1, tm + 1, tr, l, r));
  64. }
  65.  
  66. int timer = 0;
  67.  
  68. void dfs(int v, int p = -1) {
  69.     sz[v] = 1;
  70.     int mx = -1;
  71.     int pos = -1;
  72.     for (auto to : g[v]) {
  73.         pos++;
  74.         if (to == p) {
  75.             continue;
  76.         }
  77.         dfs(to, v);
  78.         if (mx == -1 || sz[to] > sz[g[v][mx]]) {
  79.             mx = pos;
  80.         }
  81.         sz[v] += sz[to];
  82.     }
  83.     if (mx != -1) {
  84.         swap(g[v][0], g[v][mx]);
  85.     }
  86. }
  87.  
  88. void calc(int v, int p = 0, int root = -1) {
  89.     tin[v] = timer++;
  90.     pre[v][0] = p;
  91.     for (int i = 1; i < MAXLOG; i++) {
  92.         pre[v][i] = pre[pre[v][i - 1]][i - 1];
  93.     }
  94.     if (root == -1) {
  95.         root = v;
  96.     }
  97.     rt[v] = root;
  98.     int alr = 0;
  99.     for (auto to : g[v]) {
  100.         if (to == p) {
  101.             continue;
  102.         }
  103.         if (!alr) {
  104.             calc(to, v, root);
  105.             alr = 1;
  106.         }
  107.         else {
  108.             calc(to, v);
  109.         }
  110.     }
  111. }
  112.  
  113. int isp(int a, int b) {
  114.     return tin[a] <= tin[b] && tin[b] <= tin[a] + sz[a] - 1;
  115. }
  116.  
  117. int lca(int a, int b) {
  118.     if (isp(a, b)) {
  119.         return a;
  120.     }
  121.     if (isp(b, a)) {
  122.         return b;
  123.     }
  124.     for (int i = MAXLOG - 1; i >= 0; i--) {
  125.         if (!isp(pre[a][i], b)) {
  126.             a = pre[a][i];
  127.         }
  128.     }
  129.     return pre[a][0];
  130. }
  131.  
  132. inline void init() {
  133.  
  134. }
  135.  
  136. inline void solve() {
  137.     init();
  138.     for (int i = 0; i < n - 1; i++) {
  139.         int a, b;
  140.         cin >> a >> b;
  141.         a--, b--;
  142.         g[a].push_back(b);
  143.         g[b].push_back(a);
  144.     }
  145.     dfs(0);
  146.     calc(0);
  147.     // for (int i = 0; i < n; i++) {
  148.     //  cerr << tin[i] << ' ';
  149.     // }
  150.     // cerr << '\n';
  151.     int m;
  152.     cin >> m;
  153.     for (int i = 0; i < m; i++) {
  154.         char c;
  155.         cin >> c;
  156.         if (c == 'I') {
  157.             int a, b;
  158.             cin >> a >> b;
  159.             a--;
  160.             upd(1, 0, MAXN - 1, tin[a], b);
  161.         }
  162.         else {
  163.             int l, r;
  164.             cin >> l >> r;
  165.             l--, r--;
  166.             int w = lca(l, r);
  167.             // cerr << l << ' ' << r << ' ' << w << '\n';
  168.             int ans = 0;
  169.             while (!isp(rt[l], w)) {
  170.                 // cerr << "get " << tin[rt[l]] << ' ' << tin[l] << '\n';
  171.                 ans = max(ans, get(1, 0, MAXN - 1, tin[rt[l]], tin[l]));
  172.                 l = pre[rt[l]][0];
  173.             }
  174.             // cerr << "get " << tin[w] << ' ' << tin[l] << '\n';
  175.             ans = max(ans, get(1, 0, MAXN - 1, tin[w], tin[l]));
  176.             while (!isp(rt[r], w)) {
  177.                 // cerr << "get " << tin[rt[r]] << ' ' << tin[r] << '\n';
  178.                 ans = max(ans, get(1, 0, MAXN - 1, tin[rt[r]], tin[r]));
  179.                 r = pre[rt[r]][0];
  180.             }
  181.             // cerr << "get " << tin[w] << ' ' << tin[r] << '\n';
  182.             ans = max(ans, get(1, 0, MAXN - 1, tin[w], tin[r]));
  183.             cout << ans << '\n';
  184.         }
  185.     }
  186. }
  187.  
  188. signed main() {
  189.     #ifdef DEBUG
  190.         freopen("F.in", "r", stdin);
  191.         freopen("F.out", "w", stdout);
  192.     #else
  193.         freopen("caves.in", "r", stdin);
  194.         freopen("caves.out", "w", stdout);
  195.     #endif
  196.     ios_base::sync_with_stdio(0);
  197.     cin.tie(0);
  198.     cout.tie(0);
  199.     while (cin >> n)
  200.         solve();
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment