mr_dot_convict

PSHTTR-codechef-mr.convict

Oct 7th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.29 KB | None | 0 0
  1. // Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows!* * * * * * */
  9. #include      <bits/stdc++.h>
  10. #include      <ext/pb_ds/assoc_container.hpp>
  11. #include      <ext/pb_ds/tree_policy.hpp>
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14.  
  15. #ifndef CONVICTION
  16. #pragma GCC       optimize ("Ofast")
  17. #pragma GCC       optimize ("unroll-loops")
  18. #pragma GCC       target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. #endif
  20.  
  21. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  22. #define PREC      cout.precision (10); cout << fixed
  23. #define x         first
  24. #define y         second
  25. #define fr(i,x,y) for (int i = x; i <= y; ++i)
  26. #define fR(i,x,y) for (int i = x; i >= y; --i)
  27. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  28. #define un(x)     sort(x.begin(), x.end()), \
  29.                   x.erase(unique(x.begin(), x.end()), x.end())
  30.  
  31. using   ll  =     long long;
  32. using   ull =     unsigned long long;
  33. using   ff  =     long double;
  34. using   pii =     pair<int,int>;
  35. using   pil =     pair<int,ll>;
  36. typedef tree
  37. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  38. ordered_set;
  39.  
  40. struct chash {
  41.    int operator () (pii x) const { return x.x*31 + x.y; }
  42. };
  43. gp_hash_table <pii, int, chash> mp;
  44.  
  45. #if __cplusplus > 201103L
  46. seed_seq seq{
  47.    (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  48.       (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  49.       (uint64_t) __builtin_ia32_rdtsc(),
  50.       (uint64_t) (uintptr_t) make_unique<char>().get()
  51. };
  52. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  53. #else
  54. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  55. mt19937 rng(seed);
  56. #endif
  57.  
  58. #define debug(args...) { \
  59.    /* WARNING : do NOT compile this debug func calls with following flags: // \
  60.     * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  61.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  62.    stringstream _ss(_s); \
  63.    istream_iterator<string> _it(_ss); err(_it, args); \
  64. }
  65. void err(istream_iterator<string> it) {
  66.    it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  67. }
  68. template<typename T, typename... Args>
  69. void err(istream_iterator<string> it, T a, Args... args) {
  70.    cerr << fixed << setprecision(15)
  71.       << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  72.    err(++it, args...);
  73. }
  74. /*****************************************************************************/
  75. //Don’t practice until you get it right. Practice until you can’t get it wrong
  76.  
  77.  
  78. signed main() {
  79.    IOS; PREC;
  80.    int tc; cin >> tc;
  81.  
  82.    while (tc--) {
  83.       int n; cin >> n;
  84.       vector <vector <pii>> T(n, vector <pii> ());
  85.  
  86.       struct Edge {
  87.          int u, v, w, idx;
  88.          bool operator < (const Edge& o) const  {
  89.             return w < o.w;
  90.          }
  91.       };
  92.       vector <Edge> edges;
  93.  
  94.       for (int e = 0; e < n - 1; ++e) {
  95.          int u, v, w;
  96.          cin >> u >> v >> w;
  97.          --u, --v;
  98.          T[u].push_back(make_pair(v, w));
  99.          T[v].push_back(make_pair(u, w));
  100.          edges.push_back({u, v, w, -1});
  101.       }
  102.  
  103.       vector <Edge> queries;
  104.       int q; cin >> q;
  105.       for (int i = 0; i < q; ++i) {
  106.          int u, v, k;
  107.          cin >> u >> v >> k;
  108.          --u, --v;
  109.          queries.push_back({u, v, k, i});
  110.       }
  111.       sort (edges.begin(), edges.end());
  112.       sort (queries.begin(), queries.end());
  113.  
  114.       vector <int> pi(n, -1), entry(n), xit(n);
  115.       vector <int> euler;
  116.  
  117.       function <void(int, int)> dfs =
  118.          [&] (int u, int pr) -> void {
  119.             pi[u] = pr;
  120.             euler.push_back(u);
  121.             entry[u] = (int)euler.size();
  122.  
  123.             for (auto vp : T[u]) {
  124.                int v = vp.x;
  125.                if (v == pr) continue;
  126.                dfs(v, u);
  127.             }
  128.             euler.push_back(u);
  129.             xit[u] = (int)euler.size();
  130.          };
  131.  
  132.       dfs(0, -1); // find euler tour
  133.  
  134.       for (Edge &e : edges) if (pi[e.u] == e.v)
  135.             swap(e.u, e.v);
  136.  
  137.       // fenwick tree on euler tour
  138.       int sz = (int)euler.size();
  139.       vector <int> BIT(sz + 2);
  140.       auto update = [&] (int x, int val) -> void {
  141.          while (x <= sz)
  142.             BIT[x] ^= val, x += (x & -x);
  143.       };
  144.       auto get_xor = [&] (int x) -> int {
  145.          int xr_val = 0;
  146.          while (x > 0)
  147.             xr_val ^= BIT[x], x -= (x & -x);
  148.          return xr_val;
  149.       };
  150.  
  151.       vector <int> answer(q);
  152.       int epr = 0;
  153.       for (auto qr : queries) {
  154.          while (epr < n - 1 && edges[epr].w <= qr.w) {
  155.             int l = entry[edges[epr].v];
  156.             int h = xit[edges[epr].v];
  157.  
  158.             update (l, edges[epr].w);
  159.             update (h + 1, edges[epr].w);
  160.             ++epr;
  161.          }
  162.  
  163.          int xru = get_xor (entry[qr.u]);
  164.          int xrv = get_xor (entry[qr.v]);
  165.          answer[qr.idx] = xru ^ xrv;
  166.       }
  167.  
  168.       for (int el : answer) cout << el << '\n';
  169.    }
  170.  
  171.  
  172.    return EXIT_SUCCESS;
  173. }
Add Comment
Please, Sign In to add comment