[BZOJ3261]最大异或和

[BZOJ3261]最大异或和

试题描述

给定一个非负整数序列 {a},初始长度为 N。       
有   M个操作,有以下两种操作类型:
 
1 、A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1。
2 、Q l r x:询问操作,你需要找到一个位置 p,满足 l<=p<=r,使得:
 
a[p] xor a[p+1] xor ... xor a[N] xor x 最大,输出最大是多少。  

输入

第一行包含两个整数 N  ,M,含义如问题描述所示。   
第二行包含 N个非负整数,表示初始的序列 A 。 
 
接下来 M行,每行描述一个操作,格式如题面所述。

输出

假设询问操作有 T个,则输出应该有 T行,每行一个整数表示询问的答案

输入示例

5 5
2 6 4 3 6
A 1
Q 3 5 4
A 4
Q 5 7 0
Q 3 6 6

输出示例

4
5
6

数据规模及约定

对于测试点 1-2,N,M<=5 。
对于测试点 3-7,N,M<=80000 。
对于测试点 8-10,N,M<=300000 。
其中测试点 1, 3, 5, 7, 9保证没有修改操作。
对于 100% 的数据, 0<=a[i]<=10^7。

题解

我们令 S[i] = A[i] ^ A[i+1] ^ A[i+2] ^ ... ^ A[n],那么我们对 S 数列建立可持久化 Trie 树,查询时贪心一波就好了。

考虑在序列末尾插入一个数 x,序列长度为 n,我们需要让所有的 S[i](1 ≤ i ≤ n) 变成 S[i] ^ x,然后再在最后一个版本基础上插入 x。

注意到这个修改是全局的,所以我们可以对于每一层打一个标记,rev[i] 表示第 i 层是否取反,然后我们每次在 trie 树上找路径时如果到了第 i 层我们把最初想要走的方向异或一下 rev[i] 就好了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;
 
const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
    if(Head == Tail) {
        int l = fread(buffer, 1, BufferSize, stdin);
        Tail = (Head = buffer) + l;
    }
    return *Head++;
}
int read() {
    int x = 0, f = 1; char c = Getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
    return x * f;
}
 
#define maxn 600010
#define maxnode 15000010
 
int ToT, rt[maxn], ch[maxnode][2], siz[maxnode];
int num[30], cnt; bool rev[30];
#define to(u,c) ch[u][c^rev[p]]
 
void update(int& y, int x, int p) {
    rev[p] ^= num[p];
    siz[y = ++ToT] = siz[x] + 1;
    if(!p) return ;
    memcpy(ch[y], ch[x], sizeof(ch[x]));
    update(to(y, num[p]), to(x, num[p]), p - 1);
    return ;
}
int query(int lu, int ru, int p, int sum) {
    if(!p) return sum;
    if(siz[to(ru,num[p]^1)] - siz[to(lu,num[p]^1)])
        return query(to(lu, num[p] ^ 1), to(ru, num[p] ^ 1), p - 1, sum << 1 | 1);
    return query(to(lu, num[p]), to(ru, num[p]), p - 1, sum << 1);
}
 
void debug(int x, int u) {
    cnt = 0; memset(num, 0, sizeof(num));
    while(x) num[++cnt] = x & 1, x >>= 1;
    printf("%d ", u);
    for(int p = 24; p; p--) {
        u = to(u, num[p]);
        printf("%d%c", u, p > 1 ? ' ' : '\n');
    }
    puts("endpath");
    return ;
}
 
int main() {
    int n = read(), q = read();
    for(int i = 1; i <= n; i++) {
        int x = read();
        cnt = 0; memset(num, 0, sizeof(num));
        while(x) num[++cnt] = x & 1, x >>= 1;
        update(rt[i], rt[i-1], 24);
    }
     
    while(q--) {
        char opt = Getchar();
        while(!isalpha(opt)) opt = Getchar();
        if(opt == 'A') {
            int x = read();
            cnt = 0; memset(num, 0, sizeof(num));
            while(x) num[++cnt] = x & 1, x >>= 1;
            n++; update(rt[n], rt[n-1], 24);
            /*debug(0, rt[2]);
            debug(2, rt[2]);
            debug(4, rt[6]);
            debug(0, rt[6]);
            debug(3, rt[6]);
            debug(5, rt[6]);
            debug(4, rt[7]); // */
        }
        if(opt == 'Q') {
            int l = read(), r = read(), x = read();
            cnt = 0; memset(num, 0, sizeof(num));
            while(x) num[++cnt] = x & 1, x >>= 1;
            printf("%d\n", query(rt[l-1], rt[r], 24, 0));
        }
    }
     
    return 0;
}

 

posted @ 2017-03-25 14:37  xjr01  阅读(190)  评论(0编辑  收藏  举报