Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D
思路:看了神犇的代码写的...
偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , r]每个数相异或^区间[l , r]出现过的数相异或。如数组1,2,1,3,3,2,3,则ans[1 , 7]=(1^2^1^3^3^2^3)^(1^2^3)
前半部分可以处理出前缀异或,后半部分先对询问按r进行排序,处理过程中相同的数只保留最后一个。
#include<bits/stdc++.h>
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;
const int N=1e6+3;
int n,a[N],prefix[N],last[N],c[N],ans[N];
struct node
{
int l,r,id;
}q[N];
map<int,int> temp;
bool cmp(node a,node b)
{
return a.r<b.r;
}
void update(int pos,int num)
{
while(pos<=n)
{
c[pos]^=num;
pos+=lowbit(pos);
}
}
int query(int pos)
{
int res=0;
while(pos)
{
res^=c[pos];
pos-=lowbit(pos);
}
return res;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
prefix[i]=prefix[i-1]^a[i];
last[i]=temp[a[i]];
temp[a[i]]=i;
}
int m;
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q+1,q+m+1,cmp);
for(int i=1,cur=1;i<=m;i++)
{
while(cur<=q[i].r)
{
if(last[cur])
update(last[cur],a[cur]);
update(cur,a[cur]);
cur++;
}
ans[q[i].id]=query(q[i].r)^query(q[i].l-1)^prefix[q[i].r]^prefix[q[i].l-1];
}
for(int i=1;i<=m;i++)
printf("%d\n",ans[i]);
return 0;
}
Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum
题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】
主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤ ...
随机推荐
- BZOJ3933 [CQOI2015]多项式
$\sum_{k = 0} ^ {n} a_kx^k = \sum_{k = 0} ^ {n} b_k(x - t)^k \Leftrightarrow \sum_{k = 0} ^ {n} a_k( ...
- sqlserver 2008 左补齐字符串
SQLServer:right函数 语法 Right(string, length) Right 函数的语法具有下面的命名参数: 部分 说明 string 必要参数.字符串表达式,从中最右边的 ...
- ASP.NET MVC 输出字符串
@{Output.Write("<h1>输出字符串</h1>");}
- LA 4119 - Always an integer
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- ubuntu 14.04 安装截图工具 Shutter及使用
一.安装截图工具 Shutter 1. 添加安装包软件源 sudo add-apt-repository ppa:shutter/ppa 1 2. 更新源并安装 shutter sudo apt-ge ...
- LCD驱动 15-3
测试:1:make menuconfig去掉原来的驱动程序 Device Drivers ---> Graphics support ---> ...
- 【STL】-pair的用法
初始化: std::pair<int, float> p; //initialize p.first and p.second with zero std::pair<int, co ...
- 全国行政区划代码(json对象版)
var area = {"110000":"北京市","110100":"北京市","110101" ...
- Eclipse版本及其代号
1.维基百科介绍 http://zh.wikipedia.org/wiki/Eclipse 2.版本及其代号
- OpenCV之响应鼠标(二):函数cvSetMouseCallback()和其副程式onMouse()的使用(OpenCV2.4.5)
每當滑鼠在視訊視窗介面點擊一下的時候,都會有固定三個動作 1.點擊(Click) 2.放開(Down)3.滑動(move) 因此,程式執行滑鼠在點擊的時候onMouse()都會連續跑三次,代表滑鼠在點 ...