题意:很多询问,求每个询问下,有多少个区间,异或=k。

分析:异或也有前缀和。[L,R] = pre[R] ^ pre[L-1];

莫队算法:是莫涛队长发明的,一种改良版的暴力离线算法。

首先将问题重新排序,有生成树的,有简单版的分块,然后通过一个区间去递推另个一区间的值。

这里需要记录一下flag[ pre[i] ] 的个数。

#include <bits/stdc++.h>

using namespace std;

const int maxn = <<;
int a[maxn]; struct Node {
int l,r,id;
}Q[maxn]; int pos[maxn];
long long ans[maxn];
long long flag[maxn]; bool cmp(Node a,Node b) {
if(pos[a.l]==pos[b.l])
return a.r < b.r;
return pos[a.l] < pos[b.l];
} int n,m,k;
int L = ,R = ;
long long Ans; void add(int x) {
Ans+=flag[a[x]^k];
flag[a[x]]++;
} void del(int x) {
flag[a[x]]--;
Ans-=flag[a[x]^k];
} int main()
{
scanf("%d%d%d",&n,&m,&k);
int sz = sqrt(n);
for(int i=; i <= n; i++) {
scanf("%d",&a[i]);
a[i] = a[i]^a[i-];
pos[i] = i/sz;
} for(int i=; i<=m ;i++) {
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].id = i;
} sort(Q+,Q+m+,cmp);
flag[] = ; for(int i=; i <=m; i++) { while(L<Q[i].l) {
del(L-);
L++;
} while(L>Q[i].l) {
L--;
add(L-);
} while(R<Q[i].r) {
R++;
add(R);
} while(R>Q[i].r) {
del(R);
R--;
} ans[Q[i].id] = Ans;
} for(int i=; i <= m; i++)
printf("%I64d\n", ans[i]); return ;
}

E. XOR and Favorite Number的更多相关文章

  1. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  2. 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 ...

  3. 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 ...

  4. XOR and Favorite Number(莫队算法+分块)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  5. CF617E XOR and Favorite Number

    CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...

  6. XOR and Favorite Number (莫对算法)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 题解-CF617E XOR and Favorite Number

    题面 CF617E XOR and Favorite Number 给定 \(n,m,k\) 和 \(n\) 个数的序列 \(a_i\),\(m\) 次求区间 \([l,r]\) 中异或值为 \(k\ ...

随机推荐

  1. Jquery EasyUI Treegrid按需加载子集

    项目说明,要一个有权限并且按需加载的树形列表. jeasyui网址 CSS <!--添加树状控件--> <link rel="stylesheet" type=& ...

  2. (转)Linux SSH批量分发管理

    Linux SSH批量分发管理 原文:http://blog.51cto.com/chenfage/1831166 第1章 SSH服务基础介绍 1.1 SSH服务 1.1.1SSH介绍 SSH是Sec ...

  3. Linux Nginx环境安装配置redmine3.1

    作者博文地址:https://www.cnblogs.com/liu-shuai/ 环境: CentOS-6.5+Nginx-1.8.0+Redmine-3.1.1+Ruby-2.0 1.配置环境 1 ...

  4. 虚拟机xp系统中Oracle 10g的安装

    1 安装过程(11步) 2.如果是xp系统可以直接并双击解压目录下的setup.ext,出现安装界面,如下: 3.输入口令和确认口令,如:oracle,点击下一步,出现如下进度条. 注:此口令即是管理 ...

  5. C语言中extern的用法--转

    http://blog.sina.com.cn/s/blog_52deb9d50100ml6y.html 在C语言中,修饰符extern用在变量或者函数的声明前,用来说明“此变量/函数是在别处定义的, ...

  6. CF 304B——Calendar——————【年月日计算】

    B - Calendar Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  7. [转]ASP.NET Core / MVC 6 HttpContext.Current

    本文转自:http://www.spaprogrammer.com/2015/07/mvc-6-httpcontextcurrent.html As you know with MVC 6, Http ...

  8. 1、v1 与 v2的比较

    1.路由的迁移 /* --- v1 ----*/ .config(function($stateProvider){ $stateProvider .state('main', { url: '/', ...

  9. 在Android源码中如何吧so库打包编译进入apk, 集成第三方库(jar和so库)

    集成第三方so和jar包 include $(CLEAR_VARS) #jar包编译            LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES :=securit ...

  10. 深入理解 Java 内存模型(转载)

    摘要: 原创出处 http://www.54tianzhisheng.cn/2018/02/28/Java-Memory-Model/ 「zhisheng」欢迎转载,保留摘要,谢谢! 0. 前提 &l ...