codeforces 617E. XOR and Favorite Number 莫队
给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k。
维护一个前缀异或值就可以了。
要注意的是 区间[l, r], 我们需要将pre[l-1]......pre[r]都加进去, pre[l-1]不能少。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
long long n, m, k, sum[maxn], ans[maxn], tmp, num[maxn*];
struct node
{
int l, r, block, id;
bool operator < (node a) const
{
if(block == a.block)
return r<a.r;
return block < a.block;
}
}q[maxn];
void add(int x) {
tmp += num[x^k];
num[x]++;
}
void del(int x) {
num[x]--;
tmp -= num[x^k];
}
int main()
{
cin>>n>>m>>k;
int BLOCK = sqrt(n*1.0);
for(int i = ; i<=n; i++) {
scanf("%I64d", &sum[i]);
sum[i] ^= sum[i-];
}
for(int i = ; i<m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].block = q[i].l/BLOCK;
q[i].id = i;
}
num[] = ;
sort(q, q+m);
int l = , r = ;
for(int i = ; i<m; i++) {
while(r<q[i].r) {
add(sum[++r]);
}
while(r>q[i].r) {
del(sum[r--]);
}
while(l<q[i].l) {
del(sum[l-]);
l++;
}
while(l>q[i].l) {
l--;
add(sum[l-]);
}
ans[q[i].id] = tmp;
}
for(int i = ; i<m; i++)
cout<<ans[i]<<endl;
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 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 E. XOR and Favorite Number(莫队算法)
题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...
- CodeForces - 617E XOR and Favorite Number (莫队+前缀和)
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
- XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)
题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...
- 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 ...
随机推荐
- javascript高级知识点——内置对象原型
代码信息来自于http://ejohn.org/apps/learn/. 可以修改内置对象的方法. if (!Array.prototype.forEach) { Array.prototype.fo ...
- 不能以方法的方式使用不可调用的“system.web.httprequest.querystring”
问题描述:不能以方法的方式使用不可调用的“system.web.httprequest.querystring”解决办法:Request.QueryString["GoodsID" ...
- Login failed for user 'NT AUTHORITY\NETWORK SERVICE'的解决方法
1.打开SQL Server Manegement Studio 2.在 Security - logins 中 NETWORK SERVICE 3.双击该用户 Server Roles 中 勾选 ...
- GCD 续集
1.延迟执行 1.1.perform... // 3秒后自动回到当前线程调用 self 的 download: 方法,并且传递参数:@“http://xxx.jpg” [self performSel ...
- JavaScript、JSP、Java及javaEE
对JavaScript.JSP.Java及javaEE之间区别的理解 JavaScript和Java名字极为类似,相信不少的初学者或者准备学这些知识的人对于JavaScript.JSP.Java及Ja ...
- JS精粹(二)
这章主要讲语法,DC使用了表示形式语法的巴克斯范式图,这的确比语言来得准确简洁.我只想表达一个问题:表达式与表达式语句的关系.因为其他问题很明白. 从DC的巴科斯范式图上大致可以认为,表达式是比表达式 ...
- CEvent,CSemaphore,CCriticalSection,CMutex
一.用CEvent实现线程同步 事件对象(Event)是最简单的同步对象,它包括有信号和无信号两种状态.在线程访问某一资源之前,也许需要等待某一事件的发生,这时用事件对象最合适.例如,只有在通信端口缓 ...
- English words
英语指路常用单词 the one-way street单行道traffic light红绿灯 fork road三叉路口intersection/crossroad 十字路口T road 丁字路口in ...
- python3.5之输出HTML实体字符
出 关① 徐兰 凭山俯海古边州, 旆②影风翻见戍楼. 马后桃花马前雪,出关争得不回头? [注]关,指居庸关.②旆(pèi),旌旗. 刚刚学习用python写爬虫,实战一下. 抓取出一个网页的内容 ...
- Java判断字符串是否为空的三种方法
方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低.1: if(s == null || s.equals("")); 方法二: 比较字符串长度, 效率高, 是我知道的最 ...