bzoj2741[FOTILE模拟赛L]

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[N];
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[n]; 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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值