CF 365 div2 D
http://codeforces.com/contest/703/problem/D
题目大意:给你一个长度为n数组,有q个询问,每次询问一个区间[l,r],这个区间的val就是所有数值为偶数个的数的亦或值。
思路:先求出所有区间的亦或和的val,然后利用树状数组离线维护,然后用所有区间^树状数组的亦或区间就是我们所要求的区间。
原因,因为树状数组维护的区间里面保存的是奇的,所以亦或以后就是偶的了
- //看看会不会爆int!数组会不会少了一维!
- //取物问题一定要小心先手胜利的条件
- #include <bits/stdc++.h>
- using namespace std;
- #define LL long long
- #define ALL(a) a.begin(), a.end()
- #define pb push_back
- #define mk make_pair
- #define fi first
- #define se second
- const int maxn = 1e6 + ;
- vector<pair<int, int> > v[maxn];
- map<int, int> m;
- int a[maxn], sum[maxn], tree[maxn], ans[maxn];
- int n, q;
- inline int lowbit(int i) {return i & -i;}
- void add(int pos, int val){
- for (int i = pos; i <= n; i += lowbit(i)){
- tree[i] ^= val;
- }
- }
- inline int cal(int pos){
- int res = ;
- for (int i = pos; i >= ; i -= lowbit(i)) res ^= tree[i];
- return res;
- }
- int main(){
- scanf("%d", &n);
- for (int i = ; i <= n; i++){
- scanf("%d", a + i); sum[i] = sum[i - ] ^ a[i];
- }
- scanf("%d", &q);
- for (int i = ; i <= q; i++){
- int l, r; scanf("%d%d", &l, &r);
- v[r].pb(mk(l, i));
- }
- int cnt = ;
- for (int i = ; i <= n; i++){
- if (m[a[i]]) add(m[a[i]], a[i]);
- add(i, a[i]);
- m[a[i]] = i;
- int len = v[i].size();
- for (int j = ; j < len; j++){
- pair<int, int> p = v[i][j];
- int res = sum[i] ^ sum[p.first - ];
- ///printf("i = %d\n", i);
- int tmp = cal(i) ^ cal(p.first - );
- ans[p.second] = res ^ tmp;
- if (p.first == && p.second == ) continue;
- cnt++;
- if (cnt == q) break;
- }
- }
- for (int i = ; i <= q; i++) printf("%d\n", ans[i]);
- return ;
- }
复杂度O(n*log)
CF 365 div2 D的更多相关文章
- 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 ...
- TTTTTTTTTTTTT CF#365 div2 B 统计点
B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 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) ,问在每个区间里所有 ...
- 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 ...
- 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, ...
- CF#603 Div2
差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...
- CF R631 div2 1330 E Drazil Likes Heap
LINK:Drazil Likes Heap 那天打CF的时候 开场A读不懂题 B码了30min才过(当时我怀疑B我写的过于繁琐了. C比B简单多了 随便yy了一个构造发现是对的.D也超级简单 dp了 ...
- CF#581 (div2)题解
CF#581 题解 A BowWow and the Timetable 如果不是4幂次方直接看位数除以二向上取整,否则再减一 #include<iostream> #include< ...
- [CF#286 Div2 D]Mr. Kitayuta's Technology(结论题)
题目:http://codeforces.com/contest/505/problem/D 题目大意:就是给你一个n个点的图,然后你要在图中加入尽量少的有向边,满足所有要求(x,y),即从x可以走到 ...
随机推荐
- js iframe跨域访问
1.什么是跨域? 2.前台解决跨域几种方法 2.1 动态创建script 2.2 使用document.domain 2.3使用HTML5新属性postMessage 2.4 利用iframe和loc ...
- 基于python的tagcloud
setp1: 安装jieba,pytagcloud pip install jieba apt-get install python-pygame pip install simplejson pip ...
- CSS3的box-sizing属性
盒模型的宽度,在 IE5.x 以及 Quirks 模式的 IE6/7 中,将 border 与 padding 都包含在 width 之内 W3C标准中的盒模型宽度为内容宽度,不包括内边距paddin ...
- maven依赖本地宝
http://www.mamicode.com/info-detail-169419.html 引用本地的jar包
- wmic应用实例
实例应用 1.磁盘管理 查看磁盘的属性 wmic logicaldisk list brief ::caption=标题.driveID=驱动器ID号.model=产品型号.Partitions=分区 ...
- 第三十七节,hashlib加密模块
在使用hashlib模块时需要先 import hashlib 引入模块 用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA ...
- getuid和geteuid的区别
getuid() : 函数返回一个调用程序的真实用户ID.表明当前运行位置程序的执行者. geteuid(): 函数返回返回一个有效用户的ID.(EUID)是你最初执行程序时所用的ID,该ID是程序 ...
- Docker私有仓库Registry 搭建
1. 关于Registry 官方的Docker hub是一个用于管理公共镜像的好地方,我们可以在上面找到我们想要的镜像,也可以把我们自己的镜像推送上去.但是,有时候,我们的使用场景需要我们拥有一个私有 ...
- centos中apache-tomcat的配置
在centos中配置Apache-toncat需要先安装jdk,前面文章已经写了怎么配置jdk,这里略过. 首先到官网下载好Apache-tomcat安装包,我这里下载的是apache-tomcat- ...
- javascript string去除两边空格
function trim(){ return this.replace(/(^\s*)|(\s*$)/g,""); g整个字符串 }