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 ...
随机推荐
- ubuntu查看'任务管理器'
ubuntu下的任务管理器打开方式:命令行输入'gnome-system-monitor'即可,展示如下:
- jsp jquery js 的基本路径获取
引子:js中需要当前页面的基础路径,获取不到request,可以通过如下方法来解决! 1.jsp基础路径,在jsp头部加上,获取基础路径http://localhost:8080/project/ ...
- github 用户不被识别问题
期末考完,继续开发. 用过的都知道,直接用的话贡献者上面显示不出自己. 查一下就知道是因为github的识别是靠邮箱设置的. 但是如果频繁创建新仓库,容易忘记设定用户名和邮箱. 突发奇想,发现 ...
- C. Unusual Product(cf)
http://codeforces.com/problemset/problem/405/C 题意: 给出一个n*n的矩阵,有q个操作,输入3时,输出A ,A等于第i行乘以第i列的对应元素的和(mod ...
- Akka源码分析-Remote-位置透明
上一篇博客中,我们研究了remote模式下如何发消息给远程actor,其实无论如何,最终都是通过RemoteActorRef来发送消息的.另外官网也明确说明了,ActorRef是可以忽略网络位置的,这 ...
- jQuery——节点操作
创建节点 1.$():创建一个li标签 $("<li class='aaa'>我是li标签</li>") 2.html():创建一个li标签并同时添加到ul ...
- Python三方库xlrd,xlwd-Excel读写
恩,我是翻译汪,主要内容来自http://www.python-excel.org/ 在xlrd,xlwt这两个库中,Excel的结构表示为workbook整个Excel对象,sheet工作表,row ...
- 【转载】HTTP 基础与变迁
原文地址:https://segmentfault.com/a/1190000006689489 HTTP HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于 ...
- 安装nodejs6.9x以后,原来在nodejs4.2.x中运行正常的ionic项目出现问题的解决
安装nodejs6.9x以后,原来在nodejs4.2.x中运行正常的程序出现的问题.看错误信息,由于NodeJs版本升级导致的. 到提示的目录下运行:npm rebuild node-sass -g ...
- Nginx 监控
编译安装加上http_stub_status_module 模块 location /status { stub_status on; # access_log /usr/local/nginx/lo ...