线性基求交(线段树)--牛客第四场(xor)
题意:
给你n个基,q个询问,每个询问问你能不能 l~r 的所有基都能表示 x 。
思路:
建一颗线性基的线段树,up就是求交的过程,按照线段树区间查询的方法进行check就可以了。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; struct node
{
long long base[]; void Init()
{
for(int i=;i<;++i)
base[i]=;
} bool check(long long x)
{
for(int i=;i>=;--i)
{
if(x&(1LL<<i))
{
if(!base[i])return ;
else x^=base[i];
}
}
return ;
} void Insert(long long x)
{
for(int i=;i>=;--i)
{
if(x>>i&)
{
if(base[i])
x^=base[i];
else
{
base[i]=x;
break;
}
}
}
}
}BASE[N<<],temp,v; void merge(const node &a,node &b,node &ans)
{
temp=v=a;
fo(i,,)
{
if(b.base[i])
{
long long x=b.base[i],now=;
int g=;
for(int j=;j>=;j--)
{
if(x>>j&)
{
if(!temp.base[j])
{
g=;
temp.base[j]=x;
v.base[j]=now;
break;
}
x^=temp.base[j];now^=v.base[j];
}
}
if(!g)
ans.Insert(now);
}
}
} void up(int rt)
{
merge(BASE[rt<<],BASE[rt<<|],BASE[rt]);
}
void Build(int l,int r,int rt)
{
if(l==r)
{
int sz;
long long x;
sc("%d",&sz);
fo(i,,sz)
{
sc("%lld",&x);
BASE[rt].Insert(x);
}
return;
}
int mid=(l+r)>>; Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
up(rt);
}
bool Query(int L,int R,long long V,int l,int r,int rt)
{
if(L<=l&&r<=R)
return BASE[rt].check(V);
int mid=(l+r)>>; if(L<=mid&&!Query(L,R,V,l,mid,rt<<))
return ;
if(R>mid&&!Query(L,R,V,mid+,r,rt<<|))
return ;
return ;
} int main()
{
int n,m;
sc("%d%d",&n,&m); Build(,n,);
fo(i,,m)
{
int l,r;
long long x;
sc("%d%d%lld",&l,&r,&x);
if(Query(l,r,x,,n,))
pr("YES\n");
else
pr("NO\n");
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
线性基求交(线段树)--牛客第四场(xor)的更多相关文章
- 线性基求交(2019牛客国庆集训派对day4)
题意:https://ac.nowcoder.com/acm/contest/1109/C 问你有几个x满足A,B集合都能XOR出x. 思路: 就是线性基求交后,有几个基就是2^几次方. #defin ...
- 笛卡尔树--牛客第四场(sequence)
思路: O(n)建一颗笛卡尔树,再O(n)dfs向上合并答案就行了. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include &l ...
- 2019牛客多校第四场B xor(线性基求交)题解
题意: 传送门 给\(n\)个集合,每个集合有一些数.给出\(m\)个询问,再给出\(l\)和\(r\)和一个数\(v\),问你任意的\(i \in[l,r]\)的集合,能不能找出子集异或为\(v\) ...
- POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...
- 带 sin, cos 的线段树 - 牛客
链接:https://www.nowcoder.com/acm/contest/160/D来源:牛客网 题目描述给出一个长度为n的整数序列a1,a2,...,an,进行m次操作,操作分为两类.操作1: ...
- 最短meeting路线(树的直径)--牛客第四场(meeting)
题意: 给你一棵树,树上有些点是有人的,问你选一个点,最短的(最远的那个人的距离)是多少. 思路: 其实就是树的直径,两遍dfs,dfs第二遍的时候遇到人就更新直径就行了,ans是/2,奇数的话+1. ...
- 分层最短路(牛客第四场)-- free
题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...
- %300为0的个数(牛客第四场)-- number
题意: 给你一串数,问你如题. 思路: 我不是这样的作法,从后往前,先取00,再算%3==0的个数,往前推的时候有递推关系: #define IOS ios_base::sync_with_stdio ...
- 3的倍数 或运算构造x(牛客第四场)-- triples I
题意: 给你一个数,希望你能用最少的3的倍数或运算成它,让你输出答案. 思路: 进制%3有规律,1.2.4.8.16%3是1.2.1.2.1 ... 利用这一点分情况取一些位合成一些数就是答案了. # ...
随机推荐
- Codeforces 1167 E Range Deleting 双指针+思维
题意 给一个数列\(a\),定义\(f(l,r)\)为删除\(a\)中所有满足\(l<=a_i<=r\)的数后的数列,问有多少对\((l,r)\),使\(f(l,r)\)是一个 ...
- jQuery文档操作之修改操作
replaceWith() 语法: $(selector).replaceWith(content); 将所有匹配的元素替换成指定的string.js对象.jQuery对象. // 将所有的h5标题替 ...
- AcWing:111. 畜栏预定(贪心 + 小根堆)
有N头牛在畜栏中吃草. 每个畜栏在同一时间段只能提供给一头牛吃草,所以可能会需要多个畜栏. 给定N头牛和每头牛开始吃草的时间A以及结束吃草的时间B,每头牛在[A,B]这一时间段内都会一直吃草. 当两头 ...
- JVM GC之对象生死
1.简述 在Java内存运行时区域的各个部分中,程序计数器.虚拟机栈.本地方法栈3个区域随着线程而生,随着线程而亡.栈中的栈帧随着方法的进入和退出而有条不紊的进行着入栈和出栈操作. 每个栈帧需要分配多 ...
- [ML] Gradient Boost
参考链接: 1. https://medium.com/@cwchang/gradient-boosting-%E7%B0%A1%E4%BB%8B-f3a578ae7205 2. https://zh ...
- Java基础__随机生成1~15之间不重复的数字
package text; import java.util.ArrayList; import java.util.List; public class Text { public static v ...
- Mybatis 实体类使用@Accessors(chain = true)注解时,对应的mapper xml 报错
去掉这个注解就行了 应该是 mybatis 会调用实体类的 getter setter 方法, 返回值可能会有所影响
- Laravel5.2中Eloquent与DB类的区别是什么?
要了解这些先看看关于数据库组件的那些事儿(就是 Eloquent ORM) 数据库组件大概分了三层: 数据库连接层 查询构造层 应用层 来看一下每一层有哪些东西,分别对应文档的哪一部分: 数据库连接层 ...
- Cannot use unsafe construct in safe context
https://stackoverflow.com/questions/25953887/how-to-use-unsafe-code-in-safe-contex I am not sure if ...
- spark streaming 6: BlockGenerator、RateLimiter
BlockGenerator和RateLimiter其实很简单,但是它包含了几个很重要的属性配置的处理,所以记录一下. ))) , SECONDS) From WizNote