51Nod1577 异或凑数 线性基 构造
国际惯例的题面:
异或凑出一个数,显然是线性基了。
显然我们能把区间[l,r]的数全都扔进一个线性基,然后试着插入w,如果能插入,则说明w不能被这些数线性表出,那么就要输出"NO"了。
然而怎么得到这个线性基?我们有两种很显然的暴力:线段树和单调莫队。然而亲测它们都不能AC......(不排除我写丑了)
考虑思考一下性质:如果我们能对于每个结束位置,用这个位置前面尽可能靠后的数构造出一个线性基,那么我们查询的时候是不是就能取出结束位置为r的线性基限制用的数出现位置不能早于l,然后直接查询就好了呢?(这种思想在Bzoj3514: Codechef MARCH14 GERALD07加强版)中也用到过。
好,如果这样做的话,怎么从结束位置为i-1的线性基得到结束位置为i的线性基呢?
我们可以把结束位置为i-1的线性基拆了,然后贪心按照出现位置从大到小插入,正确性显然,复杂度O(900n)。
查询的时候怎么办?我们把结束位置为r的线性基中出现位置>l的数拆出来,插入一个新线性基,进行查询,复杂度O(900q)。
非常不幸的是,这样仍然不能AC。虽然我已经千方百计卡常数了。
我们现在在维护出现最晚的线性无关的30个数,考虑我们维护序列最大的30个数怎么维护。
只有30个数,我们再写个堆(priority_queue?假装你是Pascal党好了)什么的显然不必要了。直接在插入的时候从大到小进行一轮冒泡排序,用当前的大数替换小数就好。
对于这个线性基,我们也能这样做。
我们从高位向低位扫描这个线性基,如果我们当前的数能被放入某个位置的话,如果这个位置为空,则直接放入;否则比较出现位置,如果当前数出现较为靠后的话,则把当前数和这个位置的数swap一下,然后从下一位继续进行插入。
然后我们会发现这样构造的线性基还会有另一个更好的性质:高位上的数出现尽量靠后。
查询的时候,从高位到低位进行查询。如果凑出w需要高位的某个数而这个数出现位置<l,那么这一位无论如何都消不掉了,直接输出'NO'即可。
这样我们就把复杂度优化为了O(30n+30q),能够轻松AC。
暴力线段树代码:
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#include<cstdio>
#include<cstring>
#include<cctype>
#define bool unsigned char
typedef unsigned int ui;
const int maxn=,maxl=;
ui in[maxn>>],bit[maxl];
struct LinearBase {
ui dat[maxl];
__inline const ui& operator [] (const ui &x) const { return dat[x]; }
__inline bool insert(ui x) {
for(ui i=;~i;i--) if( x & bit[i] ) {
if( !dat[i] ) return dat[i] = x , ;
else x ^= dat[i];
}
return ;
}
__inline void merge(const LinearBase &r) {
for(ui i=;~i;i--) if( r[i] ) insert(r[i]);
}
__inline void reset() {
memset(dat,,sizeof(dat));
}
}ans;
struct SegmentTree {
LinearBase dat[maxn];
#define lson(pos) (pos<<1)
#define rson(pos) (pos<<1|1)
__inline void build(ui pos,ui l,ui r) {
if( l == r ) return void(dat[pos].insert(in[l]));
const ui mid = ( l + r ) >> ;
build(lson(pos),l,mid) , build(rson(pos),mid+,r);
dat[pos] = dat[lson(pos)] , dat[pos].merge(dat[rson(pos)]);
}
__inline void query(ui pos,ui l,ui r,const ui &ll,const ui &rr) {
if( ll <= l && r <= rr ) return ans.merge(dat[pos]);
const ui mid = ( l + r ) >> ;
if( ll <= mid ) query(lson(pos),l,mid,ll,rr);
if( mid < rr ) query(rson(pos),mid+,r,ll,rr);
}
}sgt;
__inline unsigned char nextchar() {
static const ui BS = << ;
static unsigned char buf[BS],*st,*ed;
if( st == ed ) ed = buf + fread(st=buf,,BS,stdin);
return st == ed ? : *st++;
}
__inline ui getint() {
ui ret = ;
unsigned char ch;
while( !isdigit(ch=nextchar()) ) ;
do ret=ret*+ch-''; while( isdigit(ch=nextchar()) );
return ret;
}
int main() {
static ui n,q,l,r,w;
n = getint();
for(ui *st=in+,*ed=st+n;st!=ed;*st++=getint());
for(ui i=;i<;i++) bit[i] = << i;
sgt.build(,,n) , q = getint();
while(q--) {
l = getint() , r = getint() , w = getint() , ans.reset();
sgt.query(,,n,l,r);
puts(ans.insert(w)?"NO":"YES");
}
return ;
}
暴力重构线性基:
#pragma GCC optimize("Ofast")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#define debug cout
using namespace std;
const int maxn=5e5+1e2,maxl=;
const int inf=0x3f3f3f3f;
int in[maxn],seq[maxl+],sql;
struct LinearBase {
int dat[maxl],bel[maxl];
inline bool insert(int id,int val) {
for(int i=;~i;i--) if( val & ( << i ) ) {
if( !dat[i] ) {
dat[i] = val , bel[i] = id;
return ;
} else val ^= dat[i];
}
return ;
}
inline int findremove(int val) {
int mx = inf;
for(int i=;~i;i--) if( bel[i] && ( val & ( << i ) ) ) mx = min( mx , bel[i] );
return mx;
}
inline void output(int lim) {
for(int i=;~i;i--) if( bel[i] >= lim ) seq[++sql] = bel[i];
}
inline bool insert(int val) {
for(int i=;~i;i--) if( val & ( << i ) ) {
if( !dat[i] ) return dat[i] = val , ;
else val ^= dat[i];
}
return ;
}
inline void reset() {
memset(dat,,sizeof(dat)) , memset(bel,,sizeof(bel));
}
}lb[maxn],tp;
inline char nextchar() {
static const int BS = << ;
static char buf[BS],*st,*ed;
if( st == ed ) ed = buf + fread(st=buf,,BS,stdin);
return st == ed ? : *st++;
}
inline int getint() {
int ret = ;
char ch;
while( !isdigit(ch=nextchar()) ) ;
do ret=ret*+ch-''; while( isdigit(ch=nextchar()) );
return ret;
}
int main() {
static int n,q,l,r,w;
n = getint();
for(int i=;i<=n;i++) {
lb[i] = lb[i-];
if( !lb[i].insert(i,in[i]=getint()) ) {
sql = , lb[i-].output() , lb[i].reset();
lb[i].insert(i,in[i]) , std::sort(seq+,seq++sql);
for(int j=sql;j;j--) lb[i].insert(seq[j],in[seq[j]]);
}
}
q = getint();
while(q--) {
l = getint() , r = getint() , w = getint();
sql = , lb[r].output(l) , tp.reset();
for(int i=;i<=sql;i++) tp.insert(in[seq[i]]);
puts(tp.insert(w)?"NO":"YES");
}
return ;
}
正解代码:
#pragma GCC optimize("Ofast")
#include<cstdio>
#include<algorithm>
#include<cctype>
const int maxn=5e5+1e2,maxl=;
const int inf=0x3f3f3f3f;
int in[maxn],seq[maxl+],sql;
bool vis[maxn];
struct LinearBase {
int dat[maxl],bel[maxl];
inline void insert(int id,int val) {
for(int i=;~i;i--) if( val & ( << i ) ) {
if( !dat[i] ) {
dat[i] = val , bel[i] = id;
return;
} else {
if( bel[i] < id ) std::swap(bel[i],id) , std::swap(val,dat[i]);
val ^= dat[i];
}
}
}
inline bool query(int val,int lim) {
for(int i=;~i;i--) if( val & ( << i ) ) {
if( !dat[i] || bel[i] < lim ) return ;
val ^= dat[i];
}
return ;
}
}lb[maxn],tp;
inline char nextchar() {
static const int BS = << ;
static char buf[BS],*st,*ed;
if( st == ed ) ed = buf + fread(st=buf,,BS,stdin);
return st == ed ? : *st++;
}
inline int getint() {
int ret = ;
char ch;
while( !isdigit(ch=nextchar()) ) ;
do ret=ret*+ch-''; while( isdigit(ch=nextchar()) );
return ret;
}
int main() {
static int n,q,l,r,w;
n = getint();
for(int i=;i<=n;i++) lb[i] = lb[i-] , lb[i].insert(i,in[i]=getint());
q = getint();
while(q--) {
l = getint() , r = getint() , w = getint(), puts(lb[r].query(w,l)?"NO":"YES");
}
return ;
}
明けの星が
启明的星辰
光を見せた
洒下丝丝光芒
よわい よわい
微微地 微微地
こころに降りそそぐ
纷纷洒落到我心房
でも時は
可有的时候
おなじ想いだけを
如果能够继续
あ…描き続ければ
描绘同样的思念…
誰も知らず
谁也不知道
日が射すまでに
直到太阳升起
そっと そっと
静静地 静静地
予感を感じてた
我感觉到一丝预感
こころが感じてた
我的心 有了一丝预感
51Nod1577 异或凑数 线性基 构造的更多相关文章
- 51Nod1577 异或凑数 线性基
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1577.html 题意 给定一个长度为 n 的序列. 有 m 组询问,每一组询问给出 L,R,k ,询 ...
- 51nod 1577 异或凑数 线性基的妙用
\(OTZgengyf\)..当场被吊打\(QwQ\) 思路:线性基 提交:\(3\)次 错因:往里面加数时\(tmp.p\)与\(i\)区分不清(还是我太菜了) 题解: 我们对每个位置的线性基如此操 ...
- [51nod1577]异或凑数
题目 点这里看题目. 分析 以下设\(k=\lfloor\log_2(\max a)\rfloor\). 关于异或凑数的问题自然可以用线性基处理,即如果可以插入到线性基,就说明无法凑出这个 ...
- 【XSY2701】异或图 线性基 容斥原理
题目描述 定义两个图\(G_1\)与\(G_2\)的异或图为一个图\(G\),其中图\(G\)的每条边在\(G_1\)与\(G_2\)中出现次数和为\(1\). 给你\(m\)个图,问你这\(m\)个 ...
- BZOJ 4671 异或图 | 线性基 容斥 DFS
题面 Description 定义两个结点数相同的图 G1 与图 G2 的异或为一个新的图 G, 其中如果 (u, v) 在 G1 与 G2 中的出现次数之和为 1, 那么边 (u, v) 在 G 中 ...
- 【loj114】k大异或和 线性基+特判
题目描述 给由 $n$ 个数组成的一个可重集 $S$ ,每次给定一个数 $k$ ,求一个集合 $T⊆S$ ,使得集合 $T$ 在 $S$ 的所有非空子集的不同的异或和中,其异或和 $T_1 ...
- LOJ.114.K大异或和(线性基)
题目链接 如何求线性基中第K小的异或和?好像不太好做. 如果我们在线性基内部Xor一下,使得从高到低位枚举时,选base[i]一定比不选base[i]大(存在base[i]). 这可以重构一下线性基, ...
- bzoj 2115 [Wc2011] Xor 路径最大异或和 线性基
题目链接 题意 给定一个 \(n(n\le 50000)\) 个点 \(m(m\le 100000)\) 条边的无向图,每条边上有一个权值.请你求一条从 \(1\)到\(n\)的路径,使得路径上的边的 ...
- LOJ #113. 最大异或和 (线性基)
题目链接:#113. 最大异或和 题目描述 这是一道模板题. 给由 \(n\) 个数组成的一个可重集 \(S\),每次给定一个数 \(k\),求一个集合 \(T \subseteq S\),使得集合 ...
随机推荐
- WEBSHELL恶意代码批量提取清除工具
场景 使用D盾扫描到WEBSHELL后可以导出有路径的文本文件. 最后手动去把WEBSHELL复制到桌面然后以文件路径命名,挨个删除. D盾界面是这样的. 手动一个个找WEBSHELL并且改名效率太低 ...
- C/C++杂记:虚函数的实现的基本原理
1. 概述 简单地说,每一个含有虚函数(无论是其本身的,还是继承而来的)的类都至少有一个与之对应的虚函数表,其中存放着该类所有的虚函数对应的函数指针.例: 其中: B的虚函数表中存放着B::foo和B ...
- 强大的Js树型控件Dtree使用详解
http://www.lmwlove.com/ac/ID868 在学习文章之前,要学会看官方网站http://destroydrop.com/javascripts/tree.从官方页面你能知道:dt ...
- Python-JS (JS介绍~JS的基础数据类型)
目录一.JS语言介绍: 1.JS概念 2.JS组成 二.JS的三种存在位置(引入方式): 1.行间式: 2.内联式: 3.外联式: 三.JS出现的具体位置: 四.JS语法规范 五.JS中变量的定义 E ...
- webpack文件笔记
webpack.prod.conf.js里面的ExtractTextPlugin,把css文件提取出来,专门进行打包minify :压缩 依赖的第三方库打包到vendor.js里面 每次项目打包的时候 ...
- lr
Action(){ //获取响应结果 web_reg_save_param("system_code", "LB=system_code\":\"&q ...
- 【转】crontab实用手册
前言 crontab是Unix和Linux用于设置周期性被执行的指令,是互联网很常用的技术,很多任务都会设置在crontab循环执行,如果不使用crontab,那么任务就是常驻程序,这对你的程序要求比 ...
- Thread类中的join方法
package charpter06; //类实现接口public class Processor implements Runnable { // 重写接口方法 @Override public v ...
- ***LANMP镜像手册(Apache&Nginx)-lanmp-oneinstack
LANMP镜像手册(Apache&Nginx) Version PHP7.0.26 转自:http://docs.websoft9.com/xdocs/lanmp-oneinstack-im ...
- jsTree动态加载数据
Views代码 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="view ...