• [树状数组]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, ..., areven 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
    3
    3 7 8
    1
    1 3
    output
    Copy
    0
    input
    Copy
    7
    1 2 1 3 3 2 3
    5
    4 7
    4 5
    1 3
    1 7
    1 5
    output
    Copy
    0
    3
    1
    3
    2
    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
    ",ans[i]);
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    Cookie的使用(二)
    对创业者有启发的10个故事
    PHP MySQL 相关函数
    异步刷新加载脚本(转载老赵)
    全栈工程师之路中级篇之小程序开发第二章第一节小程序的模板评分星星模板
    全栈工程师之路中级篇之小程序开发第二章第二节小程序电影卡片模板
    全栈工程师之路中级篇之小程序开发第一章第三节阅读官方demo
    全栈工程师之路中级篇之小程序开发第一章第五节从px到rpx
    全栈工程师之路中级篇之小程序开发第一章第二节注册小程序和开发工具讲解
    全栈工程师之路中级篇之小程序开发第一章第四节从block盒式布局到Flex弹性布局
  • 原文地址:https://www.cnblogs.com/lllxq/p/9092565.html
Copyright © 2020-2023  润新知