Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 树状数组

D. Mishka and Interesting sum

链接:

http://codeforces.com/problemset/problem/703/D

题意:

给一个序列 每次询问一个区间 求区间中出现次数为偶数次的数的异或和

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<map>
 5 using namespace std;
 6
 7 struct Query {
 8     int l, r, id;
 9     bool operator<(const Query &a)const {
10         return r < a.r;
11     }
12 };
13
14 const int maxn = 1000000 + 10;
15 int a[maxn], pre[maxn], ans[maxn], tree[maxn];
16 Query q[maxn];
17
18 void add(int k, int num)
19 {
20     while (k < maxn){
21         tree[k] ^= num;
22         k += k&-k;
23     }
24 }
25
26 int sum(int k)
27 {
28     int sum = 0;
29     while (k){
30         sum ^= tree[k];
31         k -= k&-k;
32     }
33     return sum;
34 }
35
36 int main()
37 {
38     int n;
39     cin >> n;
40     for (int i = 1; i <= n; i++) {
41         scanf("%d", &a[i]);
42         pre[i] = pre[i - 1] ^ a[i];
43     }
44     int m;
45     cin >> m;
46     for (int i = 1; i <= m; i++) {
47         scanf("%d%d", &q[i].l, &q[i].r);
48         q[i].id = i;
49     }
50     sort(q + 1, q + 1 + m);
51     map<int, int>vis;
52     int t = 1;
53     for (int i = 1; i <= n; i++) {
54         if (vis[a[i]])
55             add(vis[a[i]], a[i]);
56         vis[a[i]] = i;
57         add(i, a[i]);
58         while (q[t].r <= i && t <= m) {
59             int l = q[t].l - 1, r = q[t].r;
60             ans[q[t].id] = sum(l) ^ sum(r) ^ pre[l] ^ pre[r];
61             t++;
62         }
63     }
64     for (int i = 1; i <= m; i++)
65         printf("%d\n", ans[i]);
66     return 0;
67 }
时间: 2024-10-09 08:31:13

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 input standard input output standard output Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her wit

Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的性质,在一个区间中,我们如果把所有数字都异或的话,可以发现最后偶数次的数字异或后都变成了0,只剩下了奇数次的数字异或. 举个例子,{1,2,3,2,3,5} 异或和是1^2^3^2^3^5=1^5 因为最后要计算偶数次数字的异或和,那么最后我们只需要再异或上该区间内所有不同数字即可. 那么我们可以先

Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和Xor[i],表示1~i的xor和.因为num^num=0,所以Xor[r] ^ Xor[l - 1]求的是l~r之间出现奇数次的数字xor和. 那怎么求偶数次的呢,那我们可以先求l到r之间不重复出现数字的xor(比如1 1 2 求的是1 ^ 2),然后再xor以上求出的Xor[r] ^ Xor[l

Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum

题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数异或起来就是答案,那么出现偶数次的数就可以 先求出区间 l,r 内有多少不同的数,将这些数异或起来,再异或上区间内出现奇数次的数的异或和就是答案.(出现偶数次的数异或后为0,奇数次的数异或后是本身   然后离线处理询问,对询问按右端点 sort,因为树状数组保存的是数出现的最后位置.离线处理询问后便

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/problem/C Description Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numb

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 h

Codeforces Round #227 (Div. 2)---E. George and Cards(贪心, 树状数组+set维护, 好题!)

George is a cat, so he loves playing very much. Vitaly put n cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers f

Codeforces Round #609 (Div. 2)E--K Integers(贪心+二分+树状数组+逆序对)

K Integers 参考博客:https://blog.csdn.net/Q755100802/article/details/103664555 [题意] 给定一个1到n的排列,可以交换相邻的两个元素. 现在定义一个函数f(x),表示在原排列中,通过交换操作,形成一个1,2,3....x的排列的子串,需要的最小操作步骤. 子串意味着这个排列必须是相邻的.现在你需要求出f(1),f(2),f(3)......f(n). [分析] 在1~x这几个元素相邻的情况下,因为最后排列不存在逆序对,根据贪

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 Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有出现偶数次的数异或的值. 思路:容易想到,把区间内的所有的数都异或得到的是出现奇数次的数的值,然后再异或该区间内的所有出现过的数(每个数只统计一次),得到的ans了. 第一个问题:得到询问区间的