[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. seajs使用方法

    必须执行seajs.use()时,才能自动执行预加载项 <script src="/UILib/sea.js"></script> <script s ...

  2. python中元组/列表/字典/集合

    转自:https://blog.csdn.net/lobo_seeworld/article/details/79404566

  3. Codeforces Round #552 (Div. 3) F. Shovels Shop(dp)

    题目链接 大意:给你n个物品和m种优惠方式,让你买k种,问最少多少钱. 思路:考虑dpdpdp,dp[x]dp[x]dp[x]表示买xxx种物品的最少花费,然后遍历mmm种优惠方式就行转移就好了. # ...

  4. Mysql查看登录用户以及修改密码和创建用户以及授权(转载)

    本文转自(https://www.cnblogs.com/manzb/p/6491924.html) 1.mysql查看当前登录用户,当前数据库: select user(); select data ...

  5. 前端DES加密

    /** * DES加密/解密 * @Copyright Copyright (c) 2018 * @author mrDu * @see DESCore */ /* * encrypt the str ...

  6. tftp--实现服务器与客户端的下载与上传【转】

    转自:https://blog.csdn.net/xiaopangzi313/article/details/9122975 版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...

  7. node.js+mongodb 爬虫

    demo截图: 本demo爬瓜子二手车北京区的数据 (注:需要略懂 node.js / mongodb 不懂也没关系 因为我也不懂啊~~~) 之所以选择爬瓜子二手车网站有两点: 一.网站无需登录,少做 ...

  8. dml并行

    Enabling Parallel DMLA DML statement can be parallelized only if you have explicitly enabled paralle ...

  9. SQL baseline_11g

    conn sh/sh--执行想要创建基线的语句,语句的执行计划将会被缓存set autotrace on select /*ghbaselines1*/ count(*) from customers ...

  10. Eclipse install new software无反应

    一个问题可以有不同的解决方案 其他人提供了不少方案 我遇到了这个问题 但是这些解决方案都无济于事 于是 我就采取了一个新方案: 然后重新解压,找到里面的eclipse.exe重新打开就可以了 现在有反 ...