2019年牛客多校第四场 B题xor(线段树+线性基交)
题目链接
题意
给你\(n\)个基底,求\([l,r]\)内的每个基底是否都能异或出\(x\)。
思路
线性基交板子题,但是一直没看懂咋求,先偷一份咖啡鸡板子写篇博客吧~
线性基交学习博客:传送门
代码实现如下
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef unsigned int ui;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define fuck(x) cout<<#x" = "<<x<<endl
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 50000 + 7;
const double pi = acos ( -1 );
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
ui x, a[maxn][35];
int n, q, sz, l, r;
struct base{
ui r[32];
ui f[32];
bool ins(ui x){
for (int i=31;i>=0;i--)
if (x>>i){
if (!r[i]) {r[i]=x;return 1;}
x^=r[i];
if (!x) return 0;
}
return 0;
}
void ins2(ui x){
ui tmp=x;
for (int i=31;i>=0;i--)
if (x>>i){
if (!r[i]) {f[i]=tmp;r[i]=x;return;}
x^=r[i]; tmp^=f[i];
if (!x) return;
}
return;
}
bool find(ui x){
for (int i=31;i>=0;i--)
if (x>>i){
if (!r[i]) return 0;
x^=r[i];
}
return x==0;
}
ui calc(ui x){
ui ret=0;
for (int i=31;i>=0;i--){
if (x>>i){
ret^=f[i];
x^=r[i];
}
}
return ret;
}
void print(){
for (int i=0;i<32;i++)cout<<r[i]<<' ';cout<<endl;
}
void clear(){
for (int i=0;i<32;i++) r[i]=f[i]=0;
}
};
struct node {
int l, r;
base val;
}segtree[maxn<<2];
void push_up(int rt) {
base tmp = segtree[lson].val;
base ans;
ans.clear();
for(int i = 31; i >= 0; --i) {
ui x = segtree[rson].val.r[i];
if(tmp.find(x)) {
ans.ins(x^tmp.calc(x));
} else tmp.ins2(x);
}
segtree[rt].val = ans;
}
void build(int rt, int l, int r) {
segtree[rt].l = l, segtree[rt].r = r;
if(l == r) {
for(int i = 0; i <= 31; ++i) segtree[rt].val.ins(a[l][i]);
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
push_up(rt);
}
bool query(int rt, int l, int r, LL x) {
if(segtree[rt].l == l && segtree[rt].r == r) {
return segtree[rt].val.find(x);
}
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) return query(lson, l, r, x);
else if(l > mid) return query(rson, l, r, x);
else {
return query(lson, l, mid, x) && query(rson, mid + 1, r, x);
}
}
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; ++i) {
scanf("%d", &sz);
for(int j = 0; j < sz; ++j) scanf("%lld", &a[i][j]);
for(int j = sz; j <= 31; ++j) a[i][j] = 0;
}
build(1, 1, n);
while(q--) {
scanf("%d%d%lld", &l, &r, &x);
if(query(1, l, r, x)) printf("YES\n");
else printf("NO\n");
}
return 0;
}
2019年牛客多校第四场 B题xor(线段树+线性基交)的更多相关文章
- 牛客多校第四场sequence C (线段树+单调栈)
牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...
- 2019牛客多校第四场C-sequence(单调栈+线段树)
sequence 题目传送门 解题思路 用单调栈求出每个a[i]作为最小值的最大范围.对于每个a[i],我们都要乘以一个以a[i]为区间内最小值的对应的b的区间和s,如果a[i] > 0,则s要 ...
- Palindrome Mouse(2019年牛客多校第六场C题+回文树+树状数组)
目录 题目链接 题意 思路 代码 题目链接 传送门 题意 问\(s\)串中所有本质不同的回文子串中有多少对回文子串满足\(a\)是\(b\)的子串. 思路 参考代码:传送门 本质不同的回文子串肯定是要 ...
- 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数
目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...
- Distance(2019年牛客多校第八场D题+CDQ+树状数组)
题目链接 传送门 思路 这个题在\(BZOJ\)上有个二维平面的版本(\(BZOJ2716\)天使玩偶),不过是权限题因此就不附带链接了,我也只是在算法进阶指南上看到过,那个题的写法是\(CDQ\), ...
- generator 1(2019年牛客多校第五场B题+十进制矩阵快速幂)
目录 题目链接 思路 代码 题目链接 传送门 思路 十进制矩阵快速幂. 代码 #include <set> #include <map> #include <deque& ...
- Find the median(2019年牛客多校第七场E题+左闭右开线段树)
题目链接 传送门 题意 每次往集合里面添加一段连续区间的数,然后询问当前集合内的中位数. 思路 思路很好想,但是卡内存. 当时写的动态开点线段树没卡过去,赛后机房大佬用动态开点过了,\(tql\). ...
- Explorer(2019年牛客多校第八场E题+线段树+可撤销并查集)
题目链接 传送门 题意 给你一张无向图,每条边\(u_i,v_i\)的权值范围为\([L_i,R_i]\),要经过这条边的条件是你的容量要在\([L_i,R_i]\),现在问你你有多少种容量使得你可以 ...
- 2019年牛客多校第三场 F题Planting Trees(单调队列)
题目链接 传送门 题意 给你一个\(n\times n\)的矩形,要你求出一个面积最大的矩形使得这个矩形内的最大值减最小值小于等于\(M\). 思路 单调队列滚动窗口. 比赛的时候我的想法是先枚举长度 ...
随机推荐
- 超强在线考试系统源码(私有部署&二次开发)
随着信息化技术的发展,考试系统也在进行着深入的变革.从传统的纸质考试人工评分到现在的在线考试自动评分. 在线考试系统的应用场景也在逐渐扩宽,例如:学校的学生考试.员工培训考试.招聘考试.职称考试等等. ...
- 微信公众平台开发(150)——从新浪云SAE上传图片到图文消息
从新浪云SAE上传图片到图文消息,只能用于图文消息中, 没有个数限制 if (!empty($_FILES['qrcode']['name'])){ $filename = time()." ...
- k8s安装dashboard
1.Kubernetes Dashboard 是 k8s集群的⼀个 WEB UI管理⼯具,代码托管在 github 上,地址: https://github.com/kubernetes/das ...
- python文件打包发布(引用的包也可以加进来),打包出错解决了,运行出错解决了
一开始,我以为,打包本来就很容易,可是没有..... 没想到打包还能遇到坑 T.T 打包步骤: 1.安装 pyinstaller (cmd) pip install pyinstaller 2.进入目 ...
- redis的setIfAbsent
setIfAbsent(K key, V value) 如果键不存在则新增,存在则不改变已经有的值.
- OpenCV使用CMake和MinGW的编译安装
官方教程:https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows 软件环境: Qt:5.11 CMake-3.14.4 OpenCV-4.1. ...
- SQL语句替换某个字段的部分数据
update `表名` set `字段`= replace(字段, '旧数据', '新数据');
- 可落地的DDD(4)-如何利用DDD进行微服务的划分(2)
摘要 在前面一篇介绍了如何通过DDD的思想,来调整单体服务内的工程结构,为微服务的拆分做准备.同时介绍了我们在进行微服务拆分的时候踩过的一些坑. 这篇介绍下我们最终的方案,不一定对,欢迎留言讨论. 微 ...
- [shell] while read line 与for循环的区别
[shell] while read line 与for循环的区别 while read line 与for循环的区别---转载整理 while read line 是一次性将文件信息读入并赋值给变量 ...
- C#写入Excel文件方式
由于在工作中经常要把数据库的统计数据导入Excel文件,进行IO磁盘操作,所以在这里记录下. 首先创建默认文件夹,并返回文件夹路径. private static string CPath(strin ...