Advertisement
ivnikkk

Untitled

Sep 22nd, 2022 (edited)
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "bits/stdc++.h"
  3. using namespace std;
  4. #define all(a) a.begin(), a.end()
  5. #define int long long
  6. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  7. typedef long double ld;
  8. const int c = 330;
  9. const int N = 1e5 + 1;
  10. const int LG = 20;
  11. int up[LG][N] = {}, d[N] = {};
  12.  
  13. struct Set_sqrt {
  14.     int n;
  15.     vector<int> cnt, st, sq;
  16.     Set_sqrt(int _n) : n(_n) {
  17.         cnt.resize(_n + 1); st.resize(_n + 1); sq.resize(c + 1);
  18.     }
  19.     void insert(int x) {
  20.         if (x > n) return;
  21.         cnt[x]++;
  22.         if (cnt[x] == 1) {
  23.             sq[x / c]++;
  24.             st[x] = 1;
  25.         }
  26.     }
  27.     void erase(int x) {
  28.         if (x > n) return;
  29.         cnt[x]--;
  30.         if (cnt[x] == 0) {
  31.             sq[x / c]--;
  32.             st[x] = 0;
  33.         }
  34.     }
  35.     int get_mex() {
  36.         int it = 0;
  37.         while (sq[it] == c) { it++; }
  38.         int res = it * c;
  39.         while (st[res] == 1) { res++; }
  40.         return res;
  41.     }
  42. };
  43.  
  44. struct Edge {
  45.     int u, w, ind;
  46.     Edge(int _u, int _w, int _i) : u(_u), w(_w), ind(_i) {}
  47. };
  48.  
  49. struct Query {
  50.     int l, r, ind;
  51.     Query(int _l, int _r, int _i) : l(_l), r(_r), ind(_i) {};
  52.     bool operator<(const Query& other) {
  53.         if (l / c == other.l / c) {
  54.             if ((l / c) & 1) {
  55.                 return r > other.r;
  56.             }
  57.             return r < other.r;
  58.         }
  59.         return l < other.l;
  60.     }
  61. };
  62.  
  63.  
  64. int t = 0;
  65.  
  66. vector<pair<int, int>> euler;
  67. vector<int> tin, tout;
  68. vector<vector<Edge>> gr;
  69. vector<int> ed;
  70.  
  71.  
  72. void dfs(int v, int p) {
  73.     for (int lvl = 1; lvl < LG; lvl++) {
  74.         up[lvl][v] = up[lvl - 1][up[lvl - 1][v]];
  75.     }
  76.     for (auto&& [u, w, ind] : gr[v]) {
  77.         if (u != p) {
  78.             d[u] = d[v] + 1;
  79.             up[0][u] = v;
  80.             euler.push_back({ w, ind });
  81.             tin[ind] = t++;
  82.             ed[u] = ind;
  83.             dfs(u, v);
  84.             euler.push_back({ w, ind });
  85.             tout[ind] = t++;
  86.         }
  87.     }
  88. }
  89.  
  90. int lca(int a, int b) {
  91.     for (int i = LG - 1; i >= 0; i--) {
  92.         if (d[up[i][a]] > d[b]) {
  93.             a = up[i][a];
  94.         }
  95.     }
  96.     if (up[0][a] == b) { return a; }
  97.     a = up[0][a];
  98.     for (int i = LG - 1; i >= 0; i--) {
  99.         if (up[i][a] != up[i][b]) {
  100.             a = up[i][a]; b = up[i][b];
  101.         }
  102.     }
  103.     return up[0][a];
  104. }
  105.  
  106. signed main() {
  107. #ifdef _DEBUG
  108.     freopen("input.txt", "r", stdin);
  109.     freopen("output.txt", "w", stdout);
  110. #endif
  111.     ios_base::sync_with_stdio(false);
  112.     cin.tie(nullptr);
  113.     cout.tie(nullptr);
  114.     int n, m; cin >> n >> m;
  115.     gr.resize(n), tin.resize(n); tout.resize(n); ed.resize(n);
  116.     for (int i = 0; i < n - 1; i++) {
  117.         int u, v, w;
  118.         cin >> u >> v >> w;
  119.         u--, v--;
  120.         gr[u].push_back(Edge(v, w, i));
  121.         gr[v].push_back(Edge(u, w, i));
  122.     }
  123.  
  124.     d[0] = up[0][0] = 0;
  125.     dfs(0, 0);
  126.  
  127.     vector<Query> q;
  128.  
  129.     auto get_query = [&](int u, int v, int& l, int& r) {
  130.         if (d[u] > d[v]) {
  131.             swap(u, v);
  132.         }
  133.         int lc = lca(v, u);
  134.         if (up[0][lc] == u) {
  135.             u = ed[lc];
  136.             v = ed[v];
  137.         }
  138.         else {
  139.             u = ed[u];
  140.             v = ed[v];
  141.         }
  142.         if (tin[u] > tin[v]) {
  143.             swap(u, v);
  144.         }
  145.         if (tout[u] < tin[v]) l = tout[u], r = tin[v];
  146.         else l = tin[u], r = tin[v];
  147.     };
  148.  
  149.     for (int i = 0; i < m; i++) {
  150.         int u, v;
  151.         cin >> u >> v;
  152.         u--, v--;
  153.         int l, r; get_query(u, v, l, r);
  154.         q.push_back(Query(l, r, i));
  155.     }
  156.  
  157.     vector<int> ans(m), count_in(n, 0);
  158.     Set_sqrt U(n);
  159.     sort(all(q));
  160.  
  161.     auto recalc = [&](pair<int, int>& x) {
  162.         if (!count_in[x.second]) { U.insert(x.first); }
  163.         else { U.erase(x.first); }
  164.         count_in[x.second] ^= 1;
  165.     };
  166.  
  167.     int l = 0, r = -1;
  168.     for (const Query& i : q) {
  169.         while (l > i.l) { recalc(euler[--l]); }
  170.         while (r < i.r) { recalc(euler[++r]); }
  171.         while (l < i.l) { recalc(euler[l++]); }
  172.         while (r > i.r) { recalc(euler[r--]); }
  173.         ans[i.ind] = U.get_mex();
  174.     }
  175.  
  176.     for (int i = 0; i < m; i++) {
  177.         cout << ans[i] << '\n';
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement