QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#1261#788164#9669. Function QueryN_z_ship2077Success!2024-11-27 16:15:392024-11-27 16:15:39

详细

Extra Test:

Time Limit Exceeded

input:

300000 300000
62926051 969076521 265217892 257186360 392691429 371051018 710099993 870916511 69814757 995314625 252597191 294764151 484905077 912409182 553360241 950866958 110446159 254377488 594074779 686820878 88573767 627025734 144210369 2158463 970249960 818395974 520939182 718342823 150542372 9...

output:

192862
209905
276640
208639
98003
141880
184553
72296
276323
149413
79354
262282
68628
297018
102884
103886
76640
137544
135920
89178
56504
220911
73283
275591
157337
97437
286055
50988
90515
240065
106465
291082
156098
174605
248021
85610
8975
261930
15751
287886
171701
34437
33772
118342
126506
26...

result:


ID题目提交者结果用时内存语言文件大小提交时间测评时间
#788164#9669. Function Queryship2077TL 340ms255408kbC++231.2kb2024-11-27 16:06:132024-11-27 16:16:39

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int M=3e5+5;map<int,int>mp;
int n,q,idx,ans,x[M],tr[M*60][4],cur[M*60];
int read(){
    int x=0;char ch=getchar();
    while (!isdigit(ch)) ch=getchar();
    while (isdigit(ch)) x=x*10+ch-48,ch=getchar();
    return x;
}
void insert(int x,int y,int id){
    int p=1;
    for (int i=29;~i;i--){
        int ch=((x>>i&1)<<1)|(y>>i&1);
        if (!tr[p][ch]) tr[p][ch]=++idx;
        p=tr[p][ch];
    } cur[p]=id;
}
void query(int p,int a,int b,int tmpa,int tmpb,int d=29){
    if (ans!=-1||!p) return;
    if (d<0) return ans=cur[p],void();
    const int A=a>>d,B=b>>d;
    tmpa<<=1;tmpb<<=1;
    for (int i=0;i<2;i++)
        for (int j=0;j<2;j++)
            if ((A^(tmpa|i))<=B&&B<=(A^(tmpb|j)))
                query(tr[p][i<<1|j],a,b,tmpa|i,tmpb|j,d-1);
}
int main(){
    n=read();q=read();idx=1;
    for (int i=1;i<=n;i++) x[i]=read();
    for (int i=1;i<n;i++) insert(x[i],x[i+1],i),insert(x[i+1],x[i],i);
    for (int i=1;i<=q;i++){
        int a=read(),b=read();ans=-1;
        if (mp.count(a^b)) ans=mp[a^b]; query(1,a,b,0,0);
        printf("%d\n",ans);
    }
    return 0;
}