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> ...
随机推荐
- 项目差异class文件提取-->上线用
package fileReader; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStre ...
- UISplitViewController - iPad分屏视图控制器
UISplitViewController - 分屏视图控制器 概述 UISplitViewController 是一个容器vc, 展示一个 master-detail(主-详(从))界面. 主视图改 ...
- ThinkPHP入门(二)
smarty使用 smarty引入流程 1. 控制器IndexAction.class.php function index() $this -> display(); (父类Action的di ...
- Android Studio 配置
Android配置:[转]原地址:http://www.cnblogs.com/smyhvae/p/4022844.html [开发环境] 物理机版本:Win7旗舰版(64位) Android Stu ...
- python中的monkey-patching
这个技巧我很少用过. 但知道无防. 在运行时改变函数或类的行为, 一般用猴子补丁,原类,装饰器都可以实现. #!/usr/bin/env python # -*- coding: utf-8 -*- ...
- JAVA 使用线程的几种方式
之前放在自己网站上的例子,因为网站关闭,已经找不到了,想用的时候,没有的话又重新翻书是很麻烦的事情.所以重新记录一下,以备将来查看. 第一种,让任务类继承Runable接口,然后将任务类对象放入Thr ...
- MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏
我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...
- android 入门-android Studio 配置
重要:sdk 最好先有一个版本 19版本.build-tools 19.1.0 extras 19.0和platforms android-19 1.下载android sdk 和jdk 并配置环境变 ...
- 【leetcode】Candy
题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Outlook] 重新取得outlook中被禁止访问的文件
摘要:接收到老大的邮件,邮件中带有jar包,导致我无法接收到这个文件,outlook2010中提示:outlook禁止访问不安全……,相信很多人都遇到过这个问题,以前也遇到过,总没去想着解决这个问题, ...