[CF703D]Mishka and Interesting sum/[BZOJ5476]位运算

题目大意:

一个长度为\(n(n\le10^6)\)的序列\(A\)。\(m(m\le10^6)\)次询问,每次询问区间\([l,r]\)中,出现次数为偶数的数的异或和。

思路:

将询问离线,按照右端点排序。从左到右加入每一个数,并在该数上一次出现的位置算上贡献。显然,若一个数出现了\(x\)次,则只有\(x-1\)次对答案有贡献。这可以用树状数组维护。时间复杂度\(\mathcal O((n+m)\log n)\)。

源代码:

#include<map>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e6+1,M=1e6;
int n,a[N];
struct Query {
int l,r,id;
bool operator < (const Query &rhs) const {
return r<rhs.r;
}
};
Query q[M];
class FenwickTree {
private:
int val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
void modify(int p,const int &x) {
for(;p<=n;p+=lowbit(p)) {
val[p]^=x;
}
}
int query(int p) const {
int ret=0;
for(;p;p-=lowbit(p)) {
ret^=val[p];
}
return ret;
}
int query(const int &l,const int &r) const {
return query(r)^query(l-1);
}
};
FenwickTree t;
std::map<int,int> last_pos;
int ans[M];
int main() {
n=getint();
for(register int i=1;i<=n;i++) {
a[i]=getint();
}
const int m=getint();
for(register int i=0;i<m;i++) {
q[i].l=getint();
q[i].r=getint();
q[i].id=i;
}
std::sort(&q[0],&q[m]);
for(register int i=1,j=0;i<=n;i++) {
if(last_pos[a[i]]) {
t.modify(last_pos[a[i]],a[i]);
}
for(;j<m&&q[j].r==i;j++) {
ans[q[j].id]=t.query(q[j].l,q[j].r);
}
last_pos[a[i]]=i;
}
for(register int i=0;i<m;i++) {
printf("%d\n",ans[i]);
}
return 0;
}

[CF703D]Mishka and Interesting sum/[BZOJ5476]位运算的更多相关文章

  1. CF703D Mishka and Interesting sum

    题意:给定一个1e6长度的值域1e9的数组.每次给定询问,询问区间内出现偶数次的数的异或和. 题解:首先很显然,每一次询问的答案,等于这个区间所有不同元素异或和异或上区间异或和.(因为出现偶数次的对区 ...

  2. CF #365 DIV2 D Mishka and Interesting sum 区间异或+线段树

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  3. 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) ,问在每个区间里所有 ...

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

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

  6. Mishka and Interesting sum

    Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input ...

  7. CF #365 703D. Mishka and Interesting sum

    题目描述 D. Mishka and Interesting sum的意思就是给出一个数组,以及若干询问,每次询问某个区间[L, R]之间所有出现过偶数次的数字的异或和. 这个东西乍看很像是经典问题, ...

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

  9. codeforces 703D Mishka and Interesting sum 偶数亦或 离线+前缀树状数组

    题目传送门 题目大意:给出n个数字,m次区间询问,每一次区间询问都是询问 l 到 r 之间出现次数为偶数的数 的亦或和. 思路:偶数个相同数字亦或得到0,奇数个亦或得到本身,那么如果把一段区间暴力亦或 ...

随机推荐

  1. docker安装elasticsearch

    docker search elasticsearch 选择一个版本,拉取镜像 docker pull elasticsearch: 查看镜像 docker images 通过镜像,启动一个容器,并将 ...

  2. react中根据后台值动态配置

    业务中我们要实现对应的数据是1是男,2是女,这就要根据键来进行动态匹配,通常后台来给你一个1或者2,你来进行匹配,这样的数据一般在表格中比较常见. <Card title="Mock- ...

  3. python核心编程(多线程编程)

    1.全局解释器锁 2.threading模块 thread类

  4. MySQL插入中文数据出现?号

    原文转载自:https://blog.csdn.net/LynneZoe/article/details/79174119 运行环境:win10 mysql版本:Mysql5.6 做一个项目的时候,向 ...

  5. 基于Spring注解搭建SpringMVC项目

    在2018寒冬,我下岗了,因为我的左脚先迈进了公司的大门.这不是重点,重点是我扑到了老板小姨子的怀里. 网上好多教程都是基于XML的SpringMVC,想找一篇注解的,但是写的很模糊,我刚好学到这里, ...

  6. CocoaLumberjack——带颜色的Log

    CocoaLumberjack可以带颜色Log,具体的好处嘛,谁用谁知道,:] 具体步骤如下: 1. 安装XcodeColors插件 下载地址:https://github.com/robbiehan ...

  7. fiddler限制网速

    在测试过程中,经常会要求测试弱网络情况时的一些特殊情况,这时候IOS还好说,在开发者选项中调整网络模式即可,但android就只能通过别的方式了,这里整理了通过fiddler抓包工具来设置弱网模式,在 ...

  8. ffmypeg 视频处理类库使用方法

    (经常用到ffmpeg 做一些视频数据的处理转换等,用来做测试,今天总结了一下,参考了网上部分朋友的经验,一起在这里汇总了一下,有需要的朋友可以收藏测试一下,有问题欢迎在下面回帖交流,谢谢;by te ...

  9. DataTable插件通过js导出Excel

    $('#myTab').DataTable( { serverSide: false,//分页,取数据等等的都放到服务端去. true为后台分页,每次点击分页时会请求后台数据,false为前台分页 d ...

  10. [paper reading] C-MIL: Continuation Multiple Instance Learning for Weakly Supervised Object Detection CVPR2019

    MIL陷入局部最优,检测到局部,无法完整的检测到物体.将instance划分为空间相关和类别相关的子集.在这些子集中定义一系列平滑的损失近似代替原损失函数,优化这些平滑损失. C-MIL learns ...