Advertisement
ivnikkk

Untitled

Jul 6th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. struct Hld {
  2.     struct Segtree {
  3.         int n;
  4.         vector<int> t;
  5.         void build(int v, int tl, int tr, vector<int>& a) {
  6.             if (tl == tr) t[v] = a[tl];
  7.             else {
  8.                 int tm = (tl + tr) / 2;
  9.                 build(v * 2, tl, tm, a); build(v * 2 + 1, tm + 1, tr, a);
  10.                 t[v] = max(t[v * 2], t[v * 2 + 1]);
  11.             }
  12.         }
  13.         Segtree() {}
  14.         void init(vector<int>& a) {
  15.             n = (int)a.size();
  16.             t.resize(4 * n, -inf);
  17.             build(1, 0, n - 1, a);
  18.         }
  19.         void update(int v, int tl, int tr, int pos, int now) {
  20.             if (tl == tr)  t[v] = now;
  21.             else {
  22.                 int tm = (tl + tr) / 2;
  23.                 if (pos <= tm)update(v * 2, tl, tm, pos, now);
  24.                 else update(v * 2 + 1, tm + 1, tr, pos, now);
  25.                 t[v] = max(t[v * 2], t[v * 2 + 1]);
  26.             }
  27.         }
  28.         int get_mx(int v, int tl, int tr, int l, int r) {
  29.             if (tr<l || tl>r)return -inf;
  30.             if (tl >= l && tr <= r)return t[v];
  31.             int tm = (tl + tr) / 2;
  32.             return max(get_mx(v * 2, tl, tm, l, r), get_mx(v * 2 + 1, tm + 1, tr, l, r));
  33.         }
  34.     };
  35.  
  36.     vector<vector<int>> gr;
  37.     int t, Size_n;
  38.     vector<int> siz, tin, tout, head, euler_dfs, p, w;
  39.  
  40.     Hld() {}
  41.     Hld(int _n) :Size_n(_n) {
  42.         w.resize(Size_n), gr.resize(Size_n);
  43.     }
  44.     inline void Scan_weight() {
  45.         for (int& i : w)cin >> i;
  46.     }
  47.     inline void Scan_tree() {
  48.         for (int i = 0; i < Size_n - 1; i++) {
  49.             int u, v;
  50.             cin >> u >> v;
  51.             u--, v--;
  52.             gr[u].push_back(v);
  53.             gr[v].push_back(u);
  54.         }
  55.     }
  56.  
  57.     void sizes(int v, int pr) {
  58.         p[v] = pr, siz[v] = 1;
  59.         gr[v].erase(find(all(gr[v]), pr));
  60.         for (int& u : gr[v]) {
  61.             sizes(u, v);
  62.             siz[v] += siz[u];
  63.             if (siz[u] > siz[gr[v][0]]) {
  64.                 swap(u, gr[v][0]);
  65.             }
  66.         }
  67.     }
  68.     void hld(int v) {
  69.         tin[v] = t++; euler_dfs.push_back(w[v]);
  70.         for (int& u : gr[v]) {
  71.             if (u == gr[v][0]) head[u] = head[v];
  72.             else head[u] = u;
  73.             hld(u);
  74.         }
  75.         tout[v] = t;
  76.     }
  77.     Segtree Euler;
  78.     void build() {
  79.         gr[0].push_back(0);
  80.         t = 0;
  81.         siz.resize(Size_n), tin.resize(Size_n), tout.resize(Size_n), head.resize(Size_n), p.resize(Size_n);
  82.         sizes(0, 0);
  83.         hld(0);
  84.         Euler.init(euler_dfs);
  85.     }
  86.  
  87.     inline void up(int& u, int& v, int& ans, auto& ancestor) {
  88.         while (!ancestor(head[u], v)) {
  89.             ans = max(ans, Euler.get_mx(1, 0, Euler.n - 1, tin[head[u]], tin[u]));
  90.             u = p[head[u]];
  91.         }
  92.     }
  93.  
  94.     int get_max(int u, int v) {
  95.         auto ancestor = [&](const int& u, const int& v) {
  96.             return tin[u] <= tin[v] && tout[u] > tin[v];
  97.         };
  98.         int ans = -inf;
  99.         up(u, v, ans, ancestor);
  100.         up(v, u, ans, ancestor);
  101.         if (!ancestor(u, v)) swap(u, v);
  102.         ans = max(ans, Euler.get_mx(1, 0, Euler.n - 1, tin[u], tin[v]));
  103.         return ans;
  104.     }
  105.     inline void update(const int& u, const int& new_val) {
  106.         Euler.update(1, 0, Euler.n - 1, tin[u], new_val);
  107.     }
  108. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement