Advertisement
dgc9715

coci#4 D

Jan 18th, 2020
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #ifdef DGC
  6. #include "debug.h"
  7. #else
  8. #define debug(...) 9715
  9. #endif
  10. typedef long long ll;
  11. typedef long double ld;
  12. typedef complex<double> point;
  13. #define F first
  14. #define S second
  15.  
  16. template<typename T, typename R = T>
  17. struct lowest_common_ancestor
  18. {
  19.     vector<vector<pair<int, T>>> adj;
  20.     vector<vector<int>> lca;
  21.     vector<vector<R>> dist;
  22.     vector<int> lvl;
  23.  
  24.     lowest_common_ancestor(int n) : adj(n), lca(__lg(n-1)+1, vector<int>(n)), dist(__lg(n-1)+1, vector<R>(n)), lvl(n) {}
  25.  
  26.     void add_edge(int u, int v, T w)
  27.     {
  28.         adj[u].push_back({v, w});
  29.         adj[v].push_back({u, w});
  30.     }
  31.  
  32.     void build(int r = 0)
  33.     {
  34.         queue<int> q;
  35.         q.push(r);
  36.         lca[0][r] = -1;
  37.         lvl[r] = 0;
  38.  
  39.         for (int u, v; !q.empty(); )
  40.         {
  41.             u = q.front(); q.pop();
  42.             for (auto &x : adj[u])
  43.             {
  44.                 if ((v = x.F) == lca[0][u]) continue;
  45.  
  46.                 q.push(v);
  47.                 lca[0][v] = u;
  48.                 dist[0][v] = x.S;
  49.                 lvl[v] = lvl[u] + 1;
  50.  
  51.                 for (int i = 1, lg = __lg(lvl[v]); i <= lg; i++)
  52.                 {
  53.                     lca[i][v] = lca[i - 1][lca[i - 1][v]];
  54.                     dist[i][v] = dist[i - 1][lca[i - 1][v]] ^ dist[i - 1][v];
  55.                 }
  56.             }
  57.         }
  58.     }
  59.  
  60.     pair<int, R> query(int u, int v)
  61.     {
  62.         if (lvl[v] > lvl[u])
  63.             swap(u, v);
  64.  
  65.         R D = 0;
  66.         for (int i = __lg(lvl[u]); i >= 0; i--)
  67.             if (lvl[u] - (1 << i) >= lvl[v])
  68.             {
  69.                 D ^= dist[i][u];
  70.                 u = lca[i][u];
  71.             }
  72.  
  73.         if (u == v)
  74.             return {u, D}; //u;
  75.  
  76.         for (int i = __lg(lvl[u]); i >= 0; i--)
  77.             if ((1 << i) <= lvl[u] && lca[i][u] != lca[i][v])
  78.             {
  79.                 D ^= dist[i][v] ^ dist[i][u];
  80.                 u = lca[i][u];
  81.                 v = lca[i][v];
  82.             }
  83.  
  84.         D ^= dist[0][u] ^ dist[0][v];
  85.         return {lca[0][u], D}; //lca[0][u];
  86.     }
  87. };
  88.  
  89. struct que
  90. {
  91.     int a, b, t;
  92. };
  93.  
  94. const int inf = numeric_limits<int>::max();
  95.  
  96. struct node
  97. {
  98.     int x, lz;
  99.     node *ch[2];
  100.     node() { x = inf; lz = 0; ch[0]=ch[1]=NULL; }
  101. };
  102.  
  103. void push(node *u)
  104. {
  105.     if (u && u->lz)
  106.     {
  107.         if (u->lz&1) swap(u->ch[0], u->ch[1]);
  108.         u->lz >>= 1;
  109.         if (u->ch[0]) u->ch[0]->lz ^= u->lz;
  110.         if (u->ch[1]) u->ch[1]->lz ^= u->lz;
  111.         u->lz = 0;
  112.     }
  113. }
  114.  
  115. void update(node *u)
  116. {
  117.     u->x = inf;
  118.     if (u->ch[0]) u->x = min(u->x, u->ch[0]->x);
  119.     if (u->ch[1]) u->x = min(u->x, u->ch[1]->x);
  120. }
  121.  
  122. void insert(node *u, int b, int x, int t)
  123. {
  124.     push(u);
  125.     if (b < 0)
  126.     {
  127.         u->x = min(u->x, t);
  128.         return;
  129.     }
  130.     bool d = x>>b&1;
  131.     if (!u->ch[d]) u->ch[d] = new node();
  132.     insert(u->ch[d], b-1, x, t);
  133.     update(u);
  134. }
  135.  
  136. int get(node *u, int x, int t)
  137. {
  138.     int r = 0;
  139.     for (int i = 30; i >= 0; --i)
  140.     {
  141.         push(u);
  142.         bool d = x>>i&1;
  143.         if (u->ch[!d] && u->ch[!d]->x < t)
  144.         {
  145.             u = u->ch[!d];
  146.             r |= ((int)(!d)) << i;
  147.         }
  148.         else
  149.         {
  150.             u = u->ch[d];
  151.             r |= ((int)(d)) << i;
  152.         }
  153.     }
  154.     return r^x;
  155. }
  156.  
  157. void merge(node *u, node *v, int b)
  158. {
  159.     push(u); push(v);
  160.  
  161.     if (b < 0)
  162.     {
  163.         u->x = min(u->x, v->x);
  164.         return;
  165.     }
  166.  
  167.     if (v->ch[0])
  168.     {
  169.         if (!u->ch[0])
  170.             u->ch[0] = v->ch[0];
  171.         else
  172.             merge(u->ch[0], v->ch[0], b-1);
  173.     }
  174.     if (v->ch[1])
  175.     {
  176.         if (!u->ch[1])
  177.             u->ch[1] = v->ch[1];
  178.         else
  179.             merge(u->ch[1], v->ch[1], b-1);
  180.     }
  181.     update(u);
  182. }
  183.  
  184. int flip(int x)
  185. {
  186.     int r = 0;
  187.     for (int i = 30; i >= 0; --i)
  188.         r |= (x>>i&1) << (30-i);
  189.     return r;
  190. }
  191.  
  192. int main()
  193. {
  194.     #ifdef DGC
  195.         //freopen("a.in", "r", stdin);
  196.         //freopen("b.out", "w", stdout);
  197.     #endif
  198.  
  199.     ios_base::sync_with_stdio(0), cin.tie(0);
  200.  
  201.     int q;
  202.     cin >> q;
  203.     vector<int> tim(q);
  204.     vector<vector<pair<int, int>>> adj(q);
  205.     vector<vector<que>> qs(q);
  206.     int n = 1, ti = 0;
  207.     for (string s; q--; ++ti)
  208.     {
  209.         int u, v;
  210.         cin >> s >> u >> v;
  211.         if (s[0] == 'A')
  212.         {
  213.             --u;
  214.             adj[u].push_back({ n, v });
  215.             tim[n] = ti;
  216.             ++n;
  217.         }
  218.         else
  219.         {
  220.             --u, --v;
  221.             qs[v].push_back({ u, v, ti });
  222.         }
  223.     }
  224.  
  225.     lowest_common_ancestor<int> lca(n);
  226.     for (int u = 0; u < n; ++u)
  227.         for (auto v : adj[u])
  228.             lca.add_edge(u, v.F, v.S);
  229.     lca.build();
  230.  
  231.     vector<node*> roots(n);
  232.     vector<pair<int, int>> ans;
  233.     function<void(int)> dfs = [&](int u)
  234.     {
  235.         roots[u] = new node();
  236.         insert(roots[u], 30, 0, tim[u]);
  237.  
  238.         for (auto v : adj[u])
  239.         {
  240.             dfs(v.F);
  241.             roots[v.F]->lz ^= flip(v.S);
  242.             merge(roots[u], roots[v.F], 30);
  243.         }
  244.  
  245.         for (auto &k : qs[u])
  246.             ans.push_back({ k.t, get(roots[u], lca.query(k.a, k.b).S, k.t) });
  247.     };
  248.  
  249.     dfs(0);
  250.  
  251.     sort(ans.begin(), ans.end());
  252.     for (auto i : ans)
  253.         cout << i.S << "\n";
  254.  
  255.     return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement