bingxuan9112

SPOJ QTREE LCT

Mar 29th, 2021
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #ifdef local
  3. #define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
  4. #define debug(a...) qqbx(#a, a)
  5. #define pary(a...) danb(#a, a)
  6. template <typename ...T> void qqbx(const char *s, T ...a) {
  7.     int cnt = sizeof...(T);
  8.     ((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
  9. }
  10. template <typename T> void danb(const char *s, T L, T R) {
  11.     std::cerr << "\033[1;32m[ " << s << " ] = [ ";
  12.     for (auto it = L; it != R; ++it)
  13.         std::cerr << *it << ' ';
  14.     std::cerr << "]\033[0m\n";
  15. }
  16. #else
  17. #define safe ((void)0)
  18. #define debug(...) ((void)0)
  19. #define pary(...) ((void)0)
  20. #endif // local
  21. #define all(v) begin(v),end(v)
  22. #define pb emplace_back
  23. #define sort_uni(v) sort(all(v)),v.erase(unique(all(v)), v.end())
  24. #define get_pos(u,v) int(lower_bound(all(u), v) - u.begin())
  25.  
  26. using namespace std;
  27. using ll = int64_t;
  28. using ld = long double;
  29. template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
  30. const int maxn = 100025;
  31.  
  32. struct LinkCutTree {
  33.     struct node {
  34.         int pa, ch[2];
  35.         int val, mx;
  36.         node() : pa(), ch{}, val(), mx() {}
  37.         node(int p, int c) : pa(p), ch{}, val(c), mx(c) {}
  38.     } S[maxn];
  39.     void init(int n) {
  40.         for (int i = 1; i <= n; i++) S[i] = node();
  41.     }
  42.     void pull(int x) {
  43.         S[x].mx = max({ S[x].val, S[S[x].ch[0]].mx, S[S[x].ch[1]].mx });
  44.     }
  45.     bool dir(int x) {
  46.         return S[S[x].pa].ch[1] == x;
  47.     }
  48.     bool isRoot(int x) {
  49.         return S[S[x].pa].ch[dir(x)] != x;
  50.     }
  51.     void rot(int x) {
  52.         int y = S[x].pa, z = S[y].pa, d = dir(x);
  53.         S[x].pa = z;
  54.         if (!isRoot(y)) S[z].ch[dir(y)] = x;
  55.         S[y].ch[d] = S[x].ch[!d];
  56.         if (S[x].ch[!d]) S[S[x].ch[!d]].pa = y;
  57.         S[y].pa = x;
  58.         S[x].ch[!d] = y;
  59.         pull(x);
  60.         pull(y);
  61.     }
  62.     void splay(int x) {
  63.         while (!isRoot(x)) {
  64.             if (int y = S[x].pa; !isRoot(y))
  65.                 rot(dir(x) != dir(y) ? y : x);
  66.             rot(x);
  67.         }
  68.     }
  69.     int access(int x) {
  70.         int last = 0;
  71.         while (x) {
  72.             splay(x);
  73.             S[x].ch[1] = last;
  74.             pull(x);
  75.             last = x;
  76.             x = S[x].pa;
  77.         }
  78.         return last;
  79.     }
  80.     int query(int a, int b) {
  81.         access(a);
  82.         int lca = access(b);
  83.         if (lca != a) {
  84.             splay(a);
  85.             return max(S[S[lca].ch[1]].mx, S[a].mx);
  86.         } else {
  87.             return S[S[lca].ch[1]].mx;
  88.         }
  89.     }
  90.     void modify(int i, int v) {
  91.         splay(i);
  92.         S[i].val = v;
  93.         pull(i);
  94.     }
  95. } lct;
  96.  
  97. vector<tuple<int,int,int>> g[maxn];
  98. int below[maxn];
  99. void dfs(int i, int p) {
  100.     for (auto [j, c, id]: g[i]) {
  101.         if (j == p) continue;
  102.         below[id] = j;
  103.         lct.S[j] = LinkCutTree::node(i, c);
  104.         dfs(j, i);
  105.     }
  106. }
  107. signed main() {
  108.     ios_base::sync_with_stdio(0), cin.tie(0);
  109.     int T;
  110.     cin >> T;
  111.     while (T--) {
  112.         int n;
  113.         cin >> n;
  114.         for (int i = 1; i <= n; i++) g[i].clear();
  115.         lct.init(n);
  116.         for (int i = 1; i < n; i++) {
  117.             int a, b, c;
  118.             cin >> a >> b >> c;
  119.             g[a].pb(b, c, i);
  120.             g[b].pb(a, c, i);
  121.         }
  122.         dfs(1, 0);
  123.         string com;
  124.         while (cin >> com && com != "DONE") {
  125.             if (com == "CHANGE") {
  126.                 int a, b;
  127.                 cin >> a >> b;
  128.                 lct.modify(below[a], b);
  129.             } else if (com == "QUERY") {
  130.                 int a, b;
  131.                 cin >> a >> b;
  132.                 cout << lct.query(a, b) << '\n';
  133.             }
  134.         }
  135.     }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment