Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
36272 | A21μΘ_wjy | 【S】T1 | C++ | 通过 | 100 | 2206 MS | 209792 KB | 1661 | 2025-02-10 16:40:12 |
#include<bits/stdc++.h> #define endl '\n' using namespace std; const int maxn=1e6+7; const int B=32; int n,q; int x[maxn]; struct Trie{ int tr[maxn*B][2]; int tot; int Tg[maxn*B]; inline void Ins(int x,int i){ int u=0; for(int i=31;i>=0;i--){ int c=(x>>i)&1; if(!tr[u][c])tr[u][c]=++tot; u=tr[u][c]; } Tg[u]=i; } inline int Maxx(int A){ int u=0; for(int i=31;i>=0;i--){ int c=(A>>i)&1; u=(tr[u][!c]?tr[u][!c]:tr[u][c]); } return Tg[u]; } inline int Minx(int A){ int u=0; for(int i=31;i>=0;i--){ int c=(A>>i)&1; u=(tr[u][c]?tr[u][c]:tr[u][!c]); } return Tg[u]; } }T; inline int F(int i,int a,int b){ return (x[i]^a)-b; } signed main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #ifndef ONLINE_JUDGE freopen("function.in","r",stdin); freopen("function.out","w",stdout); #endif cin>>n>>q; for(int i=1;i<=n;i++)cin>>x[i]; for(int i=1;i<=n;i++)T.Ins(x[i],i); while(q--){ int a,b; cin>>a>>b; if(n==1){ cout<<-1<<endl; continue; } int L=T.Minx(a),R=T.Maxx(a); if(1ll*F(L,a,b)*F(R,a,b)>0){ cout<<-1<<endl; continue; } if(L>R)swap(L,R); while(R-L>1){ int mid=L+R>>1; if(1ll*F(L,a,b)*F(mid,a,b)<=0)R=mid; else L=mid; } cout<<L<<endl; } fflush(stdout); return 0; }