Codeforces703D-Mishka and Interesting sum-离线树状数组
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
题意:传送门
原题目描述在最下面。
询问一个区间内出现次数为偶数次的数字的异或和。
思路:
先求出区间异或前缀和,其实就是出现次数为奇数次的数字的异或前缀和和。
然后用离线树状数组树状维护区间内区间内每种数字的前缀和。
最后的答案就是上面两个前缀和 差分一下 的异或和。
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<assert.h>
#include<bitset>
#include<unordered_map>
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x)&(-(x))
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = (int)1e6 +107;
int n, m;
LL pre[N], bit[N], ar[N], ans[N];
unordered_map<LL,int> vis;
void add(int x,LL c){
while(x<=n){
bit[x] ^= c;
x += lowbit(x);
}
}
LL query(int x){
LL sum = 0;
while(x){
sum ^= bit[x];
x -= lowbit(x);
}
return sum;
}
struct lp{
int l, r, id;
}cw[N];
bool cmp(lp &a, lp &b){
if(a.r!=b.r)return a.r<b.r;
return a.l<b.l;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("E://ADpan//in.in", "r", stdin);
//freopen("E://ADpan//out.out", "w", stdout);
#endif
while(~scanf("%d",&n)){
memset(pre,0,sizeof(pre));
memset(bit,0,sizeof(bit));
for(int i=1;i<=n;++i){
scanf("%lld",&ar[i]);
pre[i] = pre[i-1]^ar[i];
}
scanf("%d",&m);
for(int i=0;i<m;++i){
scanf("%d%d", &cw[i].l, &cw[i].r);
cw[i].id = i;
}
sort(cw,cw+m,cmp);
vis.clear();
int r = 0;
for(int i=0;i<m;++i){
while(r<cw[i].r){
++r;
add(r, ar[r]);
if(vis[ar[r]]==0) vis[ar[r]] = r;
else {
add(vis[ar[r]], ar[r]);
vis[ar[r]] = r;
}
}
ans[cw[i].id]=pre[cw[i].r]^pre[cw[i].l-1]^query(cw[i].r)^query(cw[i].l-1);
}
for(int i=0;i<m;++i){
printf("%lld%c", ans[i], " \n"[i==m-1]);
}
}
return 0;
}
####原题目描述:

Codeforces703D-Mishka and Interesting sum-离线树状数组的更多相关文章
- 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) ,问在每个区间里所有 ...
- 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 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- 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 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(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- HDU 2852 KiKi's K-Number(离线+树状数组)
题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...
- 离线树状数组 hihocoder 1391 Countries
官方题解: // 离线树状数组 hihocoder 1391 Countries #include <iostream> #include <cstdio> #include ...
- 区间的关系的计数 HDU 4638 离线+树状数组
题目大意:给你n个人,每个人都有一个id,有m个询问,每次询问一个区间[l,r],问该区间内部有多少的id是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了 ...
随机推荐
- final关键字和static关键字
final关键字:最终态--修饰成员变量,成员方法,类 final修饰变量: 基本类型变量:该变量为常量不能被赋值 引用类型变量:该地址不能被概变 地址不能被概变的原因: final Student ...
- PHP导出excel word的代码
php导出为word原理 一般,有2种方法可以导出doc文档,一种是使用com,并且作为php的一个扩展库安装到服务器上,然后创建一个com,调用它的方法.安装过office的服务器可以调用一个叫wo ...
- python 如何自动发送测试报告
首先,下载HTMLTestRuner.py文件. 源地址:http://tungwaiyip.info/software/HTMLTestRunner.html ,其次:把下载好的HTMLTestRu ...
- MySql命令行无法显示中文
好烦遇到了,遇到MySql命令行无法显示中文问题????? show variables like 'char%';//显示字符集 set names utf8;//设置字符集 describer t ...
- 杂项:SVN -u
ylbtech-杂项:SVN 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 10 ...
- 7. Jmeter-逻辑控制器介绍与使用
逻辑控制器介绍与使用 如果(if)控制器 事物控制器 循环控制器 while controller critical section controller foreach控制器 include con ...
- HDU 1392 Surround the Trees (凸包周长)
题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope ...
- PAT_A1124#Raffle for Weibo Followers
Source: PAT A1124 Raffle for Weibo Followers (20 分) Description: John got a full mark on PAT. He was ...
- Django框架(七)—— 模板层:变量、过滤器、标签、自定义标签和过滤器
目录 模板层:变量.过滤器.标签.自定义标签和过滤器 一.模板层变量 1.语法 2.使用 二.模板层之过滤器 1.语法 2.常用过滤器 3.其他过滤器 三.模板值标签 1.for标签 2.if标签 3 ...
- 2-Ubuntu命令安装mysql服务器和客户端及安装后的简单验证操作
转自: https://www.cnblogs.com/zhuyp1015/p/3561470.html 安装完成之后可以使用如下命令来检查是否安装成功: sudo netstat -tap | ...