[线段树分治][线性基]BZOJ 4184: shallot

12 篇文章 0 订阅
6 篇文章 0 订阅

Description D e s c r i p t i o n

小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏。
每个时刻她会给小葱一颗小葱苗或者是从小葱手里拿走一颗小葱苗,并且让小葱从自己手中的小葱苗里选出一些小葱苗使得选出的小葱苗上的数字的异或和最大。

Solution S o l u t i o n

挺显然的分治+线性基的吧
用线段树里定位区间的方法,把每个操作存在的区间定位好。
最后线性基询问就好啦。

#include <bits/stdc++.h>
using namespace std;

const int N = 505050;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
template<typename T>
inline void read(T &x) {
    static char c; x = 0; int sgn = 0;
    for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
    if (sgn) x = -x;
}

struct LB {
    int a[32];
    inline void Clear(void) {
        for(int i = 0; i < 31; i++) a[i] = 0;
    }
    inline void Insert(int x) {
        for (int i = 31; i >= 0; i--)
            if ((x >> i) & 1) {
                if (!a[i]) {
                    a[i] = x; break;
                }
                x ^= a[i];
            }
    }
    inline int Max(void) {
        int res = 0;
        for (int i = 31; i >= 0; i--)
            if ((a[i] ^ res) > res) res ^= a[i];
        return res;
    }
};
map<int, int> mp;
int cnt[N], lst[N], val[N];
struct data {
    int l, r, x;
    data(int _l = 0, int _r = 0, int _x = 0):l(_l), r(_r), x(_x) {}
};
LB I;
vector<data> a;
int n, m, x, Mcnt, pos, sgn;
int ans[N];

inline void DivAndConq(int l, int r, LB L, vector<data> arr) {
    vector<data> ls, rs;
    ls.clear(); rs.clear();
    int mid = (l + r) >> 1;
    for (int i = 0; i < arr.size(); i++)
        if (arr[i].l <= l && arr[i].r >= r) L.Insert(val[arr[i].x]);
        else {
            if (arr[i].l <= mid) ls.push_back(arr[i]);
            if (arr[i].r > mid) rs.push_back(arr[i]);
        }
    if (l == r) return (void)(ans[l] = L.Max());
    DivAndConq(l, mid, L, ls);
    DivAndConq(mid + 1, r, L, rs);
}

int main(void) {
    freopen("1.in", "r", stdin);
    read(n);
    for (int i = 1; i <= n; i++) {
        read(x); sgn = x > 0 ? 1 : -1;
        if (sgn < 0) x = -x;
        if (!mp.count(x)) {
            mp[x] = ++Mcnt;
            val[Mcnt] = x;
        }
        pos = mp[x];
        if (!cnt[pos]) {
            a.push_back(data(i, n, pos));
            lst[pos] = a.size() - 1;
        }
        cnt[pos] += sgn;
        if (!cnt[pos]) a[lst[pos]].r = i - 1;
    }
    I.Clear();
    DivAndConq(1, n, I, a);
    for (int i = 1; i <= n; i++)
        printf("%d\n", ans[i]);
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值