Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
36316 | chrisgr | 【S】T1 | C++ | 通过 | 100 | 2448 MS | 209772 KB | 2428 | 2025-02-10 20:36:02 |
#include<bits/stdc++.h> using namespace std; const int N = 1e6 + 5, inf = 2147483647; int n, q, cnt, trie[N << 5][2], up, nm[N << 5], x[N]; bool num[32]; void update(int x, int y) { if(y > 30) { nm[x] = up; // cout<<x<<" "<<nm[x]<<endl; return; } bool to = num[y]; if(!trie[x][to]) trie[x][to] = ++cnt; update(trie[x][to], y + 1); } int query(int x, int y) { // cout<<nm[x]<<" "<<x<<endl; if(y > 30) return nm[x]; bool to = num[y]; // cout << x << " " << to << " " << trie[x][0] << " " << trie[x][1] << endl; if(!trie[x][to]) return query(trie[x][!to], y + 1); else return query(trie[x][to], y + 1); } int main() { //freopen("slpjbh.in", "r", stdin); //freopen("slpjbh.out", "w", stdout); scanf("%d%d", &n, &q); for(up = 1; up <= n; ++up) { scanf("%d", x + up); int xx = x[up]; for(int i = 30; ~i; --i) { num[i] = xx & 1; xx >>= 1; } update(0, 0); } while(q--) { int a, b; scanf("%d%d", &a, &b); if(n == 1) puts("-1"); else { // // for(int i=1;i<=n;i++){ // // cout<<(x[i]^a)<<" "; // // } // // cout<<endl; int aa = a; for(int i = 30; ~i; --i) { num[i] = aa & 1; aa >>= 1; } int l = query(0, 0); aa = inf ^ a; for(int i = 30; ~i; --i) { num[i] = aa & 1; aa >>= 1; } int r = query(0, 0); //cout<<l<<" "<<r<<endl; if(l > r) swap(l, r); if((1ll * (a ^ x[l]) - b) * ((a ^ x[r]) - b) > 0) puts("-1"); else { if(!(1ll * (a ^ x[l]) - b)) printf("%d\n", (l ^ n ? l : n - 1)); else if(!(1ll * (x[r] ^ a) - b)) printf("%d\n", (r ^ n ? r : n - 1)); else { while(l + 1 < r) { int mid = l + r >> 1; // cout << l << " " << r << " " << mid << endl; if((1ll * (a ^ x[mid]) - b) * ((a ^ x[l]) - b) <= 0) r = mid; else l = mid; } printf("%d\n", l); } } } } return 0; }