Advertisement
cosenza987

turbinado

Sep 1st, 2022
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. //Слава Україні, Героям слава
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. using ll = int;
  8.  
  9. const int MAX = 3e5 + 7;
  10.  
  11. namespace seg {
  12.     ll seg[4 * MAX], lazy[4 * MAX];
  13.     int n, * v;
  14.  
  15.     ll build(int p = 1, int l = 0, int r = n - 1) {
  16.         lazy[p] = 0;
  17.         if (l == r) return seg[p] = v[l];
  18.         int m = (l + r) / 2;
  19.         return seg[p] = min(build(2 * p, l, m), build(2 * p + 1, m + 1, r));
  20.     }
  21.     void build(int n2, int* v2) {
  22.         n = n2, v = v2;
  23.         build();
  24.     }
  25.     void prop(int p, int l, int r) {
  26.         if (!lazy[p]) {
  27.             return;
  28.         }
  29.         seg[p] = lazy[p];
  30.         if (l != r) lazy[2 * p] = min(lazy[2 * p], lazy[p]), lazy[2 * p + 1] = min(lazy[p], lazy[2 * p + 1]);
  31.         lazy[p] = 0;
  32.     }
  33.     ll query(int a, int b, int p = 1, int l = 0, int r = n - 1) {
  34.         prop(p, l, r);
  35.         if (a <= l and r <= b) return seg[p];
  36.         if (b < l or r < a) return INT_MAX;
  37.         int m = (l + r) / 2;
  38.         return min(query(a, b, 2 * p, l, m), query(a, b, 2 * p + 1, m + 1, r));
  39.     }
  40.     ll update(int a, int b, int x, int p = 1, int l = 0, int r = n - 1) {
  41.         prop(p, l, r);
  42.         if (a <= l and r <= b) {
  43.             lazy[p] = max(lazy[p], (ll)x);
  44.             prop(p, l, r);
  45.             return seg[p];
  46.         }
  47.         if (b < l or r < a) return seg[p];
  48.         int m = (l + r) / 2;
  49.         return seg[p] = min(update(a, b, x, 2 * p, l, m), update(a, b, x, 2 * p + 1, m + 1, r));
  50.     }
  51. };
  52.  
  53. namespace hld {
  54.     vector<pair<int, int> > g[MAX];
  55.     int pos[MAX], sz[MAX];
  56.     int sobe[MAX], pai[MAX];
  57.     int h[MAX], v[MAX], t;
  58.  
  59.     void build_hld(int k, int p = -1, int f = 1) {
  60.         v[pos[k] = t++] = sobe[k]; sz[k] = 1;
  61.         for (auto& i : g[k]) if (i.first != p) {
  62.             int u = i.first;
  63.             int w = i.second;
  64.             sobe[u] = w; pai[u] = k;
  65.             h[u] = (i == g[k][0] ? h[k] : u);
  66.             build_hld(u, k, f); sz[k] += sz[u];
  67.  
  68.             if (sz[u] > sz[g[k][0].first] or g[k][0].first == p)
  69.                 swap(i, g[k][0]);
  70.         }
  71.         if (p * f == -1) build_hld(h[k] = k, -1, t = 0);
  72.     }
  73.     void build(int root = 0) {
  74.         t = 0;
  75.         build_hld(root);
  76.         seg::build(t, v);
  77.     }
  78.     ll query_path(int a, int b) {
  79.         if (a == b) return INT_MAX;
  80.         if (pos[a] < pos[b]) swap(a, b);
  81.  
  82.         if (h[a] == h[b]) return seg::query(pos[b] + 1, pos[a]);
  83.         return min(seg::query(pos[h[a]], pos[a]), query_path(pai[h[a]], b));
  84.     }
  85.     void update_path(int a, int b, int x) {
  86.         if (a == b) return;
  87.         if (pos[a] < pos[b]) swap(a, b);
  88.  
  89.         if (h[a] == h[b]) return (void)seg::update(pos[b] + 1, pos[a], x);
  90.         seg::update(pos[h[a]], pos[a], x); update_path(pai[h[a]], b, x);
  91.     }
  92.     ll query_subtree(int a) {
  93.         if (sz[a] == 1) return 0;
  94.         return seg::query(pos[a] + 1, pos[a] + sz[a] - 1);
  95.     }
  96.     void update_subtree(int a, int x) {
  97.         if (sz[a] == 1) return;
  98.         seg::update(pos[a] + 1, pos[a] + sz[a] - 1, x);
  99.     }
  100.     int lca(int a, int b) {
  101.         if (pos[a] < pos[b]) swap(a, b);
  102.         return h[a] == h[b] ? b : lca(pai[h[a]], b);
  103.     }
  104. };
  105.  
  106. int main() {
  107.     ios_base::sync_with_stdio(false);
  108.     cin.tie(0);
  109.     int n, m;
  110.     cin >> n >> m;
  111.     while(m--) {
  112.         int a, b, c;
  113.         cin >> a >> b >> c;
  114.         hld::g[a].push_back({b, c});
  115.         hld::g[b].push_back({a, c});
  116.     }
  117.     hld::build(1);
  118.     int q;
  119.     cin >> q;
  120.     while(q--) {
  121.         int a, b;
  122.         cin >> a >> b;
  123.         cout << hld::query_path(a, b) << "\n";
  124.     }
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement