题意:
给一个数列,然后m个询问,每次询问一个区间[l,r]内异或值最大的数对,输出它们的异或值。
做法:
分块+可持久化trie。
我们可以在log复杂度内求出,一个区间内任意一个数和一个定值的最大异或值。
可以用可持久化trie维护。
但是现在两个数都是不定的。
考虑分块,f[i][j]表示第i个块左端点到j这段区间的数对的最大异或值。
则递推式为f[i][j] = max(f[i][j-1], query(rt[L[i]-1], rt[j], a[j]));L[i]表示第i块的左端点。
query(rt[L[i]-1], rt[j], a[j])就表示在L[i]到j区间内找到一个数,使得这个数和a[j]的异或值最大。
对于每一个查询,首先找到x所在块的右边那一块pos[x]+1,将ans更新为f[pos[x]+1][r],
然后对于x到端点再暴力地更新答案就好了。
代码:
/*************************************************************
Problem: bzoj 2741 【FOTILE模拟赛】L
User: fengyuan
Language: C++
Result: Accepted
Time: 10532 ms
Memory: 44596 kb
Submit_Time: 2017-12-28 15:51:12
*************************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 20010, M = 400, T = 1000010;
int n, m, cnt, len, tot;
int a[N], f[M][N], L[N], pos[N];
int ch[T][2], rt[N], sum[T];
inline void insert(int lst, int &k, int x, int d)
{
if(!k) k = ++ tot; sum[k] = sum[lst] + 1;
if(d < 0) return;
int p = x>>d&1; ch[k][p^1] = ch[lst][p^1];
insert(ch[lst][p], ch[k][p], x, d-1);
}
inline int query(int lst, int k, int x, int d)
{
if(d < 0) return 0;
int p = x>>d&1;
if(sum[ch[k][p^1]] - sum[ch[lst][p^1]] > 0) return (1<<d) + query(ch[lst][p^1], ch[k][p^1], x, d-1);
else return query(ch[lst][p], ch[k][p], x, d-1);
}
int main()
{
scanf("%d%d", &n, &m); len = cnt = sqrt(n);
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
for(int i = 2; i <= n; i ++) a[i] ^= a[i-1];
if(len*len < n) { len ++; cnt = (n+len-1)/len; }
for(int i = 1; i <= n; i ++) {
pos[i] = (i+len-1)/len;
if(!L[pos[i]]) L[pos[i]] = i;
} L[cnt+1] = n+1;//分块预处理
for(int i = 1; i <= n; i ++) insert(rt[i-1], rt[i], a[i], 30);//插入trie
for(int i = 1; i <= cnt; i ++)//f[i][j]表示第i个块的左端点到j,区间内两个数最大异或和
for(int j = L[i]; j <= n; j ++) f[i][j] = max(f[i][j-1], query(rt[L[i]-1], rt[j], a[j], 30));
int lastans = 0;
while(m --) {
int x, y, l, r; scanf("%d%d", &x, &y);
x = ((LL)x+lastans)%n+1; y = ((LL)y+lastans)%n+1;
if(x > y) swap(x, y); x --;
if(pos[x] == pos[y]) {//在同一个块里,直接暴力
lastans = 0;
for(int i = x; i <= y; i ++) lastans = max(lastans, query(rt[x-1], rt[y], a[i], 30));
printf("%d\n", lastans); continue;
}
lastans = f[pos[x]+1][y];//否则直接调用f[pos[x]+1][y]表示x右边第一个块到y的最大数对异或和
for(int i = x; i < L[pos[x]+1]; i ++) lastans = max(lastans, query(rt[x-1], rt[y], a[i], 30));//对于x到x右边第一个块的左端点再暴力更新
printf("%d\n", lastans);
}
return 0;
}