树状数组,与Turing Tree类似。

xr[i]表示从1到i的抑或,树状数组维护从1到i每个数只考虑一次的异或,结果为sum(r) ^ sum(l) ^ xr[r] ^ xr[l]

其中xr[r] ^ xr[l] 相当于l + 1到r出现奇数次的数的异或,sum(r) ^ sum(l)表示l + 1到r每个数只考虑一次的异或,则两者异或为出现偶数次的数的异或。

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<algorithm>
  7. #include<map>
  8. #include<queue>
  9. #include<vector>
  10. #include<cmath>
  11. #include<utility>
  12. using namespace std;
  13. const int N = 1000008, INF = 0x3F3F3F3F;
  14. #define MS(a, num) memset(a, num, sizeof(a))
  15. #define PB(A) push_back(A)
  16. #define FOR(i, n) for(int i = 0; i < n; i++)
  17. int a[N];
  18. struct Que{
  19. int l, r, i;
  20. }que[N];
  21. int C[N],n;
  22. int ans[N];
  23. int xr[N];
  24. inline int lowbit(int x){
  25. return x&-x;
  26. }
  27. void add(int x, int val){
  28. for(int i=x;i<=n;i+=lowbit(i)){
  29. C[i] ^= val;
  30. }
  31. }
  32. int sum(int x){
  33. int ret = 0;
  34. for(int i=x;i>0;i-=lowbit(i)){
  35. ret^=C[i];
  36. }
  37. return ret;
  38. }
  39.  
  40. bool cmp(const Que &a, const Que &b){
  41. return a.r < b.r;
  42. }
  43. int main(){
  44. int t, q;
  45. scanf("%d", &n);
  46. xr[0] = 0;
  47. for(int i = 1; i <= n; i++){
  48. scanf("%d", &a[i]);
  49. xr[i] = xr[i - 1] ^a[i];
  50. }
  51. scanf("%d", &q);
  52. for(int i = 0; i < q; i++){
  53. scanf("%d %d", &que[i].l , &que[i].r);
  54. que[i].i = i;
  55. }
  56. sort(que, que + q, cmp);
  57. MS(C, 0 );
  58. map<int, int> vis;
  59. int j = 0;
  60. for(int i = 1; i <= n; i++){
  61. if(vis.find(a[i]) != vis.end()){
  62. int p = vis[a[i]];
  63. add(p, a[i]);
  64. }
  65. vis[a[i]] = i;
  66. add(i, a[i]);
  67. while(j < q && que[j].r == i){
  68. int l = que[j].l - 1, r = que[j].r;
  69. ans[que[j].i] = sum(r) ^ sum(l) ^ xr[r] ^ xr[l];
  70. j++;
  71. }
  72. }
  73. for(int i = 0; i < q; i++){
  74. printf("%d\n", ans[i]);
  75. }
  76. return 0;
  77. }

  

Mishka and Interesting sum Codeforces Round #365 (树状数组)的更多相关文章

  1. codeforces 597C (树状数组+DP)

    题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k ...

  2. Codeforces 597C. Subsequences (树状数组+dp)

    题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...

  3. CodeForces 371D Vessels(树状数组)

    树状数组,一个想法是当往p注水时,认为是其容量变小了,更新时二分枚举,注意一些优化. #include<cstdio> #include<iostream> #include& ...

  4. Leetcode 2——Range Sum Query - Mutable(树状数组实现)

    Problem: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...

  5. Petya and Array CodeForces - 1042D (树状数组)

    D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. hdu5592/BestCoder Round #65 树状数组寻找第K大

    ZYB's Premutation    Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一个排列PP,但他只记得PP中每个前缀区间的逆 ...

  7. Sereja and Brackets CodeForces - 380C (树状数组+离线)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  8. 2019ICPC 上海网络赛 L. Digit sum(二维树状数组+区间求和)

    https://nanti.jisuanke.com/t/41422 题目大意: 给出n和b,求1到n,各数在b进制下各位数之和的总和. 直接暴力模拟,TLE.. 没想到是要打表...还是太菜了. # ...

  9. Codeforces 1096F(dp + 树状数组)

    题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...

随机推荐

  1. 33 网络相关函数(一)——live555源码阅读(四)网络

    33 网络相关函数(一)——live555源码阅读(四)网络 33 网络相关函数(一)——live555源码阅读(四)网络 简介 1)IsMulticastAddress多播(组播)地址判断函数 多播 ...

  2. python入门教程链接

    python安装 选择 2.7及以上版本 linux: 一般都自带 windows: https://www.python.org/downloads/windows/ mac os: https:/ ...

  3. 1.6---旋转二维数组,旋转图像像素,旋转矩阵,90度(CC150)

    import java.util.*; public class Transform { public int[][] transformImage(int[][] matrix, int n) { ...

  4. thinkphp save()方法没有数据,保存失败解决办法

    thinkphp save()方法没有数据保存返回0,保存失败返回false   可以对返回值判断一下就好 $ret = $model->save($data); //var_dump($ret ...

  5. 转帖: 使用脚本删除程序(免除在[控制面板]->[添加或删除程序]中的手工操作)

    1. 代码:VBS strComputer = "." '这个表示本地计算机 Set objWMIService = GetObject("winmgmts:" ...

  6. c#.net循环将DataGridView中的数据赋值到Excel中,并设置样式

    Microsoft.Office.Interop.Excel.Application excel =                new Microsoft.Office.Interop.Excel ...

  7. Linux下如何移除同时在线的用户

    Linux下移除同时在线的用户太多时,shell操作会变得比较卡,很多时候经常是直接关闭终端导致不正常退出,一般要等上一段时间才会退出,这个时候主动结束用户进程使用户下线是比较好的方式,方法如下: 使 ...

  8. 【ACM】hud1166 敌兵布阵(线段树)

    经验: cout 特别慢 如果要求速度 全部用 printf !!! 在学习线段树 内容来自:http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/24 ...

  9. objective-c字典

     1 //        初始化一个空字典  2 //        NSDictionary *dictionary = [[NSDictionary alloc] init];  3 //     ...

  10. stdafx.h的作用

    // stdafx.h : include file for standard system include files,// or project specific include files th ...