Beingamanforever

DISTQUERY SPOJ, Euler tour + segtree

Jan 8th, 2025
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  4. #define NeedForSpeed                  \
  5.     ios_base::sync_with_stdio(false); \
  6.     cin.tie(NULL);                    \
  7.     cout.tie(NULL);
  8. #define int long long
  9. #define all(x) (x).begin(), (x).end()
  10. typedef vector<int> vi;
  11. typedef vector<bool> vb;
  12. typedef vector<vi> vvi;
  13. typedef vector<pair<int, int>> vpi;
  14. typedef pair<int, int> pi;
  15. #define f first
  16. #define s second
  17. #define pb push_back
  18. #define pf push_front
  19. #define ppb pop_back
  20. #define mp make_pair
  21. #define sz(x) ((int)(x).size())
  22. #define set_bits __builtin_popcountll
  23. #define yes cout << "YES" << endl
  24. #define no cout << "NO" << endl
  25. #define endl "\n"
  26. #define M_PI 3.14159265358979323846264338327950288
  27. const int mod1 = 1e9 + 7;
  28. const int mod2 = 998244353;
  29. const long long INF = 2e18;
  30. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  31. const int N = 1e5 + 5;
  32. const int L = 20;
  33. #ifndef ONLINE_JUDGE
  34. #include "heWhoCooks.cpp"
  35. #else
  36. #define debug(...)
  37. #define debugArr(...)
  38. #define __START__
  39. #define __END__
  40. #endif
  41. struct segtree
  42. {
  43.     typedef pair<int, int> T;
  44.     T def = {INF, -INF}, t[2 * N];
  45.     // min and max pair
  46.     T f(T a, T b) { return {min(a.f, b.f), max(a.s, b.s)}; }
  47.     void build(const vector<int> &v)
  48.     {
  49.         int n = v.size();
  50.         for (int i = 0; i < n; ++i)
  51.         {
  52.             t[i + N] = {v[i], v[i]};
  53.         }
  54.         for (int i = N - 1; i > 0; --i)
  55.         {
  56.             t[i] = f(t[2 * i], t[2 * i + 1]);
  57.         }
  58.     }
  59.     void modify(int p, T v)
  60.     {
  61.         for (t[p += N] = v; p /= 2;)
  62.         {
  63.             t[p] = f(t[2 * p], t[2 * p + 1]);
  64.         }
  65.     }
  66.     T query(int l, int r)
  67.     {
  68.         T resl = def, resr = def;
  69.         for (l += N, r += N; l < r; l /= 2, r /= 2)
  70.         {
  71.             if (l & 1)
  72.             {
  73.                 resl = f(resl, t[l++]);
  74.             }
  75.             if (r & 1)
  76.             {
  77.                 resr = f(t[--r], resr);
  78.             }
  79.         }
  80.         return f(resl, resr);
  81.     }
  82. };
  83. vpi adj[N];
  84. void solve()
  85. {
  86.     int n;
  87.     cin >> n;
  88.     for (int i = 1; i <= (n - 1); i++)
  89.     {
  90.         int u, v, w;
  91.         cin >> u >> v >> w;
  92.         adj[u].pb({v, w});
  93.         adj[v].pb({u, w});
  94.     }
  95.     vi in(n + 1), out(n + 1), euler, cost;
  96.     function<void(int, int)> dfs = [&](int u, int p)
  97.     {
  98.         in[u] = sz(euler);
  99.         for (auto &[v, w] : adj[u])
  100.         {
  101.             if (v == p)
  102.             {
  103.                 continue;
  104.             }
  105.             cost.pb(w);
  106.             euler.pb(v);
  107.             dfs(v, u);
  108.             cost.pb(w);
  109.             euler.pb(u);
  110.         }
  111.         out[u] = sz(euler);
  112.     };
  113.     dfs(1, 0);
  114.     debug(euler);
  115.     debug(cost);
  116.     segtree st;
  117.     st.build(cost);
  118.     int q;
  119.     cin >> q;
  120.     while (q--)
  121.     {
  122.         int u, v;
  123.         cin >> u >> v;
  124.         int l = min(in[u], in[v]), r = max(in[u], in[v]);
  125.         auto ans = st.query(l, r);
  126.         cout << ans.f << " " << ans.s << endl;
  127.     }
  128. }
  129.  
  130. signed main()
  131. {
  132.     __START__;
  133.     NeedForSpeed;
  134.     int t = 1;
  135.     while (t--)
  136.     {
  137.         solve();
  138.     }
  139.     __END__;
  140.     return 0;
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment