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

题目大意:给你一个长度为n数组,有q个询问,每次询问一个区间[l,r],这个区间的val就是所有数值为偶数个的数的亦或值。

思路:先求出所有区间的亦或和的val,然后利用树状数组离线维护,然后用所有区间^树状数组的亦或区间就是我们所要求的区间。

原因,因为树状数组维护的区间里面保存的是奇的,所以亦或以后就是偶的了

  1. //看看会不会爆int!数组会不会少了一维!
  2. //取物问题一定要小心先手胜利的条件
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #define LL long long
  6. #define ALL(a) a.begin(), a.end()
  7. #define pb push_back
  8. #define mk make_pair
  9. #define fi first
  10. #define se second
  11. const int maxn = 1e6 + ;
  12. vector<pair<int, int> > v[maxn];
  13. map<int, int> m;
  14. int a[maxn], sum[maxn], tree[maxn], ans[maxn];
  15. int n, q;
  16. inline int lowbit(int i) {return i & -i;}
  17.  
  18. void add(int pos, int val){
  19. for (int i = pos; i <= n; i += lowbit(i)){
  20. tree[i] ^= val;
  21. }
  22. }
  23.  
  24. inline int cal(int pos){
  25. int res = ;
  26. for (int i = pos; i >= ; i -= lowbit(i)) res ^= tree[i];
  27. return res;
  28. }
  29.  
  30. int main(){
  31. scanf("%d", &n);
  32. for (int i = ; i <= n; i++){
  33. scanf("%d", a + i); sum[i] = sum[i - ] ^ a[i];
  34. }
  35. scanf("%d", &q);
  36. for (int i = ; i <= q; i++){
  37. int l, r; scanf("%d%d", &l, &r);
  38. v[r].pb(mk(l, i));
  39. }
  40. int cnt = ;
  41. for (int i = ; i <= n; i++){
  42. if (m[a[i]]) add(m[a[i]], a[i]);
  43. add(i, a[i]);
  44. m[a[i]] = i;
  45. int len = v[i].size();
  46. for (int j = ; j < len; j++){
  47. pair<int, int> p = v[i][j];
  48. int res = sum[i] ^ sum[p.first - ];
  49. ///printf("i = %d\n", i);
  50. int tmp = cal(i) ^ cal(p.first - );
  51. ans[p.second] = res ^ tmp;
  52. if (p.first == && p.second == ) continue;
  53. cnt++;
  54. if (cnt == q) break;
  55. }
  56. }
  57. for (int i = ; i <= q; i++) printf("%d\n", ans[i]);
  58. return ;
  59. }

复杂度O(n*log)

CF 365 div2 D的更多相关文章

  1. CF #365 DIV2 D Mishka and Interesting sum 区间异或+线段树

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  2. TTTTTTTTTTTTT CF#365 div2 B 统计点

    B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard i ...

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

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

  4. 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 Interestin ...

  5. cf 442 div2 F. Ann and Books(莫队算法)

    cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...

  6. CF#603 Div2

    差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...

  7. CF R631 div2 1330 E Drazil Likes Heap

    LINK:Drazil Likes Heap 那天打CF的时候 开场A读不懂题 B码了30min才过(当时我怀疑B我写的过于繁琐了. C比B简单多了 随便yy了一个构造发现是对的.D也超级简单 dp了 ...

  8. CF#581 (div2)题解

    CF#581 题解 A BowWow and the Timetable 如果不是4幂次方直接看位数除以二向上取整,否则再减一 #include<iostream> #include< ...

  9. [CF#286 Div2 D]Mr. Kitayuta's Technology(结论题)

    题目:http://codeforces.com/contest/505/problem/D 题目大意:就是给你一个n个点的图,然后你要在图中加入尽量少的有向边,满足所有要求(x,y),即从x可以走到 ...

随机推荐

  1. js iframe跨域访问

    1.什么是跨域? 2.前台解决跨域几种方法 2.1 动态创建script 2.2 使用document.domain 2.3使用HTML5新属性postMessage 2.4 利用iframe和loc ...

  2. 基于python的tagcloud

    setp1: 安装jieba,pytagcloud pip install jieba apt-get install python-pygame pip install simplejson pip ...

  3. CSS3的box-sizing属性

    盒模型的宽度,在 IE5.x 以及 Quirks 模式的 IE6/7 中,将 border 与 padding 都包含在 width 之内 W3C标准中的盒模型宽度为内容宽度,不包括内边距paddin ...

  4. maven依赖本地宝

    http://www.mamicode.com/info-detail-169419.html 引用本地的jar包

  5. wmic应用实例

    实例应用 1.磁盘管理 查看磁盘的属性 wmic logicaldisk list brief ::caption=标题.driveID=驱动器ID号.model=产品型号.Partitions=分区 ...

  6. 第三十七节,hashlib加密模块

    在使用hashlib模块时需要先 import hashlib 引入模块 用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA ...

  7. getuid和geteuid的区别

    getuid() :  函数返回一个调用程序的真实用户ID.表明当前运行位置程序的执行者. geteuid(): 函数返回返回一个有效用户的ID.(EUID)是你最初执行程序时所用的ID,该ID是程序 ...

  8. Docker私有仓库Registry 搭建

    1. 关于Registry 官方的Docker hub是一个用于管理公共镜像的好地方,我们可以在上面找到我们想要的镜像,也可以把我们自己的镜像推送上去.但是,有时候,我们的使用场景需要我们拥有一个私有 ...

  9. centos中apache-tomcat的配置

    在centos中配置Apache-toncat需要先安装jdk,前面文章已经写了怎么配置jdk,这里略过. 首先到官网下载好Apache-tomcat安装包,我这里下载的是apache-tomcat- ...

  10. javascript string去除两边空格

    function trim(){ return this.replace(/(^\s*)|(\s*$)/g,""); g整个字符串 }