CF617/E XOR and Favorite Number
题目链接:http://codeforces.com/contest/617/problem/E
题意:给出一个长度为n(1e5)的序列,有m(1e5)次操作,每次操作选择一个L-R区间,然后输出符合条件的i,j的个数:L<i<j<R,ai^a------^aj=k,k是一个给定的值
题解:莫队可做,首先,求一个前缀异或和,如果求ai^……^a[j]的值,那么sum[j]^sum[i-1]。莫队的思想是已知道[L,R]的值,可以在O(1)的时间内算出[L-1,R],[L,R-1]等等的值,
好,现在如果我们维护[L-R]区间,所有{sum[L-1],sum[R-1]}和{sum[L,sum[R]},去除中间重复的部分,然后维护{sum[L-1],sum[R]},这样就方便维护所有的值了。
另外呢,优于是异或,所以呢,中间异或的值很可能会大于1e6,所以标记数组要开的大一点。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ( << ) - ;
int A[maxn], pos[maxn], sz = ;
LL num[maxn], Ans[maxn], ans = ;
struct Edge
{
int l, r, id;
} E[maxn];
bool cmp (Edge a, Edge b)
{
if(pos[a.l] == pos[b.l]) return a.r < b.r;
return pos[a.l] < pos[b.l];
}
int L = , R = ;
int N, M, k;
void add(int x)
{
ans += num[A[x] ^ k];
num[A[x]]++;
}
void det(int x)
{
num[A[x]]--;
ans -= num[A[x] ^ k];
}
int main ()
{
scanf("%d %d %d", &N, &M, &k);
for(int i = ; i <= N; i++)
{
scanf("%lld", &A[i]);
A[i] ^= A[i - ];
pos[i] = i / sz;
}
for(int i = ; i <= M; i++)
{
scanf("%d %d", &E[i].l, &E[i].r);
E[i].id = i;
}
num[] = ;
sort(E + , E + + M, cmp);
for(int i = ; i <= M; i++)
{
while(L < E[i].l)
{
det(L - );
L++;
}
while(L > E[i].l)
{
L--;
add(L - );
}
while(R < E[i].r)
{
R++;
add(R);
}
while(R > E[i].r)
{
det(R);
R--;
}
Ans[E[i].id] = ans;
}
for(int i = ; i <= M; i++) printf("%lld\n", Ans[i]);
return ;
}
CF617/E XOR and Favorite Number的更多相关文章
- 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 ...
- 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 ...
- XOR and Favorite Number(莫队算法+分块)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- CF617E XOR and Favorite Number
CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...
- XOR and Favorite Number (莫对算法)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- 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 ...
- 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 —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...
随机推荐
- python匹配某个中文字符
python2.7对中文的支持不好是众所周知的,现在遇到这样一个需求,要匹配某个中文字符.查了一个资料,思路就是转化为unicode进行比较,记录如下: line = '参考答案: A' # gbk ...
- 如何阻止自动更新‘updated_at’和'created_at'
可以在模版中添加一条代码: public $timestamps = false;
- spark作业提交参数设置(转)
来源:https://www.cnblogs.com/arachis/p/spark_parameters.html 摘要 1.num-executors 2.executor-memory 3.ex ...
- nc-使用方法
nc-远程克隆硬盘 A 接收端: nc -lp 333 | dd of=/dev/sda #用nc开启333监听端口 将收到的数据 写入到sda的硬盘上(前提是挂一块硬盘) B ...
- MySQL指定使用某个索引查询语句
查询语句查询emp_no,所以先查询emp_no的索引使用primary select emp_no,salary from salaries use index(s_f_t) where emp_n ...
- Loadrunner脚本学习总结
1.1 web脚本录制选择Web(HTTP/HTML)协议: 注意录制脚本前选择如下协议: 1.2 脚本如果需要使用如下函数: web_reg_save_param.web_fin ...
- 浅谈Java中的hashcode方法(转)
原文链接:http://www.cnblogs.com/dolphin0520/p/3681042.html 浅谈Java中的hashcode方法 哈希表这个数据结构想必大多数人都不陌生,而且在很多地 ...
- hdu 5505(数论-gcd的应用)
GT and numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 冒泡法的算法最佳情况下的时间复杂度为什么是O(n)
我在许多书本上看到冒泡排序的最佳时间复杂度是O(n),即是在序列本来就是正序的情况下. 但我一直不明白这是怎么算出来的,因此通过阅读<算法导论-第2版>的2.2节,使用对插入排序最佳时间复 ...
- GO语言Windows下Liteide
今天用到了. 就学习一下. https://www.golangtc.com/t/56e7caf5b09ecc66b90000fe 在网上看了好多此类介绍,操作太麻烦,自己琢磨出来怎么配置了. 以Li ...