您的位置:首页 > 其它

bzoj2741[FOTILE模拟赛L]

2017-07-26 21:43 302 查看
Description

FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和。

即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 … xor Aj),其中l<=i<=j<=r。

为了体现在线操作,对于一个询问(x,y):

l = min ( ((x+lastans) mod N)+1 , ((y+lastans) mod N)+1 ).

r = max ( ((x+lastans) mod N)+1 , ((y+lastans) mod N)+1 ).

其中lastans是上次询问的答案,一开始为0。

Input

第一行两个整数N和M。

第二行有N个正整数,其中第i个数为Ai,有多余空格。

后M行每行两个数x,y表示一对询问。

Output

共M行,第i行一个正整数表示第i个询问的结果。

Sample Input

3 3

1 4 3

0 1

0 1

4 3

Sample Output

5

7

7

HINT

HINT

N=12000,M=6000,x,y,Ai在signed longint范围内。

Source

By seter

solution:可持久化trie+分块+贪心

/**************************************************************
Problem: 2741
User: Venishel
Language: C++
Result: Accepted
Time:7412 ms
Memory:67744 kb
****************************************************************/

#include <cmath>
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define LL long long
#define N 15000
#define Name "xor"
struct Trie{
int son[2], w;
}trie[N*40];

int n, m, blk, tot, sum
;
int root[N], bl[N], a[N], f[1010][N];

void insert( int pre, int &root_r, int d, int step){
trie[root_r=++tot]=trie[pre];
trie[root_r].w++;
if ( step<0 ) return;
int p=(d>>step)&1;
insert( trie[pre].son[p], trie[root_r].son[p], d, step-1);
return ;
}
LL query( int d, int pre, int root_r, int step ){
if ( step<0 ) return 0;
int p=(d>>step)&1;
if ( trie[trie[root_r].son[p^1]].w - trie[trie[pre].son[p^1]].w )
return (1<<step)+query( d, trie[pre].son[p^1], trie[root_r].son[p^1], step-1 );
return query( d, trie[pre].son[p], trie[root_r].son[p], step-1 );
}
void init(){
blk=sqrt(n); tot=0;
for ( int i=1; i<=n; i++ ) bl[i]=(i-1)/blk+1, scanf( "%d",&a[i]);
for ( int i=1; i<=n; i++ ) sum[i]=sum[i-1]^a[i];
for ( int i=1; i<=n; i++ ) insert( root[i-1], root[i], sum[i], 30);
for ( int i=1; i<=bl
; i++ ){
LL ans=0;
int l=(i-1)*blk+1;
for(int j=l; j<=n; j++ ){
ans=max( ans, query(sum[j], root[l-2], root[j], 30));
f[i][j]=ans;
}
}
}
int main(){

scanf( "%d%d", &n, &m );
LL ans=0; init();
for ( int i=1; i<=m; i++ ){
int l, r;
scanf( "%d%d", &l, &r );
l= (l+ans)%n+1, r=(r+ans)%n+1;
if ( l>r ) swap(l,r);
ans=0;
int rg=min(r,blk*bl[l]);
if ( bl[l]^bl[r] ) ans=f[bl[l]+1][r];
for ( int j=l-1; j<=rg; j++ ) ans=max( ans, query(sum[j],root[l-2],root[r],30));
printf( "%lld\n", ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: