Wanafly 挑战赛 14 E 无效位置 (线性基+并查集)
传送门:https://ac.nowcoder.com/acm/contest/81/#question
题意:
n个数,m次操作
一个区间的权值定义为这个区间里选出一些数的异或和的最大值
每次将一个位置的无效,问你每次操作之前 所有 不包含无效位置的区间的权值的最大值。
题解:
倒着做
那么我们就是在一个空集上,不断的做插入操作
每次插入询问区间异或的最大值,这个可以用线性基来做
然后如果集合凑到一起去了,我们需要合并集合,用并查集存一下下标即可
代码:
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O = /O
// ____/`---'\____
// .' | |// `.
// / ||| : |||//
// / _||||| -:- |||||-
// | | - /// | |
// | \_| ''---/'' | |
// .-\__ `-` ___/-. /
// ___`. .' /--.-- `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;` _ /`;.`/ - ` : | |
// `-. \_ __ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********
")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]
"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % 2)ans = ans * a;
a = a * a;
b /= 2;
} return ans;
}
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
int a[maxn];
int x[maxn];
int fa[maxn];
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int lb[maxn][64];
int ans = 0;
void insert(int id, int val) {
for(int i = 30; i >= 0; i--) {
if ((val >> i) & 1) {
if (lb[id][i]) {
val ^= lb[id][i];
} else {
lb[id][i] = val;
break;
}
}
}
int sum = 0;
for (int i = 30; i >= 0; i--) {
sum = max(sum, sum ^ lb[id][i]);
}
ans = max(ans, sum);
}
void merge(int x, int y) {
for (int i = 0; i <= 30; i++) {
if (lb[y][i]) {
insert(x, lb[y][i]);
}
}
fa[y] = x;
}
int flag[maxn];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%d", &x[i]);
}
for(int i = 1; i <= n; i++) {
fa[i] = i;
}
vector<int> res;
for(int i = n; i >= 1; i--) {
flag[x[i]] = 1;
insert(x[i], a[x[i]]);
if (flag[x[i] - 1]) {
merge(find(x[i]), find(x[i] - 1));
}
if (flag[x[i] + 1]) {
merge(find(x[i]), find(x[i] + 1));
}
res.push_back(ans);
}
reverse(res.begin(), res.end());
for(auto it : res) {
printf("%d
", it);
}
return 0;
}