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> ...
随机推荐
- 树形DP习题
听闻noip要考树形DP,本蒟蒻万分惶恐,特刷一坨题目,以慰受惊之心. codevs 1486 /*和非常出名的"选课"是一个题*/ #include<cstdio> ...
- c#三层架构登陆实例
很早之前,就听说过三层结构了.当时只知道 三层结构 是把 系统的 界面 跟 数据库操作等不相关的程序分别开来.原来这么简单的实现,确实传说中的 三层结构啊. 首先,先来看一下是哪三层.表示层(UI, ...
- Navicat 回复 psc 文件 Mysql
在mysql 中回复 psc文件 的时候 只能一步步来,先在navicat中建一个空数据库,然后点击有上角的备份==>回复备份==> 找到psc文件==> 注意此时不要急于点击 开始 ...
- android setCompoundDrawables 不显示问题
在 vh.tvAddr.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_real_state_loc), null, nul ...
- 恶趣味小游戏 I'm hungry
之前学算法的时候无聊做了个游戏放松放松,现在传到了github以免电脑坏了就永远丢失了... github地址:https://github.com/BenDanChen/IamHungry I am ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- C# DateTime时间格式转换为Unix时间戳格式
double ntime=dateTimeToUnixTimestamp(DateTime.Now); long g1 = GetUnixTimestamp(); long g2 = ConvertD ...
- PHPCMS_v9 wap不同列表采用不同模板的方法
.在phpcms\modules\wap\index.php中搜索 $template = ($TYPE[$typeid]['parentid']==0 && in_array($ty ...
- 手机web页面制作时的注意事项
一.手机页面的标准头规范 字符编码使用utf-:指定页面手机内存缓存中的存储时间段 device-width:通知浏览器使用设备的宽度作为可视区的宽度 initial-scale - 初始的缩放比例 ...
- 一个简单的Object Hook的例子(win7 32bit)
Object Hook简单的来说就是Hook对象,这里拿看雪上的一个例子,因为是在win7 32位上的,有些地方做了些修改. _OBJECT_HEADER: kd> dt _OBJECT_HEA ...