Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
36321 | Mengmohan | 【S】T1 | C++ | 运行出错 | 41 | 70 MS | 12320 KB | 2200 | 2025-02-10 20:55:49 |
#include<bits/stdc++.h> using namespace std; const int N=1e6+10,H=30; int n,q,x[N],ch[N][2],tot; map<int,int> mp; vector<int> bt; int mknode(){ ++tot; ch[tot][1]=ch[tot][0]=0; return tot; } void add(int x){ bt.clear(); for(int i=1;i<=H;i++){ bt.push_back(x&1); x>>=1; } int p=1; for(int i=bt.size()-1;~i;--i){ if(!ch[p][bt[i]]) ch[p][bt[i]]=mknode(); p=ch[p][bt[i]]; } } int Max(int x) { int ans=0,p=1; bt.clear(); for(int i=1;i<=H;i++){ bt.push_back(x&1); x>>=1; } /*for(auto v=bt.rbegin();it!=bt.rend();++it){ }*/ for(int i=bt.size()-1;~i;--i){ if(ch[p][1-bt[i]]){ ans<<=1; ans|=1-bt[i]; p=ch[p][1-bt[i]]; } else{ ans<<=1; ans|=bt[i]; p=ch[p][bt[i]]; } } return ans; } int Min(int x){ int ans=0,p=1; bt.clear(); for(int i=1;i<=H;i++){ bt.push_back(x&1); x>>=1; } for(int i=bt.size()-1;~i;--i){ if(ch[p][bt[i]]){ ans<<=1; ans|=bt[i]; p=ch[p][bt[i]]; } else{ ans<<=1; ans|=1-bt[i]; p=ch[p][1-bt[i]]; } } return ans; } bool f(int u,int v,int a,int b){ return (1ll*((u^a)-b)*((v^a)-b))<=0ll; } int main(){ scanf("%d%d",&n,&q); int rt=mknode(); for(int i=1;i<=n;i++){ scanf("%d",&x[i]); mp[x[i]]=i; add(x[i]); } while(q--){ int a,b,flag=0; scanf("%d%d",&a,&b); if(n==1){ puts("-1"); continue; } int l=mp[Min(a)],r=mp[Max(a)],mid=0; if(l>r) swap(l,r); for(int i=1;i<1e9;i++) ; if(!f(x[l],x[r],a,b)) puts("-1"); else{ while(r-l>1){ mid=(l+r+1)>>1; if(f(x[l],x[mid],a,b)) r=mid; else l=mid; } if(f(x[l],x[l+1],a,b)) printf("%d\n",l); else printf("%d\n",r); } } return 0; }