2019杭电多校第一场hdu6579 Operation(线性基)

Operation

题目传送门

解题思路

把右边的数尽量往高位放,构造线性基的时候同时记录其在原序列中的位置,在可以插入的时候如果那个位置上存在的数字的位置比新放入的要小,就把旧的往后挤。用这种发现构造前缀线性基,求最大前缀和的时候只有忽略位置比l小的即可。

代码如下

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

inline int read(){
    int res = 0, w = 0; char ch = 0;
    while(!isdigit(ch)){
        w |= ch == '-', ch = getchar();
    }
    while(isdigit(ch)){
        res = (res << 3) + (res << 1) + (ch ^ 48);
        ch = getchar();
    }
    return w ? -res : res;
}

const int N = 1000005;

int a[N];
int b[N][40], pos[N][40];

void insert(int k, int x)
{
    for(int i = 30; i >= 0; i --){
        b[k][i] = b[k - 1][i];
        pos[k][i] = pos[k - 1][i];
    }
    int t = k;
    for(int i = 30; i >= 0; i --){
        if((x >> i) & 1){
            if(b[t][i]){
                if(pos[t][i] < k){
                    swap(b[t][i], x);
                    swap(pos[t][i], k);
                }
                x ^= b[t][i];
            }
            else {
                b[t][i] = x;
                pos[t][i] = k;
                break;
            }
        }
    }
}

int query(int l, int r)
{
    int ans = 0;
    for(int i = 30; i >= 0; i --){
        if(pos[r][i] >= l){
            if((ans ^ b[r][i]) > ans)
                ans ^= b[r][i];
        }
    }
    return ans;
}

int main()
{
    int T;
    cin >> T;
    while(T --){
        int n, m;
        n = read(), m = read();
        for(int i = 1; i <= n; i ++){
            a[i] = read();
            insert(i, a[i]);
        }  
        int last = 0;
        for(int i = 1; i <= m; i ++){
            int opt;
            opt = read();
            if(opt == 0){
                int l, r;
                l = read(), r = read();
                l = (l ^ last) % n + 1;
                r = (r ^ last) % n + 1;
                if(l > r)
                    swap(l, r);
                last = query(l, r);
                printf("%d\n", last);
            }
            else {
                int x = read();
                ++n;
                insert(n, x ^ last);
            }
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/whisperlzw/p/11243293.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值