BZOJ 4184: shallot

建一棵时间线段树,然后用一个局部变量维护当前的线性基就不用删除了
复杂度 \(O(n \log ^2 n)\)

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<ll, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) / 2)
#define lowbit(i) ((i) & (-i))
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=((b)-1);i>=(a);i--)
#define Edg int ccnt=1,head[N],to[E],ne[E];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[E],ne[E],c[E];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1e9 + 7;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2) {int ans = 1; for (; b; a = 1LL * a * a % MOD, b >>= 1)if (b & 1)ans = 1LL * ans * a % MOD; return ans % MOD;}
template<class T>T gcd(T a, T b) { while (b) { a %= b; std::swap(a, b); } return a; }
template<class T>bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T>bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
	int x = 0, f = 1; char ch = getc();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
	while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
	return x * f;
}

const int N = 5e5 + 7;
int n, a[N];
std::map<int, int> mp;

struct Node {
	std::vector<int> vec;
	void ins(int x) {
		rep (i, 0, vec.size()) chkmin(x, x ^ vec[i]);
		if (!x) return;
		vec.pb(x);
		per (i, 1, vec.size()) if (vec[i] > vec[i - 1]) std::swap(vec[i], vec[i - 1]);
	}
	int query() {
		int ans = 0;
		rep (i, 0, vec.size()) if ((ans ^ vec[i]) > ans) ans = ans ^ vec[i];
		return ans;
	}
} tree[N << 2], kk;

void update(int p, int l, int r, int x, int y, int v) {
	if (x > y) return;
	if (x <= l && y >= r) return tree[p].ins(v);
	if (x <= mid) update(lp, l, mid, x, y, v);
	if (y > mid) update(rp, mid + 1, r, x, y, v);
}
void solve(int p, int l, int r, Node cur) {
	rep (i, 0, tree[p].vec.size()) cur.ins(tree[p].vec[i]);
	if (l == r) {
		printf("%d\n", cur.query());
		return;
	}
	solve(lp, l, mid, cur); solve(rp, mid + 1, r, cur);
}

int main() {
#ifdef LOCAL
	freopen("ans.out", "w", stdout);
#endif
	n = _();
	rep (i, 1, n + 1) {
		a[i] = _();
		if (a[i] < 0) {
			int pos = mp[-a[i]];
			update(1, 1, n, pos, i - 1, -a[i]);
			mp[-a[i]] = 0;
		}
		else mp[a[i]] = i;
	}
	rep (i, 1, n + 1) {
		if (a[i] > 0) {
			int pos = mp[a[i]];
			if (pos) update(1, 1, n, pos, n, a[i]), mp[a[i]] = 0;
		}
	}
	solve(1, 1, n, kk);
#ifdef LOCAL
	printf("%.10f\n", (db)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}
posted @ 2020-03-08 13:52  Mrzdtz220  阅读(120)  评论(0编辑  收藏  举报