# [BZOJ4184]shallot

### [BZOJ4184]shallot

#### 输入示例

6
1 2 3 4 -2 -3


#### 输出示例

1
3
3
7
7
5


#### 数据规模及约定

$n \leq 500000, a_i \leq 2^{31}-1$

#### 题解

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;

int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}

#define maxn 500010
#define MOD 1000037

struct Hash {
int ToT, head[MOD], nxt[maxn], val[maxn];
vector <int> tim[maxn];
Hash(): ToT(0) { memset(head, 0, sizeof(head)); }
int Find(int x) {
int u = x % MOD;
for(int e = head[u]; e; e = nxt[e]) if(val[e] == x) return e;
return 0;
}
void Insert(int x, int pos) {
int id = Find(x);
if(id) { tim[id].push_back(pos); return ; }
int u = x % MOD;
val[++ToT] = x; tim[ToT].push_back(pos); nxt[ToT] = head[u]; head[u] = ToT;
return ;
}
} hh;

struct Bit { int a[31]; } tmp;

int n;
vector <int> vals[maxn<<2];
void update(int o, int l, int r, int ql, int qr, int v) {
if(ql <= l && r <= qr) vals[o].push_back(v);
else {
int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
if(ql <= mid) update(lc, l, mid, ql, qr, v);
if(qr > mid) update(rc, mid + 1, r, ql, qr, v);
}
return ;
}
void solve(int o, int l, int r, Bit b) {
for(int i = 0; i < vals[o].size(); i++) {
int x = vals[o][i];
for(int j = 30; j >= 0; j--)
if(!b.a[j] && (x >> j & 1)) {
b.a[j] = x; break;
}
else if(x >> j & 1) x ^= b.a[j];
}
if(l == r) {
int ans = 0;
for(int j = 30; j >= 0; j--) if(ans < (ans ^ b.a[j])) ans ^= b.a[j];
printf("%d\n", ans);
return ;
}
int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
solve(lc, l, mid, b); solve(rc, mid + 1, r, b);
return ;
}

int main() {
for(int i = 1; i <= n; i++) {
int x = read();
if(x > 0) hh.Insert(x, i);
else {
int id = hh.Find(-x);
update(1, 1, n, hh.tim[id][hh.tim[id].size()-1], i - 1, -x);
hh.tim[id].pop_back();
}
}

for(int i = 1; i <= hh.ToT; i++)
for(int j = 0; j < hh.tim[i].size(); j++) update(1, 1, n, hh.tim[i][j], n, hh.val[i]);

solve(1, 1, n, tmp);

return 0;
}

posted @ 2017-10-01 15:18  xjr01  阅读(227)  评论(0编辑  收藏  举报