XOR Queries
XOR Queries
时间限制: 1000ms 内存限制: 256M
给出一个长度为n的数组C,回答m个形式为(L,R,A,B)的询问,含义为存在多少个不同的数组下标k∈[L,R]满足C[k]⨁A≥B(式中⨁为异或运算)。
输入第一行为一个整数T,表示一共有T组测试数据。
对于每组测试数据:
第一行为两个整数n,m(1≤n,m≤50000)。
第二行为n个整数表示数组C(0≤C[i]≤109)。
接下来m行中,第i行有四个整数Li,Ri,Ai,Bi(1≤Li≤Ri≤n, 0≤Ai,Bi≤109)。
对于每次询问:输出一个整数表示满足条件的数组下标数目。
1
5 2
1 2 3 4 5
1 3 1 1
2 5 2 3
2
2
分析:区间问题很容易想到莫队;
然后有了区间内所有数,怎么查x^a>=b;
考虑异或,建01字典树插入删除查询即可;
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=5e4+;
int n,m,k,t,a[maxn],bel[maxn],tot,cnt,ans[maxn],ch[maxn*][],sz[maxn*],num;
long long ret;
struct node
{
int l,r,id,block,c,d;
bool operator<(const node&p)const
{
return block==p.block?r<p.r:block<p.block;
}
}qu[maxn];
void insert(int x)
{
int pos=;
for(int i=;i>=;i--)
{
int y=(x>>i&);
if(ch[pos][y]==)
{
ch[pos][y]=++num;
ch[num][]=ch[num][]=;
sz[ch[pos][y]]=;
}
sz[ch[pos][y]]++;
pos=ch[pos][y];
}
}
void del(int x)
{
int pos=;
for(int i=;i>=;i--)
{
int y=(x>>i&);
sz[ch[pos][y]]--;
pos=ch[pos][y];
}
}
int query(int x,int y)
{
int ret=,pos=;
for(int i=;i>=;i--)
{
int z=(x>>i&),w=(y>>i&);
if(w==)
{
pos=ch[pos][z^];
if(i==)ret+=sz[pos];
}
else ret+=sz[ch[pos][z^]],pos=ch[pos][z];
if(pos==)break;
}
return ret;
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
cnt=;
num=;
sz[]=;
tot=;
ch[][]=ch[][]=;
scanf("%d%d",&n,&m);
int sz=(int)sqrt(n);
for(i=;i<=n;i++)
{
bel[i]=tot;
if(++cnt==sz)tot++,cnt=;
}
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=m;i++)scanf("%d%d%d%d",&qu[i].l,&qu[i].r,&qu[i].c,&qu[i].d),qu[i].id=i,qu[i].block=bel[qu[i].l];
sort(qu+,qu+m+);
int l=,r=;
for(i=;i<=m;i++)
{
while(r < qu[i].r)
{
insert(a[++r]);
}
while(l > qu[i].l)
{
insert(a[--l]);
}
while(r > qu[i].r)
{
del(a[r--]);
}
while(l < qu[i].l)
{
del(a[l++]);
}
ans[qu[i].id]=query(qu[i].c,qu[i].d);
}
for(i=;i<=m;i++)printf("%d\n",ans[i]);
}
return ;
}
XOR Queries的更多相关文章
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) D. Something with XOR Queries
地址:http://codeforces.com/contest/872/problem/D 题目: D. Something with XOR Queries time limit per test ...
- XOR Queries(莫队+trie)
题目链接: XOR Queries 给出一个长度为nn的数组CC,回答mm个形式为(L, R, A, B)(L,R,A,B)的询问,含义为存在多少个不同的数组下标k \in [L, R]k∈[L,R] ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries
https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...
- 计蒜客15430 XOR Queries(Trie处理位运算问题)
题意: 给出一个长度为n的数组C,回答m个形式为(L, R, A, B)的询问, 含义为存在多少个不同的数组下标k属于[L, R]满足C[k] XOR A >= B(式中XOR为异或运算). T ...
- codechef Chef And Easy Xor Queries
做法:我们考虑前缀异或和,修改操作就变成了区间[i,n]都异或x 查询操作就变成了:区间[1,x]中有几个k 显然的分块,每个块打一个tag标记表示这个块中所有的元素都异或了tag[x] 然后处理出这 ...
- codeforces 872 D. Something with XOR Queries(思维)
题目链接:http://codeforces.com/contest/872/problem/D 题意:给你一个排列p和对应的位置b也就是说p[b[i]]=i,然后给你最多询问2*n次找出所有的p排列 ...
- ACM ICPC Kharagpur Regional 2017
ACM ICPC Kharagpur Regional 2017 A - Science Fair 题目描述:给定一个有\(n\)个点,\(m\)条无向边的图,其中某两个点记为\(S, T\),另外标 ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- Codeforeces 617E XOR and Favorite Number(莫队+小技巧)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
随机推荐
- 选择排序(1)——简单选择排序(selection sort)
选择排序是一种很常见的排序算法,它需要对数组 中的元素进行多次遍历.每经过一次循环,选择最小的元素并把它放在靠近数组前端的位置. 代码实现: public static void selectionS ...
- [luogu_U15116]珈百璃堕落的开始
https://www.zybuluo.com/ysner/note/1239458 题面 给定\(n\)个二元组\((x,y)\),问有多少种方案,使得选出其中几个后,\(\sum x=\sum y ...
- bzoj3132
二维树状数组 树状数组什么的只支持修改单个数值,但是这道题要我们更新一个区域 盗图 就是这样,然后维护四个bit就行了 #include<bits/stdc++.h> using name ...
- [Swift通天遁地]三、手势与图表-(9)制作五彩缤纷的气泡图表
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- windows下 redis/tomcat 服务自启动
//设置redis服务自启动 //根据个人配置执行语句. redis-server --service-install redis.windows.conf --loglevel verbose ...
- Windows(7/8/10)搭建Elasticsearch 6.x版本
今天公司用到了Elasticsearch ,记录一下单机版搭建的流程. 首先我们来看下什么是Elasticsearch : ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分 ...
- centos源更新
.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup .下载新的CentOS-Base.r ...
- c++ 四种类型转换机制
类型转换机制可以分为:隐式类型转换 和 显示类型转换(强制类型转换) C中的类型转换: 事情要从头说起,这个头就是C语言.我们已经习惯了使用C-like类型转换,因为它强大而且简单. 主要有一下两种形 ...
- Glide4.0 centerCrop属性和圆角 冲突
首先致谢:https://blog.csdn.net/flyinbed_/article/details/75506062 咱们不是代码的生产者,只是代码的搬运工. 最近有个工作中有个需求就是展示的图 ...
- service里设置websocket心跳并向fragment发送数据
垃圾小白写了自己看的 /** * service 文件 */ public class SocketService extends Service { //自己定义接口用来传参 private sta ...