2019年杭电多校第一场 1002题Operation(HDU6579+线性基)

题目链接

传送门

题意

初始时有 n n n个数,现在有 q q q次操作:

  • 查询 [ l , r ] [l,r] [l,r]内选择一些数使得异或和最大;
  • 在末尾加入一个数。

题目强制在线。

思路

对于 i i i我们记录 [ 1 , i ] [1,i] [1,i]每个基底最靠近 i i i的位置和这个位置的值,然后查询时看 r r r这个位置记录的每个基底的位置是否大于等于 l l l,如果大于等于那么 [ l , r ] [l,r] [l,r]内一定有一个位置可以贡献这个基底,然后比较答案大小即可。
本题和 c f 1100 F cf1100F cf1100F一样的写法只是多了个操作而已。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)

const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1000000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

int t, n, q, op, l, r, x;
int a[maxn], b[32], pos[32], base[maxn][32], las[maxn][32];

bool add(int val, int pp) {
    for (int i = 30; i >= 0; i--) {
        if (val & (1ll << i)) {
            if (!b[i]) {
                pos[i] = pp;
                b[i] = val;
                break;
            }
            if(pos[i] < pp) {
                swap(b[i], val);
                swap(pos[i], pp);
            }
            val ^= b[i];
        }
    }
    return val > 0;
}

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &q);
        for(int i = 30; i >= 0; --i) b[i] = 0, pos[i] = 0;
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            add(a[i], i);
            for(int j = 30; j >= 0; --j) base[i][j] = b[j], las[i][j] = pos[j];
        }
        int lastans = 0;
        while(q--) {
            scanf("%d", &op);
            if(op) {
                ++n;
                scanf("%d", &x);
                x ^= lastans;
                add(x, n);
                for(int i = 30; i >= 0; --i) base[n][i] = b[i], las[n][i] = pos[i];
            } else {
                scanf("%d%d", &l, &r);
                l = (l ^ lastans) % n + 1, r = (r ^ lastans) % n + 1;
                if(l > r) swap(l, r);
                lastans = 0;
                for(int i = 30; i >= 0; --i) {
                    if(las[r][i] >= l && (lastans ^ base[r][i]) > lastans) {
                        lastans ^= base[r][i];
                    }
                }
                printf("%d\n", lastans);
            }
        }
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值