[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. (四)Python中的“四大才子”(字符串、列表、字典、集合)

    前戏:在python中把数据序列分为可变(mutable)和不可变(immutable)两种 不可变:string.int.float.tuple 特点:相同对象只是占用一个内存地址,不管有多少个变量 ...

  2. [置顶]Python开发之路

    阅读目录   第一篇:python入门 第二篇:数据类型.字符编码.文件处理 第三篇:函数 第四篇:模块与包 第五篇:常用模块 第六篇:面向对象 第七篇:面向对象高级 第八篇:异常处理 第九篇:网络编 ...

  3. C# NPOI 操作Excel 案例

    1.加入NPOI 程序集,使用nuget添加程序集 2.引用NPOI程序集 private IWorkbook ExportExcel(PrintQuotationOrderViewModel mod ...

  4. L1-Day5

    1.他偷了那个装满钱的书包. [我的翻译]He stole the bag full of money. [标准答案]He stole that school bag full of money. ( ...

  5. 自己写的一个用js把select换成div与span与ul的东西

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. java实现二叉树的建立以及实现二叉查找树的查、插、删、遍历

    一.采用存储结构 1.顺序存储:采用数组,顺序存储适配于完全二叉树,对于非完全二叉树并不合适,主要体现在空间上的浪费,所以我们需要用到另一种存储方式——链式存储. 2.链式存储:数据data用键值对的 ...

  7. pysvn 相关

    sudo apt-get install python-svn sudo apt-get install svn-workbench 安装过程中如果缺少相关依赖下载好在执行这两条语句 安装好之后的界面 ...

  8. 【转】一文掌握 Linux 性能分析之网络篇(续)

    [转]一文掌握 Linux 性能分析之网络篇(续) 在上篇网络篇中,我们已经介绍了几个 Linux 网络方向的性能分析工具,本文再补充几个.总结下来,余下的工具包括但不限于以下几个: sar:统计信息 ...

  9. SQL优化传送门

    转载:pursuer.chen的SQL Server 容易忽略的错误 链接:https://www.cnblogs.com/chenmh/p/3999475.html

  10. 题解-GXOI/GZOI2019 特技飞行

    Problem loj3085 bzoj不放题面差评 题意概要:给出两条竖直直线,再给出 \(n\) 架飞机的初始航线:一条接通这两条直线的线段,保证航线交点不在两条直线上.现要求安排所有飞机在航线相 ...