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;
}

Wanafly 挑战赛 14 E 无效位置 (线性基+并查集)的更多相关文章

  1. Educational Codeforces Round 14 D. Swaps in Permutation(并查集)

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...

  2. Wannafly挑战赛14 - E 并查集维护线性基区间

    给一个1-base数组{a},有N次操作,每次操作会使一个位置无效.一个区间的权值定义为这个区间里选出一些数的异或和的最大值.求在每次操作前,所有不包含无效位置的区间的权值的最大值. 线性基删除不知道 ...

  3. Wannafly挑战赛14E无效位置

    https://www.nowcoder.com/acm/contest/81/E 给一个1-base数组{a},有N次操作,每次操作会使一个位置无效.一个区间的权值定义为这个区间里选出一些数的异或和 ...

  4. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  5. wannafly挑战赛14

    第一次打wannafly..觉得自己好菜啊... 题目描述 在三维空间中,平面 x = 0, y = 0, z = 0,以及平面 x + y + z = K 围成了一个三棱锥. 整天与整数打交道的小明 ...

  6. BZOJ_2844 albus就是要第一个出场 【线性基】

    一.题目 albus就是要第一个出场 二.分析 非常有助于理解线性基的一题. 构造线性基$B$后,如果$|A| > |B|$,那么就意味着有些数可以由$B$中的数异或出来,而多的数可以取或者不取 ...

  7. BZOJ_2115 [Wc2011] Xor 【图上线性基】

    一.题目 [Wc2011] Xor 二.分析 比较有意思的一题,这里也学到一个结论:$1$到$N$的任意一条路径异或和,可以是一个任意一条$1$到$N$的异或和与图中一些环的异或和组合得到.因为一个数 ...

  8. 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 238  Solved: 113[Submit][Status ...

  9. 【HDU 3949】 XOR (线性基,高斯消元)

    XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. js赋值符号“=”的小例子

    var obj1={x:5}; var obj2=obj1; obj1.a=obj1={x:6}; console.log(obj1.a); console.log(obj2.a); 为什么obj1. ...

  2. markdown-it + highlight.js简易实现

    markdown-it 官方demo markdown-it 文档 1.配置highlightjs,针对markdown中各种语言高亮,针对对应的标签 pre code 里面的样式 -- index. ...

  3. 小爬爬1:开篇&&简单介绍启动

    1.第一阶段的内容 2.学习的方法? 思考,总结,重复 3.长大了意味着什么?家庭的责任,真的很重 4.数据分析&&数据清洗 numpy&&pandas&&am ...

  4. 2019-3-21-win10-uwp-修改图片质量压缩图片

    title author date CreateTime categories win10 uwp 修改图片质量压缩图片 lindexi 2019-03-21 15:29:20 +0800 2019- ...

  5. idea建立maven聚合项目 标签: mavenidea 2017-01-08 15:33 2477人阅读 评论(30)

    上篇文章写了如何用idea建立maven项目,idea建立maven聚合项目我感觉不如eclipse方便,不过并不是没有办法,下面写一下这个小教程. 建立maven project 建立maven p ...

  6. [java]网上商城错误集锦 2016-05-08 21:49 499人阅读 评论(32) 收藏

    网上商城敲到了第三天,马上就要踏入第四天啦,不过敲得这几天,学习到了不少东西,也接触了很多新东西,当然,遇到最多的,就是各种bug!下面总结一下自己遇到的这些bug. 一.时间获取不到 这个bug起源 ...

  7. Libevent:2设置

    Libevent有一些整个进程共享的全局设置.这些设置会影响到整个的库.因此必须在调用Libevent其他函数之前进行设置,否则,LIbevent就会陷入不一致的状态. 一:Libevent中的日志信 ...

  8. Uva 10074【递推dp】

    UVa 10074 题意:求01矩阵的最大子0矩阵. http://www.csie.ntnu.edu.tw/~u91029/MaximumSubarray.html#2 这里说的很清楚.先求Larg ...

  9. UVa 10323 【数学】

    UVa 10323 题目:计算阶乘在10000~6227020800之间的值,不在范围对应输出Under或者Over. 分析:简单题.数论.因为13!=6227020800,7!<10000&l ...

  10. Cacti 加入多台主机带宽汇聚

            前面我写了一个cacti加入主机带宽监控的博客.能够參考http://blog.csdn.net/dai451954706/article/details/35272465 .有时可能 ...