Advertisement
ivnikkk

Untitled

Aug 3rd, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 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.     Hld() {}
  42.     Hld(int _n) :Size_n(_n) {
  43.         w.resize(Size_n), gr.resize(Size_n);
  44.     }
  45.     inline void Scan_weight() {
  46.         for (int& i : w)cin >> i;
  47.     }
  48.     inline void Scan_tree() {
  49.         for (int i = 0; i < Size_n - 1; i++) {
  50.             int u, v;
  51.             cin >> u >> v;
  52.             u--, v--;
  53.             gr[u].push_back(v);
  54.             gr[v].push_back(u);
  55.         }
  56.     }
  57.  
  58.     void sizes(int v, int pr) {
  59.         p[v] = pr, siz[v] = 1;
  60.         gr[v].erase(find(all(gr[v]), pr));
  61.         for (int& u : gr[v]) {
  62.             sizes(u, v);
  63.             siz[v] += siz[u];
  64.             if (siz[u] > siz[gr[v][0]]) {
  65.                 swap(u, gr[v][0]);
  66.             }
  67.         }
  68.     }
  69.     void hld(int v) {
  70.         tin[v] = t++; euler_dfs.push_back(w[v]);
  71.         for (int& u : gr[v]) {
  72.             if (u == gr[v][0]) head[u] = head[v];
  73.             else head[u] = u;
  74.             hld(u);
  75.         }
  76.         tout[v] = t;
  77.     }
  78.     Segtree Euler;
  79.     void build() {
  80.         gr[0].push_back(0);
  81.         t = 0;
  82.         siz.resize(Size_n), tin.resize(Size_n), tout.resize(Size_n), head.resize(Size_n), p.resize(Size_n);
  83.         sizes(0, 0);
  84.         hld(0);
  85.         Euler.init(euler_dfs);
  86.     }
  87.  
  88.     inline void up(int& u, int& v, int& ans, auto& ancestor) {
  89.         while (!ancestor(head[u], v)) {
  90.             ans = max(ans, Euler.get_mx(1, 0, Euler.n - 1, tin[head[u]], tin[u]));
  91.             u = p[head[u]];
  92.         }
  93.     }
  94.  
  95.     int get_max(int u, int v) {
  96.         auto ancestor = [&](const int& u, const int& v) {
  97.             return tin[u] <= tin[v] && tout[u] > tin[v];
  98.         };
  99.         int ans = -inf;
  100.         up(u, v, ans, ancestor);
  101.         up(v, u, ans, ancestor);
  102.         if (!ancestor(u, v)) swap(u, v);
  103.         ans = max(ans, Euler.get_mx(1, 0, Euler.n - 1, tin[u], tin[v]));
  104.         return ans;
  105.     }
  106.     inline void update(const int& u, const int& new_val) {
  107.         Euler.update(1, 0, Euler.n - 1, tin[u], new_val);
  108.     }
  109. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement