CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum
题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有出现偶数次的数异或的值。
思路:容易想到,把区间内的所有的数都异或得到的是出现奇数次的数的值,然后再异或该区间内的所有出现过的数(每个数只统计一次),得到的ans了。
第一个问题:得到询问区间的所有数的异或值,由 a[1~r] ^ a[0~(l-1)] = a[l~r] 可以用数组all_xor[i]保存a[1~i]区间的所有数的异或值,每次对询问区间的左右断点的all_xor值异或即可。
第二个问题:得到该区间内所有出现过的数的异或值,离线树状数组。具体操作如下:
先按照询问的右区间保存所有的询问g[r].(l, id),当前询问的结果保存在ans[id]里。
用树状数组one_xor[i]保存a[1~i]区间所有出现过的数(只统计一次)的异或值。遍历a[i],如果a[i]出现过了,对于后面的询问,右区间一定是>=i的,即i之前的区间都已经不关心这个值是否出现过了,
add_xor(mp[a[i]], a[i]),使前面的区间不再有这个数。然后mp[a[i]] = i , add_xor(mp[a[i]], a[i]) ; 这样保证了后面的每次询问都只在该最近一次出现过a[i]值的地方找到a[i]。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <map>
#define maxn 1000010
using namespace std; int a[maxn], one_xor[maxn], ans[maxn], all_xor[maxn]; //分别对应 原数组 (0~i)所有出现过的数的异或 保存ans (0~i)所有的数的异或 struct Query {
int l, id;
}; map<int, int>mp;
vector<Query> query[maxn];
int n, m; void init() {
mp.clear();
for (int i=1; i<=maxn; ++i) {
query[i].clear();
}
memset(one_xor, 0, sizeof(one_xor));
} void add_xor(int x, int val) {
for (; x<=n; x+=(x&(-x)))
one_xor[x] ^= val;
} int sum_xor(int l, int r) {
int ans = 0;
for (; r>0; r-=(r&(-r))) ans ^= one_xor[r];
for (; l>0; l-=(l&(-l))) ans ^= one_xor[l];
return ans;
} int main(){
//freopen("in.cpp", "r", stdin);
while(~scanf("%d", &n)) {
init();
all_xor[0] = 0;
for (int i=1; i<=n; ++i) {
scanf("%d", &a[i]);
all_xor[i] = all_xor[i-1]^a[i];
}
scanf("%d", &m);
for (int i=1; i<=m; ++i) {
int l, r;
scanf("%d%d", &l, &r);
query[r].push_back({l, i});
} for (int i=1; i<=n; ++i) {
if (mp.count(a[i])) {
add_xor(mp[a[i]], a[i]);
}
mp[a[i]] = i;
add_xor(mp[a[i]], a[i]); for (int j=0; j<query[i].size(); ++j) {
Query now = query[i][j];
ans[now.id] = (all_xor[i] ^ all_xor[now.l-1]);
ans[now.id] ^= sum_xor(i, now.l-1);
}
} for (int i=1; i<=m; ++i) {
printf("%d\n", ans[i]);
}
}
return 0;
}
CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组的更多相关文章
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)
转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树
题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum 树状数组+离线
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum
题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...
随机推荐
- Myeclipse优化篇
1 . window-preferences-MyEclipse Enterprise Workbench-Maven4MyEclipse-Maven ,将 Maven JDK 改为电脑上安装的 JD ...
- 疯狂java讲义之流程控制与数组
while package ch4; /** * Created by Jiqing on 2016/11/6. */ public class While { public static void ...
- 共享内存 最快IPC 的原因
参考 http://www.360doc.com/content/13/0817/11/7377734_307777806.shtml 我的理解,这里指的是用户进程的用户态空间和内核空间,也就是那个3 ...
- 【转载】高性能IO设计 & Java NIO & 同步/异步 阻塞/非阻塞 Reactor/Proactor
开始准备看Java NIO的,这篇文章:http://xly1981.iteye.com/blog/1735862 里面提到了这篇文章 http://xmuzyq.iteye.com/blog/783 ...
- android模拟器中文乱码
问题:在xml文件中设置的中文能正确输出,但是在java文件中设置的中文会在模拟器上呈现乱码 解决方案:在build.gradle文件中添加一行代码 android {compileOptions.e ...
- 转:C++语言的15个晦涩特性
转自 http://blog.jobbole.com/54140/ 操作符重载和检查顺序 重载,(逗号),||或者&&操作符会引起混乱,因为它打破了正常的检查规则.通常情况下,逗号操作 ...
- C#_接口
.Net提供了接口,这个不同于Class或者Struct的类型定义.接口有些情况,看似和抽象类一样,因此有些人认为在.Net可以完全用接口来替换抽象类.其实不然,接口和抽象类各有长处和缺陷,因此往往在 ...
- mysql优化(三)–explain分析sql语句执行效率
mysql优化(三)–explain分析sql语句执行效率 mushu 发布于 11个月前 (06-04) 分类:Mysql 阅读(651) 评论(0) Explain命令在解决数据库性能上是第一推荐 ...
- Java内部接口的调用方式
package com.hs.review; public class Person { public static void main(String[] args) { Person p1 = ne ...
- Android SQlite详解
在项目开发中,我们或多或少都会用到数据库.在Android中,我们一般使用SQLite,因为Android在android.database.sqlite包封装了很多SQLite操作的API.我自己写 ...