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("*********\n")
#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)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
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\n", it);
}
return 0;
}

最新文章

  1. 接口自动化之Postman+Newman
  2. 怎么使用git来管理项目版本?
  3. PHP生成二维码库phpqrcode
  4. bjfu1284 判别正则表达式
  5. nyist 58 最小步数 BFS
  6. zabbix windows angent安装:
  7. 向西项目管理工具Maven一片
  8. opencv探索之路(一):win10 X64+VS2015+opencv3.10安装教程
  9. Awk,Cat,Head分析Nginx日志常用命令
  10. Android 文件下载三种基本方式
  11. Salesforce 简介
  12. ionic ion-tab图标修改, 自定义tab图标
  13. Fescar Quick Start
  14. centos 6.x下编译dpdk 16.7 心得
  15. POJ1417 True Liars
  16. Flume学习之路 (一)Flume的基础介绍
  17. 玲珑oj 1028 贪心
  18. Sublime Text 插件推荐——for web developers
  19. Python(迭代、三元表达式、列表生成、生成器、迭代器)
  20. Windows平台下源码分析工具

热门文章

  1. Selenium-----wait的三种等待
  2. postman认证使用篇(五)
  3. C++四:继承与派生
  4. laravel 踩坑 env,config
  5. 从零学React Native之06flexbox布局
  6. [C#] 如何分析stackoverflow等clr错误
  7. @noi.ac - 441@ 你天天努力
  8. @noi.ac - 492@ casino
  9. Python 的经典入门书籍
  10. npx cowsay 让动物说话~