Advertisement
Abrar_Al_Samit

Pishty And Tree (CodeChef)

Jul 21st, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 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. const int maxn = 1e5 + 5;
  14. vector<int>g[maxn];
  15. void PlayGround() {
  16.     int N;
  17.     cin >> N;
  18.     vector<pair<int,pair<int,int>>>edges(N-1);
  19.     for(auto& it : edges) {
  20.         cin >> it.second.first >> it.second.second >> it.first;
  21.         g[it.second.first].push_back(it.second.second);
  22.         g[it.second.second].push_back(it.second.first);
  23.     }
  24.     sort(edges.begin(), edges.end());
  25.     vector<int>tin(N+1), tout(N+1);
  26.     int timer = 1;
  27.     function<void(int,int)> DFS = [&] (int node, int parent) {
  28.         tin[node] = timer++;
  29.         for(auto it : g[node]) if(it != parent) {
  30.             DFS(it, node);
  31.         }
  32.         tout[node] = timer++;
  33.     };
  34.     DFS(1, 1);
  35.     auto isAncestor = [&] (int u, int v) {
  36.         return tin[u] <= tin[v] && tout[u] >= tout[v];
  37.     };
  38.     for(auto& it : edges) {
  39.         if(!isAncestor(it.second.first, it.second.second))
  40.             swap(it.second.first, it.second.second);
  41.     }
  42.     struct Qu{
  43.         int u, v, k, idx;  
  44.         bool operator< (Qu o) const {
  45.             return make_tuple(k, u, v, idx) < make_tuple(o.k, o.u, o.v, o.idx);
  46.         };
  47.     };
  48.     int M;
  49.     cin >> M;
  50.     vector<Qu>ask(M);
  51.  
  52.     vector<int>segmentTree(N<<3), lazy(N<<3);
  53.     auto propagate = [&] (int node) {
  54.         segmentTree[node+node] ^= lazy[node];
  55.         segmentTree[node+node+1] ^= lazy[node];
  56.         lazy[node+node] ^= lazy[node];
  57.         lazy[node+node+1] ^= lazy[node];
  58.         lazy[node] = 0;
  59.     };
  60.     function<void(int,int,int,int,int,int)> Update = [&] (int l, int r, int L, int R, int val, int node) {
  61.         if(l>=L && r<=R) {
  62.             segmentTree[node] ^= val;
  63.             lazy[node] ^= val;
  64.             return;
  65.         }
  66.         if(l>R || r<L) return;
  67.         int mid = (l+r)>>1;
  68.         if(lazy[node]) propagate(node);
  69.         Update(l, mid, L, R, val, node+node);
  70.         Update(mid+1, r, L, R, val, node+node+1);
  71.         segmentTree[node] = segmentTree[node+node] ^ segmentTree[node+node+1];
  72.     };
  73.     function<int(int,int,int,int)> Query = [&] (int l, int r, int tar, int node) {
  74.         if(l==r) {
  75.             return segmentTree[node];
  76.         }
  77.         int mid = (l+r)>>1;
  78.         if(lazy[node]) propagate(node);
  79.         if(tar <= mid) return Query(l, mid, tar, node+node);
  80.         else return Query(mid+1, r, tar, node+node+1);
  81.     };
  82.  
  83.     for(int i=0; i<M; ++i) {
  84.         int u, v, k; cin >> u >> v >> k;
  85.         Qu q = {u, v, k, i};
  86.         ask[i] = q;
  87.     }
  88.     sort(ask.begin(), ask.end());
  89.     vector<int>answers(M);
  90.     auto it = edges.begin();
  91.     for(int i=0; i<M; ++i) {
  92.         int val = ask[i].k;
  93.         auto upto = upper_bound(edges.begin(), edges.end(), make_pair(val, make_pair(INT_MAX, INT_MAX)));
  94.         while(it != upto) {
  95.             Update(1, N+N, tin[it->second.second], tout[it->second.second], it->first, 1);
  96.             ++it;
  97.         }
  98.         answers[ask[i].idx] = (Query(1, N+N, tin[ask[i].u], 1) ^ Query(1, N+N, tin[ask[i].v], 1));
  99.  
  100.     }
  101.     for(int it : answers) cout << it << ' ';
  102.     for(int i=1; i<=N; ++i) g[i].clear();
  103.     cout << '\n';
  104.     #ifndef ONLINE_JUDGE
  105.         cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  106.     #endif
  107. }
  108. int main() {
  109.     ios_base::sync_with_stdio(0);
  110.     cin.tie(0);
  111.     cout.tie(0);
  112.     #ifndef ONLINE_JUDGE
  113.         freopen("input.txt", "r", stdin);
  114.     #endif
  115.     int T;
  116.     cin >> T;
  117.     while(T--)
  118.     PlayGround();
  119.  
  120.     return 0;
  121. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement