Wanafly 挑战赛 14 E 无效位置 (线性基+并查集)
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 无效位置 (线性基+并查集)的更多相关文章
- Educational Codeforces Round 14 D. Swaps in Permutation(并查集)
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...
- Wannafly挑战赛14 - E 并查集维护线性基区间
给一个1-base数组{a},有N次操作,每次操作会使一个位置无效.一个区间的权值定义为这个区间里选出一些数的异或和的最大值.求在每次操作前,所有不包含无效位置的区间的权值的最大值. 线性基删除不知道 ...
- Wannafly挑战赛14E无效位置
https://www.nowcoder.com/acm/contest/81/E 给一个1-base数组{a},有N次操作,每次操作会使一个位置无效.一个区间的权值定义为这个区间里选出一些数的异或和 ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- wannafly挑战赛14
第一次打wannafly..觉得自己好菜啊... 题目描述 在三维空间中,平面 x = 0, y = 0, z = 0,以及平面 x + y + z = K 围成了一个三棱锥. 整天与整数打交道的小明 ...
- BZOJ_2844 albus就是要第一个出场 【线性基】
一.题目 albus就是要第一个出场 二.分析 非常有助于理解线性基的一题. 构造线性基$B$后,如果$|A| > |B|$,那么就意味着有些数可以由$B$中的数异或出来,而多的数可以取或者不取 ...
- BZOJ_2115 [Wc2011] Xor 【图上线性基】
一.题目 [Wc2011] Xor 二.分析 比较有意思的一题,这里也学到一个结论:$1$到$N$的任意一条路径异或和,可以是一个任意一条$1$到$N$的异或和与图中一些环的异或和组合得到.因为一个数 ...
- 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并
4568: [Scoi2016]幸运数字 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 238 Solved: 113[Submit][Status ...
- 【HDU 3949】 XOR (线性基,高斯消元)
XOR Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- SharePoint开发中怎样使用Visual Studio给你的Web Part加入图标
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u012025054/article/details/36051545 SharePoint开发中怎样 ...
- iOS 通知观察者的被调函数不一定运行在主线程
Tony in iOS | 08/08/2013 iOS 通知观察者的被调函数不一定运行在主线程 今天修复Bug时候发现的一个小细节,记录下. 问题描述 事情是这样的:我在A视图(UITableVie ...
- git 403
MacBook-Pro:~ easy$ git pull fatal: unable to access 'https://git.xx.com:40443/source/projectName/': ...
- @codeforces - 444A@ DZY Loves Physics
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 点 m 边的图,边有边权,点有点权. 找到一个连通 ...
- C++讲课总结 标签: c++总结 2015-02-28 14:48 671人阅读 评论(25) 收藏
昨天老师算是给串了一本C++ 的课本,根据自己的理解,赶紧记录一下,也好作为自己学习时候的根据. C++编程简介:每本讲语言的书,第一章总是简介,内容无非是发展历史,语言特色等东西,专业的东西不多,都 ...
- 10Redis键空间通知(keyspace notifications)
Redis的键空间通知(keyspace notifications)功能是自2.8.0版本开始加入的,客户端可以通过订阅/发布(Pub/Sub)机制,接收那些以某种方式改变了Redis数据空间的事件 ...
- @NOIP2018 - D1T1@ 铺设道路
目录 @题目描述@ @考场上的思路@ @比较正常的题解@ @题目描述@ 春春是一名道路工程师,负责铺设一条长度为 n 的道路. 铺设道路的主要工作是填平下陷的地表.整段道路可以看作是 n 块首尾相连的 ...
- 最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)
最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...
- python常量和变量
1.1 常量 常量是内存中用于保存固定值的单元,在程序中常量的值不能发生改变:python并没有命名常量,也就是说不能像C语言那样给常量起一个名字. python常量包括:数字.字符串.布尔值.空值: ...
- vue element 给指数的div加loading
const loading = this.$loading({ lock: true, text: 'Loading', spinner: 'el-icon-lo ...