Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2016
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define MAXN 100011
  4. // #define debug(...) do{ fprintf( stdout, __VA_ARGS__ ); } while( false )
  5. #define debug(...) do{  } while( false )
  6.  
  7. int T,N,M,A,B;
  8. int v[MAXN], vpre[MAXN], ibegin[MAXN], iend[MAXN];
  9. vector<int> adj[MAXN];
  10.  
  11. /* https://www.quora.com/How-can-you-build-a-data-structure-on-an-array-that-returns-kth-order-statistics-on-subarrays-in-logarithmic-time */
  12. struct elem {
  13.     int val, pos;
  14.     bool operator< (elem b) const {
  15.         return val<b.val;
  16.     }
  17. };
  18. int* tree[4*MAXN+10];
  19. elem temp[MAXN], arr[MAXN], sorted[MAXN];
  20. int* merge(int e, int d) {
  21.     int* num_left = (int*) malloc(sizeof(int) * (d - e + 1));
  22.     int left = e, right = (e+d)/2+1;
  23.     int i = 0, cnt = 0;
  24.     while (left <= (e+d)/2 && right <= d) {
  25.         if (arr[left].pos <= arr[right].pos) {
  26.             num_left[i] = ++cnt;
  27.             temp[i] = arr[left++];
  28.         }
  29.         else {
  30.             num_left[i] = cnt;
  31.             temp[i] = arr[right++];
  32.         }
  33.         i++;
  34.     }
  35.     while (left <= (e+d)/2) {
  36.         num_left[i] = ++cnt;
  37.         temp[i] = arr[left++];
  38.         i++;
  39.     }
  40.     while (right <= d) {
  41.         num_left[i] = cnt;
  42.         temp[i] = arr[right++];
  43.         i++;
  44.     }
  45.     for (int j = 0; j < (d-e+1); j++) {
  46.         arr[e+j]=temp[j];
  47.     }  
  48.     return num_left;        
  49. }
  50. void create_tree (int i=1, int e=1, int d=N) {
  51.     if (e == d) return;
  52.     else {
  53.         create_tree(2*i, e, (e+d)/2);
  54.         create_tree(2*i+1, (e+d)/2 + 1, d);
  55.         tree[i] = merge(e-1, d-1);
  56.     }
  57. }
  58. int query(int p, int q, int k, int i=1, int st=1, int end=N) {  
  59.     if (st == end) return sorted[st-1].val;
  60.     int left = (p!=1 ? tree[i][p-2] : 0);
  61.     int right = tree[i][q-1];
  62.     int diff = right - left;
  63.     if (diff >= k)
  64.         return query(left+1,right,k,2*i,st,st+(end-st)/2);
  65.     else
  66.         return query(p-left,q-right,k-diff,2*i+1,st+(end-st)/2+1,end);
  67. }
  68. /* ------------------------------------------------------------------------ */
  69.  
  70. int preorderCount;
  71. int dfs(int root, int pred)
  72. {
  73.     ibegin[root] = preorderCount++;
  74.     vpre[ibegin[root]] = v[root];
  75.     for (auto u : adj[root])
  76.         if (u != pred) dfs(u,root);
  77.     iend[root] = preorderCount-1;
  78. }
  79.  
  80. int main()
  81. {
  82.     ios_base::sync_with_stdio(false);
  83.     scanf("%d", &T);
  84.     while (T--)
  85.     {
  86.         scanf("%d %d", &N, &M);
  87.         for (int i = 0; i < N; i++)
  88.         {
  89.             adj[i].clear();
  90.             scanf("%d", &v[i]);
  91.         }
  92.         for (int i = 0; i < N-1; i++)
  93.         {
  94.             scanf("%d %d", &A, &B); A--; B--;
  95.             adj[A].push_back(B);
  96.             adj[B].push_back(A);
  97.         }
  98.         preorderCount = 0;
  99.         dfs(0,-1);
  100.         debug("pre: ");
  101.         for (int i = 0; i < N; i++)
  102.         {
  103.             debug("%d ", vpre[i]);
  104.             sorted[i].val = vpre[i];
  105.             sorted[i].pos = i;
  106.         }
  107.         debug("\n");
  108.         sort(sorted,sorted+N);
  109.         memcpy(arr,sorted,sizeof(sorted));
  110.         create_tree();
  111.         for (int i = 0; i < M; i++)
  112.         {
  113.             scanf("%d %d", &A, &B); A--;
  114.             int l = ibegin[A], r = iend[A];
  115.             printf("%d ", query(l+1,r+1,B));
  116.         }
  117.         printf("\n");
  118.  
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement