2019杭电多校第一场 B Operation

问题 B: Operation

时间限制: 5 Sec  内存限制: 256 MB
提交: 218  解决: 43
[提交] [状态] [命题人:admin]

题目描述

There is an integer sequence a of length n and there are two kinds of operations:
0 l r: select some numbers from al...ar so that their xor sum is maximum, and print the maximum value.
1 x: append x to the end of the sequence and let n=n+1.

 

输入

There are multiple test cases. The first line of input contains an integer T(T≤10), indicating the number of test cases.
For each test case: 
The first line contains two integers n,m(1≤n≤5×105,1≤m≤5×105), the number of integers initially in the sequence and the number of operations.
The second line contains n integers a1,a2,...,an(0≤ai<230), denoting the initial sequence.
Each of the next m lines contains one of the operations given above.
It's guaranteed that ∑n≤106,∑m≤106,0≤x<230.
And operations will be encrypted. You need to decode the operations as follows, where lastans denotes the answer to the last type 0 operation and is initially zero: 
For every type 0 operation, let l=(l xor lastans)mod n + 1, r=(r xor lastans)mod n + 1, and then swap(l, r) if l>r.
For every type 1 operation, let x=x xor lastans.

 

 

输出

For each type 0 operation, please output the maximum xor sum in a single line.

 

样例输入

复制样例数据

1
3 3
0 1 2
0 1 1
1 3
0 3 4

样例输出

1
3

 

[提交][状态]

题意:操作0求[l,r]区间内最大的异或和,操作1在数组末尾增加一个数

思路:没学过线性基,所以训练赛时想的很偏

大概说一下线性基的性质(线性基可以理解为由一个集合S经过一番异或操作得到的新集合,见第一条性质)

(1)线性基的元素能相互异或得到原集合的元素的所有相互异或得到的值。

(2)线性基是满足性质 1 的最小的集合。

(3)线性基没有异或和为 0 的子集。

(4)线性基中每个元素的异或方案唯一,也就是说,线性基中不同的异或组合异或出的数都是不一样的。

(5)线性基中每个元素的二进制最高位互不相同。

构造方法:假设原集合S所有数字中出现的最高位的1为第k位,那么建一个数组a,大小为[0,k]

对原集合S中每一个数字x,从高位到低位扫,假设第i位为1,如果a的第i位为0,则令a的第i位为x,否则令x异或a的第i位(也就是说把x的这一位的1给消掉)

经常用到线性基的情况

查询原集合内任意几个元素 xor 的最大/小值,查询某个数是否能被异或出来(如果插入的数x被异或成了 0,能被异或出来)

回到这个题

可以用贪心的思想,利用前缀来解决超时的问题

在每一次添加元素都在继承原来的线性基

对[l,r]进行查询时,先确定范围,f[r][j]的线性基存的范围为[1,pos[r][j]],保证了区间在r的左侧,那么还需要pos[r][j] >= l,这样也保证了在l的右侧

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e6 + 10;
int a[maxn];
int f[maxn][32], pos[maxn][32];

void add(int i, int x) {
    for (int j = 30; j >= 0; j--) {
        f[i][j] = f[i - 1][j];    //保存线性基,这步继承1到i-1的线性基
        pos[i][j] = pos[i - 1][j];//保存线性基第j个基的元素的位置
    }
    int k = i;
    for (int j = 30; j >= 0; j--) {
        if (x >> j) {
            if (!f[i][j]) {   //a数组不存在,则直接赋值
                f[i][j] = x;
                pos[i][j] = k;
                break;
            } else {  //存在则需要把原来这位的数往左移动(不太懂为什么)
                if (k > pos[i][j]) {
                    swap(k, pos[i][j]);
                    swap(x, f[i][j]);
                }
                x ^= f[i][j];
            }
        }
    }
}

int main() {
    //freopen("out.txt", "r", stdin);
    int T;
    scanf("%d", &T);
    while (T--) {
        int n, q;
        scanf("%d%d", &n, &q);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            add(i, a[i]);
        }
        int op, l, r, tmp = 0;
        while (q--) {
            scanf("%d", &op);
            if (op == 0) {
                scanf("%d%d", &l, &r);
                l = (l ^ tmp) % n + 1;
                r = (r ^ tmp) % n + 1;
                if (l > r) swap(l, r);
                tmp = 0;
                for (int j = 30; j >= 0; j--) {
                    if ((tmp ^ f[r][j]) > tmp && pos[r][j] >= l)
                        tmp ^= f[r][j];
                }
                printf("%d\n", tmp);
            } else {
                scanf("%d", &a[++n]);
                a[n] ^= tmp;
                add(n, a[n]);
            }
        }
        memset(f, 0, sizeof(f));
        memset(pos, 0, sizeof(pos));
    }
    return 0;
}

参考:线性基  不错的题解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值