[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. Window7 定制 Explore中的右键菜单

    win+R 命令  运行 regedit ,打开注册表 在  HKEY_CLASSES_ROOT\*\shell\VisualCode下创建针对文件的新增命令 command 在HKEY_CLASSE ...

  2. 域 搭建OU 组织单元

    以这个界面开始操作: 在 baidu.com 右键---新建----组织单位----北京分公司 在 baidu.com 右键---新建----组织单位----北京分公司 在北京分公司 和南京分公司下面 ...

  3. MySQL命令行查询乱码解决方法

    转自Agoly的博客,原文链接https://www.cnblogs.com/qmfsun/p/4846467.html 感谢博主Agoly这篇文章说的很详细很透彻. MySQL会出现中文乱码的原因不 ...

  4. Image 上传下载Api

    1.配置 "UploadConfig": { // 自定义存放位置,无需放到站点内部 "Path": "C:\\Users\\kxy\\Desktop ...

  5. lua post参数获取,参数截断

    post 请求头: a.application/x-www-form-urlencoded 普通表单提交 b.multipart/form-data 含有文件的表单,二进制上传 c.applicati ...

  6. db2数据库备份与恢复

    备份 先停掉Tomcat,然后在机器A上执行以下语句: db2stop force db2start db2 force application all db2 backup database pos ...

  7. springboot idea 代码更改自己编译设置

    第一步:在pom.xml文件中添加springboot 编译依赖 <dependency> <groupId>org.springframework.boot</grou ...

  8. SpringBoot jar 注册windows服务

    1.pom.xml中添加 <plugin> <groupId>cn.joylau.code</groupId> <artifactId>joylau-s ...

  9. 「JOISC 2018 Day 3」比太郎的聚会

    题解: 很套路的题目 我们按照询问中的不算的个数是否大于$block$分类 如果大于,就$O(n)dp$一下 如果小于,就预处理出到每个点前$block$小的点 $block取\sqrt{n}$的话复 ...

  10. 《ServerSuperIO Designer IDE使用教程》- 5.树形结构管理设备驱动,小版本更新。发布:v4.2.3.1版本

    v4.2.3.1 更新内容:1.选择和管理设备驱动,增加树状结构显示.2.优化ide代码,核心代码没有改动.下载地址:官方下载 5. 树形结构管理设备驱动,小版本更新 5.1    概述 此次升级主要 ...