[树状数组]Mishka and Interesting sum(codeforces703D)

Mishka and Interesting sum

time limit per test

3.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!

Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can‘t process large arrays. Right because of that she invited you to visit her and asked you to process m queries.

Each query is processed in the following way:

  1. Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment.
  2. Integers, presented in array segment [l,  r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down.
  3. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where  — operator of exclusive bitwise OR.

Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.

Input

The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array.

The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements.

The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries.

Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.

Output

Print m non-negative integers — the answers for the queries in the order they appear in the input.

Examples

input

Copy

33 7 811 3

output

Copy

0

input

Copy

71 2 1 3 3 2 354 74 51 31 71 5

output

Copy

03132

Note

In the second sample:

There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.

In the second query there is only integer 3 is presented even number of times — the answer is 3.

In the third query only integer 1 is written down — the answer is 1.

In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is .

In the fifth query 1 and 3 are written down. The answer is .

题意:m个询问,每个询问求一个区间内出现次数为偶的数的异或和

思路:一个区间所有数的异或和,等于这个区间内出现次数为奇的数的异或和,那么这个区间内出现次数为偶的数的异或和就等于这个区间出现过的数的异或和异或上这个区间内所有数的异或和(也就是出现次数为奇的数在出现过的数中的补集);

一个区间所有数的异或和很好求,只需记录前缀异或和就可以了;那,一个区间内出现过的数的异或和怎么求?总不能也维护一个前缀值把?因此,在线操作是没办法的;

采取离线操作,将每个询问区间按右端点从小到大排序;依次处理排序后的区间;在这个区间[l,r]内,在某位置出现的数要在该位置标记为此数(更新),若是该数在之前位置出现过了,将之前位置的该数消除(同样是更新操作)——也就是对于重复出现的数,总是保留位置靠右的数;这样,getsum(r)-getsum(l-1)将总是得到区间[l,r]内出现过的数的异或和;(太巧妙了%%%)

AC代码:

#include <iostream>
#include<cstdio>
#include<map>
#include<algorithm>
#define lowbit(x) x&(-x)
using namespace std;

int n;
int a[1000010];
int sum[1000010];//前缀异或和
int c[1000010];//树状数组中的C数组
int ans[1000010];

struct Q{
  int l,r,ind;
}q[1000010];

bool cmp(Q a,Q b){
  return a.r<b.r;
}

map<int ,int> last;//记录某个数出现的最后位置

void add(int x,int val){
  for(int i=x;i<=n;i+=lowbit(i)) c[i]^=val;
}

int getsum(int x){
  int ret=0;
  for(int i=x;i>0;i-=lowbit(i)) ret^=c[i];
  return ret;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {scanf("%d",&a[i]); sum[i]=sum[i-1]^a[i];}
    int m;
    scanf("%d",&m);
    for(int i=1;i<=m;i++) {scanf("%d%d",&q[i].l,&q[i].r); q[i].ind=i;}
    sort(q+1,q+1+m,cmp);
    for(int pos=1,i=1;i<=m;i++){
        for(;pos<=q[i].r;pos++){
            add(pos,a[pos]);
            if(last[a[pos]]!=0) add(last[a[pos]],a[pos]);//如果a[pos]不是第一次出现,消除上次出现位置标记的a[pos]
            last[a[pos]]=pos;//将a[pos]出现的最后位置更新为pos
        }
        ans[q[i].ind]=sum[q[i].r]^sum[q[i].l-1]^getsum(q[i].r)^getsum(q[i].l-1);
    }
    for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
    return 0;
}

原文地址:https://www.cnblogs.com/lllxq/p/9092565.html

时间: 2024-10-13 00:10:47

[树状数组]Mishka and Interesting sum(codeforces703D)的相关文章

HDU---4417Super Mario 树状数组 离线操作

题意:给定 n个数,查询 位置L R内 小于x的数有多少个. 对于某一次查询 把所有比x小的数 ”的位置“ 都加入到树状数组中,然后sum(R)-sum(L-1)就是答案,q次查询就要离线操作了,按高度排序. #include <set> #include <map> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <cct

hdu 3450 树状数组优化dp

题意就不说了: hdu2227差不多只不过这道题不是递增  而是相邻差不超过d   其实是为都一样,  也有差别: 这道题没说范围  并且树之间的大小关系对结果有影响,所以树状数组里根据原来id来求,由于数值很大 所以还是用到离散化  这里的离散化感觉和映射差不多,离散化用来查找id: 当num[i]的id是x是,dp[i]表示方案数  很明显dp[i]=sun(dp[j],j<i&&abs(num[j]-num[i])<=d)  这里在树状数组里面就是求sum[1到num[i

poj 2299 Ultra-QuickSort 求逆序数,树状数组解法,详细解析

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 44554   Accepted: 16195 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

OI模板(2) —— 树状数组(BIT)

在codevs上有模板题目,虽说是线段树模板,不过就题目描述来说,树状数组轻松水过 传送门:http://codevs.cn/problem/1080/ 能用线段树就不要用平衡树,能用树状数组就不要用线段树,这话是显然的,代码长度.难道上都有较大区别,BIT是最简单的一个 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #incl

树状数组区间修改and查询和

在差分数组上稍加改变,就可以实现这个骚操作 首先我们先来看一看普通的树状数组(基于差分)怎么暴力的求解区间和就是询问区间长度次和 \(\sum^{i=1}_{len}\sum^{j=1}_{i}base[j]\) base为原数列 以上便是暴力求解,然后我们可以发现\(base[i]\)被加了\(p-i+1\)次 于是乎,我们就可以改写上式成为下式 \((len+1)\sum^{i=1}_{len}-\sum^{i=1}_{len}(base[i]*i)\) 我们用一个树状数组维护\((len+

1042.D Petya and Array 前缀 + 树状数组

11.19.2018 1042.D Petya and ArrayNew Point: 前缀 + 树状数组 :树状数组逐个维护前缀个数 Describe: 给你一个数组,一个标记数,问你有多少区间[l,r]使得这个区间的和小于这个标记数值 Solution: 没能想到 前缀数组 + 树状数组快速查询 记录前缀数组sum[i],得到区间和为sum[i] - sum[j] < t,转化为求sum[i] - t < sum[j],遍历i,求取情况,然后利用树状数组快速查询符合的区间j的个数 树状数组

CF587F Duff is Mad(AC自动机+树状数组+分块)

考虑两一个暴力 1 因为询问\([a,b]\)可以拆成\([1,b]\)-\([1,a-1]\)所以把询问离线,然后就是求\([1,x]\)中被\(S_i\)包含的串的数量.考虑当\([1,x-1]->[1,x]\)时我们把\(S_x\)结束节点在fail树的子树加1.然后询问就是求\(S_i\)在AC自动机上跑时经过所有点的点权用树状数组维护.设\(\sum{len[S_i]}=L\)这样的复杂度就是\(O(mLlogL)\)无法通过此题. 2 依然离线.这次我们把\(S_i\)放在fail树

【xsy2111】 【CODECHEF】Chef and Churus 分块+树状数组

题目大意:给你一个长度为$n$的数列$a_i$,定义$f_i=\sum_{j=l_i}^{r_i} num_j$. 有$m$个操作: 操作1:询问一个区间$l,r$请你求出$\sum_{i=l}^{r} f_i$. 操作2:将$a_x$变成$y$. 此题貌似正常做都不是很好做,考虑用一些奇奇怪怪的做法(比如说分块) 考虑到此题数列在不断地变化,我们考虑用树状数组来维护序列$a$,查询$f_i$的值可以在$O(log n)$的时间内完成. 如果这么做,单次询问的复杂度是$O(n log n)$的,

E - Apple Tree(树状数组+DFS序)

There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by branches. Kaka numbers