ohwhatalovelyday

Untitled

Oct 3rd, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define FASTER() ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  4. #define ff first
  5. #define ss second
  6. #define pb push_back
  7. #define all(a) a.begin(), a.end()
  8. #define dbg(x) cerr<<" "<<#x<<" "<<x<<endl
  9.  
  10. typedef long long ll;
  11.  
  12. using namespace std;
  13.  
  14. struct edge {
  15.     int to, id;
  16. };
  17.  
  18. struct Query {
  19.     int l, r, id;
  20. };
  21.  
  22. const int maxn = 1e5 + 13;
  23. const int k = 320;
  24. int n, t = -1;
  25. int cost[maxn];
  26. vector <pair <int, int>> lr;
  27. vector <edge> gr[maxn];
  28. vector <int> e;
  29. vector <pair <int, int>> tim;
  30. vector <Query> query;
  31.  
  32. bool cmp (const Query &a, const Query &b) {
  33.     int lf = a.l / k, ls = b.l / k;
  34.     if(lf == ls) {
  35.         if(a.r % 2 == 0) {
  36.             return a.r < b.r;
  37.         } else {
  38.             return b.r < a.r;
  39.         }
  40.     }
  41.     return lf < ls;
  42. }
  43.  
  44. void euler(int v, int p) {
  45.     tim[v].ff = t++;
  46.     for(auto to : gr[v]) {
  47.         if(to.to != p) {
  48.             e.pb(to.id);
  49.             lr[v].ff = e.size() -1;
  50.             euler(to.to, v);
  51.             e.pb(to.id);
  52.             lr[v].ss = e.size() - 1;
  53.         }
  54.     }
  55.     tim[v].ss = t++;
  56. }
  57.  
  58. inline bool isUpper(int a, int b) {
  59.     return (tim[a].ff <= tim[b].ff && tim[a].ss >= tim[b].ss);
  60. }
  61.  
  62. void prin (set <int> &st) {
  63.     int cnt = 0;
  64.     for(auto a : st) {
  65.         cout << a << ' ';
  66.         cnt++;
  67.         if(cnt == 10) break;
  68.     }
  69.     cout << endl;
  70. }
  71.  
  72. struct MO {
  73.     set <int> st;
  74.     vector <int> used;
  75.     unordered_map <int, int> cnt;
  76.     int l, r;
  77.     MO(int n) {
  78.         l = 0;
  79.         r = -1;
  80.         used = vector <int>(n + 1, 0);
  81.         for(int z = 0; z <= maxn; z++) {
  82.             st.insert(z);
  83.         }
  84.     }
  85.  
  86.     void add(int id) {
  87.         int x = cost[id];
  88.         used[id]++;
  89.         if(used[id] == 2) {
  90.             cnt[x]--;
  91.             if(cnt.count(x) == 0 || cnt[x] == 0) {
  92.                 st.insert(x);
  93.             }
  94.         } else {
  95.             cnt[x]++;
  96.             st.erase(x);
  97.         }
  98.     }
  99.  
  100.     void del(int id) {
  101.         int x = cost[id];
  102.         used[id]--;
  103.         if(used[id] == 1) {
  104.             cnt[x]++;
  105.         } else {
  106.             cnt[x]--;
  107.         }
  108.         if(used[id] == 1) {
  109.             st.erase(x);
  110.         } else if(cnt[x] == 0) {
  111.             st.insert(x);
  112.         }
  113.     }
  114.  
  115.     inline int get() {
  116.         return *st.begin();
  117.     }
  118. };
  119.  
  120. inline void addLeft(MO &goo) {
  121.     goo.l--;
  122.     goo.add(e[goo.l]);
  123. }
  124.  
  125. inline void delLeft(MO &goo) {
  126.     goo.del(e[goo.l]);
  127.     goo.l++;
  128. }
  129.  
  130. inline void addRight(MO &goo) {
  131.     goo.r++;
  132.     goo.add(e[goo.r]);
  133. }
  134.  
  135. inline void delRight(MO &goo) {
  136.     goo.del(e[goo.r]);
  137.     goo.r--;
  138. }
  139.  
  140. inline pair <int, int> getlr(int u, int v) {
  141.     if(isUpper(u, v) || isUpper(v, u)) {
  142.         int inff = tim[u].ff, inss = tim[v].ff;
  143.         int outff = tim[u].ss, outss = tim[v].ss;
  144.         if(inff > inss) swap(inff, inss);
  145.         if(outff > outss) swap(outff, outss);
  146.         if(abs(inss - inff) < abs(outss - outff)) {
  147.             return {inff, inss};
  148.         } else {
  149.             return {outff, outss};
  150.         }
  151.     }
  152.  
  153.     if(tim[v].ss < tim[u].ff) {
  154.         return {tim[v].ss, tim[u].ff};
  155.     }
  156.     return {tim[u].ss, tim[v].ff};
  157. }
  158.  
  159. int main() {
  160.     FASTER();
  161.     int q;
  162.     cin >> n >> q;
  163.     lr.assign(n + 1, {-1, -1});
  164.     tim.resize(n + 1);
  165.     for(int i = 0; i < n - 1; i++) {
  166.         int u, v, c;
  167.         cin >> u >> v >> c;
  168.         u--; v--;
  169.         gr[u].pb({v, i});
  170.         gr[v].pb({u, i});
  171.         cost[i] = c;
  172.     }
  173.  
  174.     euler(0, -1);
  175.     tim[0] = {0, e.size() - 1};
  176. //    for(auto a : e) cout << a + 1 << ' ';
  177.  
  178.     query.resize(q);
  179.     for(int i = 0; i < q; i++) {
  180.         int u, v;
  181.         cin >> u >> v;
  182.         u--; v--;
  183.         auto tmp = getlr(u, v);
  184.         int l = tmp.ff, r = tmp.ss;
  185.         query[i] = {l, r, i};
  186.     }
  187.     sort(all(query), cmp);
  188.  
  189.     MO goo(n);
  190.     vector <int> ans(q);
  191.     for(int i = 0; i < q; i++) {
  192.         auto qe = query[i];
  193.         while(goo.r < qe.r) {
  194.             addRight(goo);
  195.         }
  196.         while(goo.r > qe.r) {
  197.             while(goo.l > qe.l) {
  198.                 addLeft(goo);
  199.             }
  200.             delRight(goo);
  201.         }
  202.         while(goo.l < qe.l) {
  203.             while(goo.r < qe.r) {
  204.                 addRight(goo);
  205.             }
  206.             delLeft(goo);
  207.         }
  208.         while(goo.l > qe.l) {
  209.             addLeft(goo);
  210.         }
  211.         ans[qe.id] = goo.get();
  212.     }
  213.     for(auto a : ans) cout << a << '\n';
  214. }
  215.  
Advertisement
Add Comment
Please, Sign In to add comment