[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. PMP知识点(四)——项目管理计划的内容

    项目管理计划([4.2制定项目管理计划]的输出) 包含三个基准和十三个子计划和一些其他内容 三个基准:成本基本.进度基准.范围基准 其中范围基准([5.4创建WBS]的输出)包含了:项目范围说明书.W ...

  2. Python——使用第三方库Pillow生成图片缩略图

    流程如下: 1.首先确认是否安装了pip 在命令提示符窗口下输入pip,如果Windows提示未找到命令,可以重新运行安装程序添加pip. 2.在命令提示符窗口下输入pip install Pillo ...

  3. IE8 环境的 JQuery 中的 $.ajax 拒绝访问---解决方案

    需求是兼容到 IE8. $.ajax 总是请求失败,直接跳转到 error 函数中,报错:拒绝访问. 网上的一些解决方法都试过了还是没有用,比如再请求之前设置 jQuery.support.cors ...

  4. redhat7.4切换yum源为免费源

    1.redhat是Linux系统中付费的企业版,虽然安装什么是免费的,但是需要注册. 如果你有注册码,暂请出门左拐(我没有注册码,所以我也不会注册,不用往下看了). Linux系统收费版:RedHat ...

  5. SimpleDateFormat日期格式解析

    先看一个代码示例: import java.text.SimpleDateFormat; import java.util.Date; public class test{ public static ...

  6. CF1153C Serval and Parenthesis Sequence

    题目地址:CF1153C Serval and Parenthesis Sequence 思路:贪心 如果有解,那么 \(s_0 = (\) && \(s_{n-1} = )\) &a ...

  7. cocos2dx模拟器修改窗口大小

    修改模拟器窗口大小SimulatorWin.cpp搜索 frameSize修改frameSize = Size(1920*0.9, 1080*0.9);

  8. webpack是什么

    1,打包工具 模块打包 2.前端工程师 ,必不可少工具webpack作用 1.打包 (多个文件,打包成一个文件) 2.转化(less,sass,ts) 需要核心 技术 loader 3优化(SPA越来 ...

  9. Spring出现事务代理的原因

    JdbcTemplate 在配置事务代理之前,JdbcTemplate 的关闭策略,就是操作完立刻关闭!意味着,默认情况是JdbcTemplate操作是不支持事务的!!! 但是我们的程序是需要支持事务 ...

  10. TP-Shop安装步骤教程(Windows版)

    TP-Shop安装步骤教程(Windows版) PS:首次发文,请多指教! 一.安装要求 1.PHP5.4以上,MYsql5.5以上. 2.需要Phpcurl,gd库.php_mysqli,php_o ...