Advertisement
Morass

Joker

Sep 4th, 2016
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define PB push_back
  5. #define D(X) cout<<"  "<<#X": "<<X<<endl;
  6. #define IN(n) int n; scanf("%d", &n);
  7. #define FOR(i, m, n) for (int i(m); i < n; i++)
  8. #define REP(i, n) FOR(i, 0, n)
  9. #define aa first
  10. #define bb second
  11.  
  12. typedef long long ll;
  13. typedef long double ld;
  14. typedef pair<int,int> ii;
  15. typedef pair<ll,ll> pll;
  16. typedef vector<int> vi;
  17. typedef vector<ll> vll;
  18. typedef vector<ii> vii;
  19.  
  20. struct Node {
  21.     int clr, lev;
  22.     unsigned ss[17];
  23.     void xx() {
  24.         REP(i, 17)
  25.             ss[i] = 0;
  26.     }
  27. };
  28. Node nds[123456];
  29.  
  30. unsigned popcount(unsigned n) {
  31.     unsigned cnt = 0;
  32.     REP(i, 17)
  33.         if (n & (1 << i)) cnt++;
  34.     return cnt;
  35. }
  36.  
  37. void solve() {
  38.     IN(n);
  39.     REP(i, n)
  40.         nds[i].xx();
  41.     REP(i, n)
  42.         scanf("%d", &nds[i].clr);
  43.     int mlev = nds[n - 1].lev + 1;
  44.     for (int i = n - 1; i >= 0; i--) {
  45.         int l = i*2 + 1, r = l + 1, lev = nds[i].lev;
  46.         if (l < n)
  47.             FOR(j, lev + 1, mlev)
  48.                 nds[i].ss[j] |= nds[l].ss[j];
  49.         if (r < n)
  50.             FOR(j, lev + 1, mlev)
  51.                 nds[i].ss[j] |= nds[r].ss[j];
  52.         FOR(j, lev, mlev)
  53.             nds[i].ss[j] |= (1 << nds[i].clr);
  54.     }
  55.     IN(q)
  56.     REP(i, q) {
  57.         IN(u) IN(k);
  58.         u--;
  59.         int x = nds[u].lev, ori = x, keep = u;
  60.         while (1) {
  61.             if (keep >= n) {
  62.                 printf("-1\n");
  63.                 break;
  64.             }
  65.             if (popcount(nds[u].ss[x]) >= k) {
  66.                 printf("%d\n", x - ori);
  67.                 break;
  68.             }
  69.             x++;
  70.             keep = keep * 2 + 1;
  71.         }
  72.     }
  73. }
  74.  
  75. int main() {
  76.     ios::sync_with_stdio(0);
  77.     IN(t)
  78.     int thr = 0, tot = 0;
  79.     REP(i, 123450) {
  80.         nds[i].lev = tot;
  81.         thr++;
  82.         if (thr >= (1 << tot)) {
  83.             thr = 0;
  84.             tot++;
  85.         }
  86.     }
  87.     while(t--)
  88.         solve();
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement