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\). 思路 单调队列滚动窗口. 比赛的时候我的想法是先枚举长度 ...
随机推荐
- MongoDB Shell 命令
更新列名 db.Stores.update({}, {$rename : {"StoreId" : "MetaId"}}, false, true) 查询长度 ...
- Spring MVC标签
1.我们平时使用表单的时候,一般都是这样: <form id="form" action="Login.do" method="post&quo ...
- Azure DevOps Server (TFS) 修改工作项附件大小限制
1. 问题描述 当上传工作项附件时,系统提示"附件大小限制" 2.解决方案 2.1 默认设置 默认情况下,Azure DevOps Service和Team Foundation ...
- [记录]mscorlib recursive resource lookup bug解决方法
[Content]Expression: [mscorlib recursive resource lookup bug]Description: Infinite recursion during ...
- xunsearch安装
1.下载 sudo wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2 2.解压 sudo tar -xvjf x ...
- Java的常用API
Object类 1.toString方法在我们直接使用输出语句输出对象的时候,其实通过该对象调用了其toString()方法. 2.equals方法方法摘要:类默认继承了Object类,所以可以使用O ...
- 常用Java API之Ramdom--用代码模拟猜数小游戏
常用Java API之Ramdom Ramdom类用来生成随机数字.使用起来也是三个步骤: 1.导包 import java.util.Random; 2.创建 Random r = new Rand ...
- MQTT --- Retained Message
保留消息定义 如果PUBLISH消息的RETAIN标记位被设置为1,则称该消息为“保留消息”: Broker会存储每个Topic的最后一条保留消息及其Qos,当订阅该Topic的客户端上线后,Brok ...
- springboot初体验-不知道怎么创建spring-boot项目?
https://spring.io/projects/spring-boot/ 在以上地址找到 Quick start Bootstrap your application with Spring I ...
- asp获取access数据库中的一条随机记录
针对“用一条SQL得到数据库中的随机记录集”问题在网上已经有很多答案了: SQL Server 2000: SELECT TOP n * FROM tanblename ORDER BY NEWID( ...