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是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了 ...
随机推荐
- leetcode-第14周双周赛-1271-十六进制魔术数字
自己的提交: class Solution: def toHexspeak(self, num: str) -> str: num = hex(int(num)) num = str(num)[ ...
- SnowFlakeId 分布式雪花id算法
package com.jn.baseservice.utils; import com.jn.baseservice.common.Number; import lombok.Getter; imp ...
- applicationContext-redis.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 关于RF做自动化大致流程的梳理
RF只是一个框架,类似于单元测试框架,可以实现对用例的有效管理.结合其它第三方库,可以进行,接口,数据库,APP的自动化测试.结合JENKINS,还可以进行有效的持续集成. 本文不讲调用第三方库的哪些 ...
- appium无法定位连接的真机元素
报错信息: com.android.ddmlib.SyncException:Remote object dosen't exist! 解决办法: 错误原因是因为没有dump下来界面的信息保存到uid ...
- 7. Python运算符之逻辑、成员、身份运算符及优先级
运算符 逻辑表达式 描述 and x and y 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值. or x or y 布尔& ...
- POJ 3304 Segments (判断直线与线段相交)
题目链接:POJ 3304 Problem Description Given n segments in the two dimensional space, write a program, wh ...
- 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷
通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下: 步骤1:通过MyEclipse中的window->show View->ot ...
- 区分slice,splice,split
原文:https://www.cnblogs.com/webjoker/p/5218114.html 1.slice(数组) 用法:array.slice(start,end) 解释:该方法是对数组进 ...
- 8-MySQL-Ubuntu-数据表中数据的增加(一)
增(insert) (1)全部字段插入数据:按表中字段顺序增加数据 注:(1)主键字段可以使用0/null/default来占位.(2)gender字段中数据类型是枚举,可以使用索引数字1,2,3,4 ...