Advertisement
Abrar_Al_Samit

Cowland (USACO 2019 Feb Gold)

Jul 15th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8.  
  9. #define debug(x) cerr << '[' << (#x) << "] = " << x << '\n';
  10.  
  11. template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
  12.  
  13. void PlayGround() {
  14.     int N, Q;
  15.     cin >> N >> Q;
  16.     int vals[N+1];
  17.     vector<vector<int>>edges(N+1, vector<int>());
  18.     for(int i=1; i<=N; ++i) cin >> vals[i];
  19.     for(int i=1; i<N; ++i) {
  20.         int u, v; cin >> u >> v;
  21.         edges[u].push_back(v);
  22.         edges[v].push_back(u);
  23.     }
  24.     int timer = 1, l = 20;
  25.     vector<vector<int>>up(N+1, vector<int>(l+1));
  26.     vector<int>tin(N+1), tout(N+1);
  27.  
  28.     function<void(int,int)> DFS = [&] (int node, int parent) {
  29.         tin[node] = timer++;
  30.         up[node][0] = parent;
  31.         for(int i=1; i<=l; ++i) {
  32.             up[node][i] = up[up[node][i-1]][i-1];
  33.         }
  34.        
  35.         for(auto it : edges[node]) if(it!=parent) {
  36.             DFS(it, node);
  37.         }
  38.         tout[node] = timer++;
  39.     };
  40.     DFS(1,1);
  41.  
  42.     auto isAncestor = [=] (int u, int v) {
  43.         return tin[u] <= tin[v] && tout[u] >= tout[v];
  44.     };
  45.     auto getLCA = [&] (int u, int v) {
  46.         if(isAncestor(u, v)) return u;
  47.         if(isAncestor(v, u)) return v;
  48.         for(int i=l; i>-1; --i) if(!isAncestor(up[u][i], v)) {
  49.             u = up[u][i];
  50.         }
  51.         return up[u][0];
  52.     };
  53.    
  54.     vector<int>segmentTree(N<<3), lazy(N<<3);
  55.     auto propagate = [&] (int node) {
  56.         lazy[node+node] ^= lazy[node];
  57.         lazy[node+node+1] ^= lazy[node];
  58.         segmentTree[node+node] ^= lazy[node];
  59.         segmentTree[node+node+1] ^= lazy[node];
  60.  
  61.         lazy[node] = 0;
  62.     };
  63.     function<int(int,int,int,int)> Query = [&] (int l, int r, int tar, int node) {
  64.         if(l==r) return segmentTree[node];
  65.         int mid = (l+r)>>1;
  66.         if(lazy[node]) propagate(node);
  67.         if(tar <= mid) return Query(l, mid, tar, node+node);
  68.         else return Query(mid+1, r, tar, node+node+1);
  69.     };
  70.     function<void(int,int,int,int,int,int)> Update = [&] (int l, int r, int L, int R, int val, int node) {
  71.         if(l>=L && r<=R) {
  72.             segmentTree[node] ^= val;
  73.             lazy[node] ^= val;
  74.             return;
  75.         }
  76.         if(l>R || r<L) return;
  77.         int mid = (l+r)>>1;
  78.         if(lazy[node]) propagate(node);
  79.        
  80.         Update(l, mid, L, R, val, node+node);
  81.         Update(mid+1, r, L, R, val, node+node+1);
  82.         segmentTree[node] = segmentTree[node+node] ^ segmentTree[node+node+1];
  83.     };
  84.  
  85.     for(int i=1; i<=N; ++i) {
  86.         Update(1, N+N, tin[i], tout[i], vals[i], 1);
  87.     }
  88.  
  89.     while(Q--) {
  90.         int t; cin >> t;
  91.         if(t==1) {
  92.             int i, v; cin >> i >> v;
  93.             Update(1, N+N, tin[i], tout[i], vals[i], 1);
  94.             Update(1, N+N, tin[i], tout[i], v, 1);
  95.             vals[i] = v;
  96.         } else {
  97.             int u, v; cin >> u >> v;
  98.             int lca = getLCA(u, v);
  99.             cout << (vals[lca] ^ Query(1, N+N, tin[u], 1) ^ Query(1, N+N, tin[v], 1)) << '\n';
  100.         }
  101.     }
  102.     #ifndef ONLINE_JUDGE
  103.         cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  104.     #endif
  105. }
  106. int main() {
  107.     ios_base::sync_with_stdio(0);
  108.     cin.tie(0);
  109.     cout.tie(0);
  110.     #ifndef ONLINE_JUDGE
  111.         //freopen("input.txt", "r", stdin);
  112.         freopen("cowland.in", "r", stdin);
  113.         freopen("cowland.out", "w", stdout);
  114.     #endif
  115.     PlayGround();
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement