Advertisement
Fshl0

Untitled

Apr 15th, 2022
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define F first
  3. #define S second
  4. #define pb push_back
  5. #define mp make_pair
  6.  
  7. using namespace std;
  8. typedef long long ll;
  9. typedef pair <int, int> pii;
  10.  
  11. #include <ext/pb_ds/assoc_container.hpp>
  12. #include <ext/pb_ds/tree_policy.hpp>
  13. using namespace __gnu_pbds;
  14.  
  15. #define ordered_set tree<pair<ll,int>, null_type,less<pair<ll,int>>, rb_tree_tag,tree_order_statistics_node_update>
  16.  
  17. const int INF = 1e7;
  18. const ll longINF = 1e17;
  19. const int mxN = 1e5 + 9;
  20. const int mxM = 1e5 + 9;
  21. const int MOD = 998244353;
  22. const double pi = acos (-1);
  23.  
  24. void setIO (string s) {
  25.     freopen ((s + ".in").c_str(), "r", stdin);
  26.     freopen ((s + ".out").c_str(), "w", stdout);
  27.    
  28.     return;
  29. }
  30.  
  31. struct node {
  32.     int val = 0;
  33.     node *ln, *rn;
  34.    
  35.     node () {
  36.         ln = rn = NULL;
  37.         val = 0;
  38.     }
  39.    
  40.     node (const node* node2) {
  41.         ln = node2 -> ln;
  42.         rn = node2 -> rn;
  43.         val = node2 -> val;
  44.     }
  45.    
  46.     node (int x) {
  47.         val = x;
  48.         ln = rn = NULL;
  49.     }
  50.    
  51.     node (node* L, node* R) {
  52.         ln = L;
  53.         rn = R;
  54.         val = 0;
  55.        
  56.         if (L) val += ln -> val;
  57.         if (R) val += rn -> val;
  58.     }
  59. };
  60.  
  61. node* goCopy (node* v, int l, int r, int idx) {
  62.     if (idx < l || idx > r)
  63.         return v;
  64.    
  65.     if (l == r)
  66.         return new node ((v -> val) + 1);
  67.    
  68.     int mid = (l + r) >> 1;
  69.     if (v -> ln == NULL)
  70.         v -> ln = new node();
  71.     if (v -> rn == NULL)
  72.         v -> rn = new node();
  73.    
  74.     return new node (goCopy(v -> ln, l, mid, idx),
  75.         goCopy(v -> rn, mid + 1, r, idx));
  76. }
  77.  
  78.  
  79. node *segTree[mxN];
  80. int A[mxN], pa[mxN];
  81. vector <int> adj[mxN];
  82.  
  83. int in[mxN], out[mxN], t = 0, lol;
  84. int up[mxN][35];
  85.  
  86. void DFS (int v, int p) {
  87.     in[v] = ++t;
  88.     up[v][0] = p;
  89.     for (int i = 1; i <= lol; i++)
  90.         up[v][i] = up[up[v][i - 1]][i - 1];
  91.     for (auto u: adj[v]) {
  92.         if (u == p) continue;
  93.         DFS (u, v);
  94.     }
  95.     out[v] = ++t;
  96. }
  97.  
  98. bool chk (int u, int v) {
  99.     return (in[u] <= in[v]) && (out[u] >= out[v]);
  100. }
  101.  
  102. int lca (int u, int v) {
  103.     if (chk(u, v)) return u;
  104.     if (chk(v, u)) return v;
  105.     for (int i = lol; i >= 0; i--)
  106.         if (!chk(up[u][i], v))
  107.             u = up[u][i];
  108.     return up[u][0];
  109. }
  110.  
  111. void goDFS (int v, int p) {
  112.     segTree[v] = goCopy (segTree[p], 0, 1e9 + 1, A[v]);
  113.     pa[v] = p;
  114.    
  115.     for (auto u: adj[v]) {
  116.         if (u == p)
  117.             continue;
  118.            
  119.         goDFS (u, v);
  120.     }
  121.    
  122.     return;
  123. }
  124.  
  125. void validate (node* x) {
  126.     if (x -> ln == NULL)
  127.         x -> ln = new node();
  128.     if (x -> rn == NULL)
  129.         x -> rn = new node();
  130.    
  131.     return;
  132. }
  133.  
  134. int getQuery (node* u, node* v, node* lc, node* plc, int l, int r, int k) {
  135.     if (l == r) {
  136.         return l;
  137.     }
  138.    
  139.     validate (u);
  140.     validate (v);
  141.     validate (lc);
  142.     validate (plc);
  143.    
  144.     int mid = (l + r) >> 1;
  145.     int sumL = (u -> ln -> val) + (v -> ln -> val);
  146.     sumL -= (lc -> ln -> val);
  147.     sumL -= (plc -> ln -> val);
  148.    
  149.     if (sumL >= k) {
  150.         return getQuery (u -> ln, v -> ln, lc -> ln, plc -> ln, l, mid, k);
  151.     } else return getQuery (u -> rn, v -> rn, lc -> rn, plc -> rn, mid + 1, r, k - sumL);
  152. }
  153.  
  154. void solve () {
  155.     int n, m;
  156.     cin >> n >> m;
  157.    
  158.     for (int i = 1; i <= n; i++)
  159.         cin >> A[i];
  160.    
  161.     for (int i = 1; i <= n - 1; i++) {
  162.         int u, v;
  163.         cin >> u >> v;
  164.        
  165.         adj[u].pb (v);
  166.         adj[v].pb (u);
  167.     }
  168.    
  169.     segTree[0] = new node();
  170.     goDFS (1, 0);
  171.     DFS (1, 1);
  172.     lol = log2(n);
  173.        
  174.     while (m--) {
  175.         int u, v, k;
  176.         cin >> u >> v >> k;
  177.        
  178.         int lcA = lca (u, v);
  179.         cout << u << ' ' << v << ' ' << lca (u, v) << ' ' << lca (v, u) << "\n";
  180.         //cout << getQuery (segTree[v], segTree[v], segTree[lcA], segTree[pa[lcA]], 0, 1e9 + 1, k) << endl;
  181.     }
  182.    
  183.     return;
  184. }
  185.  
  186. int main () {
  187.     int t = 1;
  188.     //cin >> t;
  189.    
  190.     ios_base::sync_with_stdio (0);
  191.     cin.tie (0);
  192.    
  193.     //setIO ("deleg");
  194.     //preCalc ();
  195.     while (t--) {
  196.         //initialize common variables
  197.        
  198.        
  199.         //go solve
  200.         solve ();
  201.     }
  202.        
  203.     return 0;
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement