# BZOJ 2741 【FOTILE模拟赛】L(可持久化trie)

http://www.lydsy.com/JudgeOnline/problem.php?id=2741

 1 #include<algorithm>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<iostream>
6 #define ll long long
7 int n,m,a[200005],b[200005],ch[800105][2],sz,block_num,block_size,size[800105];
8 int f[205][12105],root[200005];
10     char ch=getchar();int t=0,f=1;
11     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
12     while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();}
13     return t*f;
14 }
15 void insert(int &k,int kk,int v,int dep){
16     k=++sz;
17     size[k]=size[kk]+1;
18     if (dep==-1) return;
19     ch[k][0]=ch[kk][0],ch[k][1]=ch[kk][1];
20     if (v&(1<<dep)) insert(ch[k][1],ch[kk][1],v,dep-1);
21     else insert(ch[k][0],ch[kk][0],v,dep-1);
22 }
23 int query(int x,int y,int v){
24     int res=0;
25     for (int i=30;i>=0;i--){
26         int t=((v&(1<<i))>0);
27         if (size[ch[y][t^1]]-size[ch[x][t^1]]>0){
28             res|=(1<<i);
29             y=ch[y][t^1];
30             x=ch[x][t^1];
31         }else{
32             y=ch[y][t];
33             x=ch[x][t];
34         }
35     }
36     return res;
37 }
38 int main(){
39     scanf("%d%d",&n,&m);
40     block_size=(int)sqrt(n);
41     block_num=n/block_size+(n%block_size!=0);
42     for (int i=1;i<=n;i++) {scanf("%d",&a[i]);a[i]^=a[i-1];}
43     int ans=0;
44     for (int i=1;i<=n;i++) insert(root[i],root[i-1],a[i],30);
45     for (int i=1;i<=block_num;i++)
46      for (int j=(i-1)*block_size+1;j<=n;j++){
47             f[i][j]=std::max(f[i][j-1],query(root[(i-1)*block_size],root[j],a[j]));
48             if (i==1) f[i][j]=std::max(f[i][j],a[j]);
49      }
50     while (m--){
51         int x,y;
52         scanf("%d%d",&x,&y);
53         x%=n;y%=n;
54         x=(x+(ans%n))%n+1;
55         y=(y+(ans%n))%n+1;
56         if (x>y) std::swap(x,y);
57         x--;
58         int num=x/block_size+(x%block_size!=0);
59         ans=0;
60         int l=num*block_size+1;
61         if (l<=y) ans=f[num+1][y];
62         l=std::min(l,y);
63         for (int j=x;j<l;j++)
64          ans=std::max(ans,query(root[x],root[y],a[j]));
65         printf("%d\n",ans);
66     }
67 }

posted @ 2016-06-14 17:14  GFY  阅读(148)  评论(0编辑  收藏  举报