2019牛客多校第四场B xor——线段树&&线性基的交
题意
给你 $n$ 个集合,每个集合中包含一些整数。我们说一个集合表示一个整数当且仅当存在一个子集其异或和等于这个整数。现在你需要回答 $m$ 次询问 ($l, r, x$),是否 $l$ 到 $r$ 的每个集合都能表示 $x$.
分析
先求出每个集合的线性基,然后用线段树维护线性基的交,详见代码
#include<bits/stdc++.h>
#define reg register
using namespace std;
typedef long long ll; const int bits = ;
const int maxn = + ;
int n, q;
ll a[maxn][]; struct LBase {
//const static int bits = 31; //0~31位
ll d[bits+], tmp[bits+]; //线性基
bool flag = false; //记录是否cnt < n
LBase() {memset(d, , sizeof d);}
void insert(ll x)
{
for(int i=bits;~i;i--)
if(x&(1ll<<i))
{
if(!d[i]){ d[i]=x; break;}
else x ^= d[i];
}
flag = true;
}
bool check(ll x) //返回true表示已经能被表示
{
for(int i=bits;~i;i--)
if(x&(1ll<<i))
{
if(!d[i]) return false;
else x ^= d[i];
}
return true;
}
ll qmax(ll res=)
{
for(int i=bits;~i;i--)
res=max(res,res^d[i]);
return res;
}
ll qmin()
{
if(flag) return ;
for(int i=;i<=bits;i++)
if(d[i]) return d[i];
}
ll query(ll k) //查询第k小
{
ll res=; int cnt=;
k-=flag; if(!k)return ;
for(int i=;i<=bits;i++){
for(int j=i-;~j;j--)
if(d[i]&(1ll<<j)) d[i]^=d[j];
if(d[i]) tmp[cnt++]=d[i];
}
if(k>=(1ll<<cnt))return -;
for(int i=;i<cnt;i++)
if(k&(1ll<<i)) res^=tmp[i];
return res;
}
void merge(const LBase &a) { //求并集
for (int i = bits; i >= ; --i)
if (a.d[i]) insert(a.d[i]);
}
}; LBase intersection(const LBase &a,const LBase &b) //求交集
{
LBase ans, c=b,d=b;
for(int i = ;i <= bits;i++)
{
ll x =a.d[i];
if(!x) continue;
int j=i;ll T=;
for(;j>=;--j)
if((x>>j)&)
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;
} struct SegTree
{
LBase b[maxn << ];
void build(int o, int L, int R)
{
int M = L + (R-L) / ;
if(L == R)
{
for(int i = ;i < ;i++) b[o].insert(a[L][i]);
}
else
{
build(*o, L, M);
build(*o+, M+, R);
//b[o].merge(b[2*o]); b[o].merge(b[2*o+1]);
b[o] = intersection(b[*o], b[*o+]);
}
} //查询[ql, qr]中是否都能表示出x
bool query(int o,int L,int R, int ql, int qr, ll x)
{
int M = L + (R - L) / ;
bool flag1 = true, flag2 = true;
if(ql <= L && R <= qr) return b[o].check(x);
if(ql <= M) flag1 = query(*o, L, M, ql, qr, x);
if(qr > M) flag2 = query(*o+, M+, R, ql, qr, x);
return flag1 && flag2;
} }seg; int main(){
scanf("%d%d", &n, &q);
for(int i = ;i <= n;i++)
{
int sz; scanf("%d", &sz);
for(int j = ;j < sz;j++) scanf("%lld", &a[i][j]);
for(int j=sz; j < ;j++) a[i][j] = ;
}
seg.build(, , n);
while(q--)
{
int l, r; ll x;
scanf("%d%d%lld", &l, &r, &x);
if(seg.query(, , n, l, r, x)) printf("YES\n");
else printf("NO\n");
}
return ;
}
2019牛客多校第四场B xor——线段树&&线性基的交的更多相关文章
- 2019牛客多校第四场B xor(线性基求交)题解
题意: 传送门 给\(n\)个集合,每个集合有一些数.给出\(m\)个询问,再给出\(l\)和\(r\)和一个数\(v\),问你任意的\(i \in[l,r]\)的集合,能不能找出子集异或为\(v\) ...
- 2019牛客多校第七场C-Governing sand(线段树+枚举)
Governing sand 题目传送门 解题思路 枚举每一种高度作为最大高度,则需要的最小花费的钱是:砍掉所有比这个高度高的树的所有花费+砍掉比这个高度低的树里最便宜的m棵树的花费,m为高度低的里面 ...
- 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数
目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...
- 2019牛客多校第四场 A meeting
链接:https://ac.nowcoder.com/acm/contest/884/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10485 ...
- 2019牛客多校第四场J free——分层图&&最短路
题意 一张无向图,每条边有权值,可以选择不超过 $k$ 条路使其权值变成0,求 $S$ 到 $T$ 的最短路.(同洛谷 P4568) 分析 首先,分层图最短路可以有效解决这种带有 「阶段性」的最短路, ...
- 2019牛客多校第四场A meeting——树的直径
题意: 一颗 $n$ 个节点的树上标有 $k$ 个点,找一点使得到 $k$ 个关键结点的最大距离最小. 分析: 问题等价于求树的直径,最小距离即为直径除2向上取整. 有两种求法,一是动态规划,对于每个 ...
- [2019牛客多校第四场][G. Tree]
题目链接:https://ac.nowcoder.com/acm/contest/884/G 题目大意:给定一个树\(A\),再给出\(t\)次询问,问\(A\)中有多少连通子图与树\(B_i\)同构 ...
- 2019牛客多校第四场D-triples I 贪心
D-triples 题意 给你一个\(n\),问至少有几个数或运算起来可以等于\(n\),并且输出数量和这个几个数.题目说明给的\(n\)一定符合条件(不会输出\(n= 1\) 之类不存在情况). 思 ...
- 2019牛客多校第四场C-sequence(单调栈+线段树)
sequence 题目传送门 解题思路 用单调栈求出每个a[i]作为最小值的最大范围.对于每个a[i],我们都要乘以一个以a[i]为区间内最小值的对应的b的区间和s,如果a[i] > 0,则s要 ...
随机推荐
- Python--字典的一些用法dict.items()
1.dict.items() 例子1: 以列表返回可遍历的(键, 值) 元组数组. dict = {'Name': 'Runoob', 'Age': 7} print ("Value : % ...
- 『Python基础』第1节 Windows环境下安装Python3.x
一. Python安装 1. 下载安装包 https://www.python.org/downloads/release/python-374/ # 3.7安装包 # 如需安装python2.7版本 ...
- SAS学习笔记15 SAS导入数据(import txt csv xlsx spss)
- jira索引失败
""" # 参考:http://www.mamicode.com/info-detail-2369087.html jira断电重启后索引失败, 解决方法: 关闭jira ...
- C#对象转换工具类
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...
- pycharm2019.2永久激活
Pycharm2019.2永久激活Pycharm官网在不到两个月内与2019.7.24更新到最新版本pycharm2019.2,不可说更新不快,对于"喜新厌旧"的我怎能错过新版本呢 ...
- java如何在不访问数据库就可以对list分页?
废话不多说,直接上代码 import java.util.ArrayList; import java.util.List; public class demo { public static voi ...
- [Vuex系列] - Actions的理解之我见
Actions如何定义的 恕小端不才,对Action的总结如下: Action 可以提交mutation方法,通过mutation来改变state Action 函数可以接收一个context对象,通 ...
- BPM软件_财务报销流程管理解决方案_K2工作流引擎
财务报销,对任何企业都是日常运营中重要的一个环节.但报销流程周期长,反复签字手续繁杂,报销过程不透明 ,单据归档保存.检索困难等问题也让员工头疼.为了解决这些困扰,财务报销流程电子化一时成为热门之选. ...
- macOS 在终端中使用 adb命令,每次都要source ~/.bash_profile 才生效
macOS下已经配置好Android开发环境,环境变量也添加了,但是在终端中使用adb命令每次都需要source .bash_profile之后才能识别, 否则就提示 zsh: command no ...