题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169

题意:有n个数和m次查询,每次查询区间[l, r]问满足ai ^ ai+1 ^ ... ^ aj == k的(i, j) (l <= i <= j <= r)有多少对。

思路:离线做。首先预处理前缀异或和sum[],那么ai ^ ... ^ aj == sum[i-1] ^ sum[j]。

这样对一次查询[l, r]的处理,可以从左到右扫一次,统计k ^ sum[i]出现的次数(l <= i <= r)。

假设已经处理到[L, R],对下一次的[l, r]处理——

若L < l,显然多余,需要去掉[L, l-1]的部分,若L > l需要加上[l, L-1]的部分,反之不需要处理。

若R > r,..................[r+1, R]......,若R < r........[R+1, r]......,..............。

用莫队做即可。

#include <bits/stdc++.h>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
LL pos[maxn], c[maxn], s[maxn], cnt[<<], sum[maxn], out_ans[maxn];
LL n, m, ans, k;
struct node
{
LL r, l, id, res;
}Node[maxn]; int cmp(node a, node b)
{
if(pos[a.l] == pos[b.l])
return a.r < b.r;
return a.l < b.l;
} int cmp_id(node a, node b)
{
return a.id < b.id;
} void add(int x)
{
ans += cnt[sum[x]^k]; // 右区间拓展时: 这样就保证了c[i]^k 是在前面出现过的 因为如果没有出现过 则cnt对应的值为0 左区间同理
cnt[sum[x]]++;
} void dec(int x)
{
cnt[sum[x]]--; //右区间缩小时: 防止c[i]^k 是后边的 左区间同理
ans -= cnt[sum[x]^k]; } int main()
{
scanf("%lld%lld%lld", &n, &m, &k);
for(int i=; i<=n; i++)
{
scanf("%lld", &c[i]);
sum[i] = sum[i-] ^ c[i];
}
int block = sqrt(n);
for(int i=; i<=n; i++)
pos[i] = (i-)/block + ;
for(int i=; i<=m; i++)
{
scanf("%lld%lld", &Node[i].l, &Node[i].r);
Node[i].id = i;
Node[i].l--; //预处理
}
sort(Node+, Node+m+, cmp);
cnt[] = ;
for(int i=, l=, r=; i<=m; i++)
{
for(; r < Node[i].r; ++r)
add(r+);
for(; r > Node[i].r; r--)
dec(r);
for(; l < Node[i].l; ++l)
dec(l);
for(; l > Node[i].l; --l)
add(l-); Node[i].res = ans;
}
sort(Node+, Node+m+, cmp_id); for(int i=; i<=m; i++)
printf("%I64d\n",Node[i].res); return ;
}

XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)的更多相关文章

  1. XOR and Favorite Number Codeforces - 617E || [CQOI2018]异或序列

    https://www.luogu.org/problemnew/show/P4462 http://codeforces.com/problemset/problem/617/E 这个是莫队裸题了吧 ...

  2. XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和

    CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! =  j) ...

  3. XOR and Favorite Number CodeForces - 617E

    a[i]^a[i+1]--a[j]=k; 处理前缀和pre[i] 那么上式可以表示为pre[i-1]^pre[j]=k; #include<bits/stdc++.h> using nam ...

  4. Codeforces 877F Ann and Books 莫队

    转换成前缀和, 预处理一下然后莫队. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...

  5. codeforces 940F 带修改的莫队

    F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...

  6. Codeforces D. Powerful array(莫队)

    题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...

  7. Codeforces 86D Powerful array (莫队)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  8. codeforces 86D,Powerful array 莫队

    传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...

  9. CodeForces - 86D D. Powerful array —— 莫队算法

    题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...

随机推荐

  1. spring-boot2.x Application properties属性配置

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  2. MOD 模除运算符

    用于奇数和偶数的校验,星期几的计算,以及其它专门的计算.

  3. Shader食谱 Chapter3--Toonshader卡通效果

    Shader食谱 Chapter3--Toonshader卡通效果 unity shader toon 卡通Shader  Shader食谱 Chapter3--Toonshader卡通效果 Over ...

  4. 关于springcloud的一些问题总结.txt

    @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCo ...

  5. 拦截器 应用详解--SpringMVC

    在实际项目中,拦截器的使用是非常普遍的,例如在购物网站中通过拦截器可以拦截未登录的用户,禁止其购买商品,或者使用它来验证已登录用户是否有相应的操作权限等,Spring MVC提供了拦截器功能,通过配置 ...

  6. hive的优化

    hive.optimize.cp=true:列裁剪hive.optimize.prunner:分区裁剪hive.limit.optimize.enable=true:优化LIMIT n语句hive.l ...

  7. which命令详解

    基础命令学习目录首页 原文链接:https://www.cnblogs.com/jkin/p/10289085.html Linux which命令用于查找文件. which指令会在环境变量$PATH ...

  8. Homebrew -- 安装与使用

    使用 React Native,必须安装的依赖有:Node.Watchman 和 React Native 命令行工具以及 Xcode. 推荐使用Homebrew来安装 Node 和 Watchman ...

  9. 使用sass与compass合并雪碧图(二)

    上一篇文章介绍了怎样使用compass合并雪碧图,生成的icons.css文件中单位是px,PC端可以直接在html文件中使用,但在移动端,我们需要根据不同分辨率的屏幕,来缩放图片大小,显然使用px单 ...

  10. 面向对象OO第15次作业总结

    面向对象OO第15次作业总结 1.论述测试与正确性论证的效果差异,比较其优缺点测试通过大量测试数据来覆盖测试代码,比较直观,优点在于知道测的是啥,特别直观,缺点在于很难覆盖所有情况.正确性论证从逻辑关 ...