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是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了 ...
随机推荐
- 【JZOJ1914】【BZOJ2125】最短路
description 给一个N个点M条边的连通无向图,满足每条边最多属于一个环,有Q组询问,每次询问两点之间的最短路径. analysis 建出圆方树后,可以知道仙人掌上每一个方点连着的边双其实就是 ...
- Mongodb导入本地数据(.dat)到仓库(window)
借鉴文章,完成了数据文件导入到Mongodb, 尊重作者版权:https://blog.csdn.net/weixin_44198965/article/details/100022616 1.找到你 ...
- vma
linux内核的每一样技术,都是为了解决某个问题的. vma结构体的提出是为了管理"虚拟内存空间"这个资源. 像brk(),mmap()分配给用户空间的都是虚拟地址(当然),那内核就要知道哪一部分虚拟 ...
- PHP ftp_pwd() 函数
定义和用法 ftp_pwd() 函数返回指定 FTP 连接的当前目录名称. 语法 ftp_pwd(ftp_connection) 参数 描述 ftp_connection 必需.规定要使用的 FTP ...
- cf期望概率专题
cf1009E:求到第i段期望和的比较困难,但是单独求每段的期望是比较容易的,所以单独对每段求和,然后累计总和 E[i]=1/2*a1+1/4*a2+...+1/2^(i-1)*ai-1+1/2^(i ...
- 【Java基础知识】JNI入门
1.问题:javah or javac -h 在配置好java的环境变量后,java和javac都是可以用的,但是用javah就会出现问题. 会报错误 Unable to locate an exe ...
- jQuery 封装的ajax
jquery封装的ajax 具体操作: $.get(url [,data] [,fn回调函数] [, dataType]); data:给服务器传递的数据,请求字符串 .json对象 都可以设 ...
- 【vue】---vue中使用async+await出现的问题及解决方案
一.在Vue中出现的问题 因为我没有用脚手架,自己用webpack配置的环境,因此报了以下错误,出现的问题应该是缺少解析器的原因 二.解决方案 安装: npm i babel-plugin-trans ...
- jenkins集成sonar代码审核工具
在项目测试管理过程中,项目上线很多时候时间仓促,导致代码质量不高,测试时间不充分会导致线上出现各种各样的问题,这个时候一方面是增加测试的质量把控,还要从根本上解决开发小哥的代码质量问题.而Sonar这 ...
- linux模范配置文件格式
模范配置文件 #--------------------------------------------------------------------- # Global settings #--- ...