Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个。
莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减少对答案的贡献。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN (1<<20) int block;
struct Query{
int l,r,i;
bool operator<(const Query &q) const {
if(l/block==q.l/block) return r<q.r;
return l/block<q.l/block;
}
}query[]; int n,m,k,a[],sum[]; int cnt1[MAXN],cnt2[MAXN];
long long ans;
void left_insert(int i){
++cnt1[sum[i-]];
++cnt2[sum[i]];
ans+=cnt2[sum[i-]^k];
}
void left_remove(int i){
ans-=cnt2[sum[i-]^k];
--cnt1[sum[i-]];
--cnt2[sum[i]];
}
void right_insert(int i){
++cnt1[sum[i-]];
++cnt2[sum[i]];
ans+=cnt1[sum[i]^k];
}
void right_remove(int i){
ans-=cnt1[sum[i]^k];
--cnt1[sum[i-]];
--cnt2[sum[i]];
} long long res[]; int main(){
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=n; ++i){
scanf("%d",a+i);
sum[i]=sum[i-]^a[i];
}
for(int i=; i<m; ++i){
scanf("%d%d",&query[i].l,&query[i].r);
query[i].i=i;
} block=(int)(sqrt(n)+1e-);
sort(query,query+m); int l=,r=;
right_insert();
for(int i=; i<m; ++i){
while(l<query[i].l) left_remove(l++);
while(l>query[i].l) left_insert(--l);
while(r<query[i].r) right_insert(++r);
while(r>query[i].r) right_remove(r--);
res[query[i].i]=ans;
} for(int i=; i<m; ++i){
printf("%lld\n",res[i]);
}
return ;
}
Codeforces 617E XOR and Favorite Number(莫队算法)的更多相关文章
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- Codeforces 617E XOR and Favorite Number莫队
http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- codeforces 617E E. XOR and Favorite Number(莫队算法)
题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- CODEFORCES 340 XOR and Favorite Number 莫队模板题
原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...
- CodeForces 617E XOR and Favorite Number
莫队算法. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...
随机推荐
- vector的erase的用法
vector<string>::iterator it = v.erase(v.begin() + 3, v.begin() + 6); 可以直接从begin进行加减,比如我们要移除第3个 ...
- Caffe初试(一)win7_64bit+VS2013+Opencv2.4.10+CUDA6.5配置Caffe环境
折腾了几天,终于在windows系统上成功配置了Caffe环境,期间遇到了很多问题,每个问题的解决也都花了不少时间,查过挺多资料,感觉挺有意义,这里写篇博客记录一下. 原来我使用的CUDA版本是7.5 ...
- debug与release
因为在Debug中有ASSERT断言保护,所以要崩溃,而在Release优化中就会删掉ASSERT,所以会出现正常运行. void func() { char b[2]={0}; strc ...
- NYOJ题目112指数运算
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAs0AAAIICAIAAAAaCETRAAAgAElEQVR4nO3drW7jWtwv4PcmwnMhxb ...
- 1 mysql的安装
win10 总之前期的步骤大概有:1下载安装:2 安装好后配置环境变量:3:登陆数据库 1:安装 mysql有安装版和直接解压就可以用的,据说大神都是安装的直接解压的,但鉴于自己是小白,就整了个安装版 ...
- PHP常用函数大全。
php usleep() 函数延迟代码执行若干微秒. unpack() 函数从二进制字符串对数据进行解包. uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID. time_sleep_ ...
- Android Tab -- 使用Fragment、FragmentManager来实现
原文地址:http://blog.csdn.net/crazy1235/article/details/42678877 效果: 代码:https://github.com/ldb-github/La ...
- SQL学习笔记----更改SQL默认的端口号
1.SQLServer配置管理器----SQLServer网络配置----MSSQLSERVER的协议---TCP/IP(已启用)---IP地址 清空素有的IP,在IPALL下更改默认的端口: 2. ...
- Python 调试 PDB
出处:http://blog.163.com/gjx0619@126/blog/static/12740839320114995947700/ 完整 请参考:http://docs.python.or ...
- Ext.MessageBox消息框
Ext JS消息提示框主要包括:alert.confirm.prompt.show 1.Ext.MessageBox.alert() 调用格式: alert( String title, String ...