Advertisement
ivnikkk

Untitled

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