Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define PB push_back
- #define D(X) cout<<" "<<#X": "<<X<<endl;
- #define IN(n) int n; scanf("%d", &n);
- #define FOR(i, m, n) for (int i(m); i < n; i++)
- #define REP(i, n) FOR(i, 0, n)
- #define aa first
- #define bb second
- typedef long long ll;
- typedef long double ld;
- typedef pair<int,int> ii;
- typedef pair<ll,ll> pll;
- typedef vector<int> vi;
- typedef vector<ll> vll;
- typedef vector<ii> vii;
- struct Node {
- int clr, lev;
- unsigned ss[17];
- void xx() {
- REP(i, 17)
- ss[i] = 0;
- }
- };
- Node nds[123456];
- unsigned popcount(unsigned n) {
- unsigned cnt = 0;
- REP(i, 17)
- if (n & (1 << i)) cnt++;
- return cnt;
- }
- void solve() {
- IN(n);
- REP(i, n)
- nds[i].xx();
- REP(i, n)
- scanf("%d", &nds[i].clr);
- int mlev = nds[n - 1].lev + 1;
- for (int i = n - 1; i >= 0; i--) {
- int l = i*2 + 1, r = l + 1, lev = nds[i].lev;
- if (l < n)
- FOR(j, lev + 1, mlev)
- nds[i].ss[j] |= nds[l].ss[j];
- if (r < n)
- FOR(j, lev + 1, mlev)
- nds[i].ss[j] |= nds[r].ss[j];
- FOR(j, lev, mlev)
- nds[i].ss[j] |= (1 << nds[i].clr);
- }
- IN(q)
- REP(i, q) {
- IN(u) IN(k);
- u--;
- int x = nds[u].lev, ori = x, keep = u;
- while (1) {
- if (keep >= n) {
- printf("-1\n");
- break;
- }
- if (popcount(nds[u].ss[x]) >= k) {
- printf("%d\n", x - ori);
- break;
- }
- x++;
- keep = keep * 2 + 1;
- }
- }
- }
- int main() {
- ios::sync_with_stdio(0);
- IN(t)
- int thr = 0, tot = 0;
- REP(i, 123450) {
- nds[i].lev = tot;
- thr++;
- if (thr >= (1 << tot)) {
- thr = 0;
- tot++;
- }
- }
- while(t--)
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement