题意:

传送门

给\(n\)个集合,每个集合有一些数。给出\(m\)个询问,再给出\(l\)和\(r\)和一个数\(v\),问你任意的\(i \in[l,r]\)的集合,能不能找出子集异或为\(v\)。简单点说,\(v\)能用\([l,r]\)任意一个集合的子集异或和表示。

思路:

子集异或和显然是用线性基。我们用线段树维护任意区间的线性基交集即可。

代码:

/**
求交集 O(logn * logn)
**/
LBasis intersection(const LBasis &a, const LBasis &b){
LBasis ans, c = b, d = b;
ans.init();
for (int i = 0; i <= 32; i++){
ll x = a.d[i];
if(!x)continue;
int j = i;
ll T = 0;
for(; j >= 0; --j){
if((x >> j) & 1)
if(c.d[j]) {x ^= c.d[j]; T ^= d.d[j];}
else break;
}
if(!x) ans.d[i] = T;
else {c.d[j] = x; d.d[j] = T;}
}
return ans;
}
#include<map>
#include<set>
#include<cmath>
#include<cstdio>
#include<stack>
#include<ctime>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 50000 + 5;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
using namespace std; struct LBasis{
ll d[33];
int tot;
void init(){
memset(d, 0, sizeof(d));
tot = 0;
}
bool insert(ll x){
for(int i = 32; i >= 0; i--){
if(x & (1LL << i)){
if(d[i]) x ^= d[i];
else{
d[i] = x;
return true;
}
}
}
return false;
} bool checkin(ll x){
for(int i = 32; i >= 0; i--){
if(x & (1LL << i)){
if(d[i]) x ^= d[i];
else return false;
}
}
return true;
} };
LBasis intersection(const LBasis &a, const LBasis &b){
LBasis ans, c = b, d = b;
ans.init();
for (int i = 0; i <= 32; i++){
ll x = a.d[i];
if(!x)continue;
int j = i;
ll T = 0;
for(; j >= 0; --j){
if((x >> j) & 1)
if(c.d[j]) {x ^= c.d[j]; T ^= d.d[j];}
else break;
}
if(!x) ans.d[i] = T;
else {c.d[j] = x; d.d[j] = T;}
}
return ans;
}
LBasis node[maxn << 2];
void pushup(int rt){
node[rt] = intersection(node[rt << 1], node[rt << 1 | 1]);
}
void build(int l, int r, int rt){
if(l == r){
int sz;
scanf("%d", &sz);
node[rt].init();
while(sz--){
ll x;
scanf("%lld", &x);
node[rt].insert(x);
}
return;
}
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
pushup(rt);
}
bool query(int L, int R, int l, int r, ll v, int rt){
if(L <= l && R >= r){
return node[rt].checkin(v);
}
int m = (l + r) >> 1;
bool ok = true;
if(L <= m)
ok = ok && query(L, R, l, m, v, rt << 1);
if(R > m)
ok = ok && query(L, R, m + 1, r, v, rt << 1 | 1);
return ok;
}
int main(){
int n, m;
scanf("%d%d", &n, &m);
build(1, n, 1);
while(m--){
int l, r;
ll x;
scanf("%d%d%lld", &l, &r, &x);
if(query(l, r, 1, n, x, 1)) printf("YES\n");
else printf("NO\n");
}
return 0;
}

2019牛客多校第四场B xor(线性基求交)题解的更多相关文章

  1. 2019牛客多校第四场B xor——线段树&&线性基的交

    题意 给你 $n$ 个集合,每个集合中包含一些整数.我们说一个集合表示一个整数当且仅当存在一个子集其异或和等于这个整数.现在你需要回答 $m$ 次询问 ($l, r, x$),是否 $l$ 到 $r$ ...

  2. 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数

    目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...

  3. 2019牛客多校第四场 A meeting

    链接:https://ac.nowcoder.com/acm/contest/884/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10485 ...

  4. 2019牛客多校第四场J free——分层图&&最短路

    题意 一张无向图,每条边有权值,可以选择不超过 $k$ 条路使其权值变成0,求 $S$ 到 $T$ 的最短路.(同洛谷 P4568) 分析 首先,分层图最短路可以有效解决这种带有 「阶段性」的最短路, ...

  5. 2019牛客多校第四场A meeting——树的直径

    题意: 一颗 $n$ 个节点的树上标有 $k$ 个点,找一点使得到 $k$ 个关键结点的最大距离最小. 分析: 问题等价于求树的直径,最小距离即为直径除2向上取整. 有两种求法,一是动态规划,对于每个 ...

  6. [2019牛客多校第四场][G. Tree]

    题目链接:https://ac.nowcoder.com/acm/contest/884/G 题目大意:给定一个树\(A\),再给出\(t\)次询问,问\(A\)中有多少连通子图与树\(B_i\)同构 ...

  7. 2019牛客多校第四场D-triples I 贪心

    D-triples 题意 给你一个\(n\),问至少有几个数或运算起来可以等于\(n\),并且输出数量和这个几个数.题目说明给的\(n\)一定符合条件(不会输出\(n= 1\) 之类不存在情况). 思 ...

  8. 2019牛客多校第四场C-sequence(单调栈+线段树)

    sequence 题目传送门 解题思路 用单调栈求出每个a[i]作为最小值的最大范围.对于每个a[i],我们都要乘以一个以a[i]为区间内最小值的对应的b的区间和s,如果a[i] > 0,则s要 ...

  9. 2019牛客多校第四场K number dp or 思维

    number 题意 给一个数字串,问有几个子串是300的倍数 分析 dp写法:这题一看就很dp,直接一个状态dp[i][j]在第i位的时候膜300的余数是j左过去即可.这题比赛的时候样例老是少1,后面 ...

随机推荐

  1. IP2723T中文规格书PDF

    IP2723T 是一款集成多种协议.用于 USB 输出端口的快充协议 IC.支持多种快充协议,包括 USBTypeC DFP,PD2.0/PD3.0/PPS,HVDCPQC4/QC4+/QC3.0/Q ...

  2. 1.2V升压到3V和3.3V的升压芯片

    1.2V镍氢电池升压到3V和3.3V输出,1.2V升压3V,1.2V升压3.3V稳压输出供电的芯片. PW5100 是一款低静态电流.达效率. PFM 模式控制的同步升压变换器. PW5100 所需的 ...

  3. Python+Selenium+Unittest实现PO模式web自动化框架(6)

    1.TestCases目录下的模块 TestCases目录下是存放测试用例的目录. TestCases目录下的测试用例采用unittest框架来构建. 例如:登录功能的测试用例.(test_1_log ...

  4. C#高级编程第11版 - 第五章 索引

    [1]5.1 泛型概述 1.通过泛型,你可以创建独立于特定类型(contained types)以外的方法和类,而不用为不同类型编写多份同样功能的代码,你只需要创建一个方法或者类. 2.泛型类使用泛型 ...

  5. Netty服务端Server代码说明

    本文是简单的Netty启动服务端代码理解笔记 public class MyServer { public static void main(String[] args) throws Excepti ...

  6. CentOS7.9静默安装Oracle19C软件

    CentOS7.9静默安装Oracle19C软件 Oracle发布了支持的版本.可以看到了Oracle11gR2和Oracle12C.一直到2022年就不支持patch和服务.(感慨Oracle 11 ...

  7. Bitter.Core系列七:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 示例 更新删除插入

    Bitter Orm 在操作数据库增删改的时候,支持模型驱动和直接执行裸SQL 操作,示例代码如下: 一:模型驱动(增删改) /// <summary> /// 插入,删除,更新示例(模型 ...

  8. e.next = nil // avoid memory leaks e.prev = nil // avoid memory leaks

    /Go/src/container/list/list.go:10

  9. 墓碑机制与 iOS 应用程序的生命周期

    ① 应用程序的状态 iOS 应用程序一共有 5 种状态: Not running:应用未运行 Inactive:应用运行在 foreground 但没有接收事件 Active:应用运行在 foregr ...

  10. .params和query的区别

    用法:query要用path来引入,params要用name来引入,接收参数都是类似的,分别是this.$route.query.name和this.$route.params.name.url地址显 ...