codeforces 703D D. Mishka and Interesting sum(树状数组)

D. 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

3
3 7 8
1
1 3

output

0

input

7
1 2 1 3 3 2 3
5
4 7
4 5
1 3
1 7
1 5

output

0
3
1
3
2

题意:

问一个区间里面出现偶数次的数字的异或和是多少;

思路:

一个区间的异或和得到的是出现奇数次的数字的异或和,现在再异或个所有的数字的异或和就是答案了;离线处理,按有区间排序,
结合map存与每个数相同的最近的前边的前驱位置,然后用树状数组每次删去不用的前驱位置;

AC代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

/************************************************

 

************************************************ */

  

  

#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <bits/stdc++.h>

#include <stack>

  

using namespace std;

  

#define For(i,j,n) for(int i=j;i<=n;i++)

#define mst(ss,b) memset(ss,b,sizeof(ss));

  

typedef  long long LL;

  

template<class T> void read(T&num) {

    char CH; bool F=false;

    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());

    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());

    F && (num=-num);

}

int stk[70], tp;

template<class T> inline void print(T p) {

    if(!p) { puts("0"); return; }

    while(p) stk[++ tp] = p%10, p/=10;

    while(tp) putchar(stk[tp--] + '0');

    putchar('\n');

}

  

const LL mod=1e9+7;

const double PI=acos(-1.0);

const int inf=1e9;

const int N=1e6+10;

const int maxn=2e3+14;

const double eps=1e-12;

 

int n,a[N],sum[N],ha[N];

int lowbit(int x){return x&(-x);}

 

void update(int x,int temp)

{

    while(x<=n)

    {

        sum[x]^=temp;

        x+=lowbit(x);

    }

}

 

int query(int x)

{

    int s=0;

    while(x)

    {

        s^=sum[x];

        x-=lowbit(x);

    }

    return s;

}

struct node

{

    int l,r,id;

}po[N];

int cmp(node a,node b)

{

    //if(a.r==b.r)return a.l<b.l;

    return a.r<b.r;

}

map<int,int>mp;

int pre[N],ans[N];

int main()

{      

        read(n);

        For(i,1,n)

        {

            read(a[i]);

            ha[i]=ha[i-1]^a[i];

            pre[i]=mp[a[i]];

            mp[a[i]]=i;

        }

        int q;

        read(q);

        For(i,1,q)

        {

            read(po[i].l);

            read(po[i].r);

            po[i].id=i;

        }

        sort(po+1,po+q+1,cmp);//cout<<"##"<<endl;

        int p=1;

        For(i,1,q)

        {

            while(p<=po[i].r)

            {

                if(pre[p])update(pre[p],a[pre[p]]);

                update(p,a[p]);

                p++;

            }

            ans[po[i].id]=(ha[po[i].r]^ha[po[i].l-1]^query(po[i].r)^query(po[i].l-1));

            //cout<<po[i].id<<" "<<sum[po[i].l-1]<<" "<<sum[3]<<" "<<sum[po[i].r]<<endl;

        }

        For(i,1,q)printf("%d\n",ans[i]);

        return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值