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的更多相关文章

  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. sha加密算法

    密钥生成 公钥(e,n)  私钥(d,n) 找两个互质的大素数p和q, 计算n=p*p, f(n)=(p-1)*(q-1) 选择随机整数e(e和f(n)互质) de=f(n)mod 1 利用公钥加密 ...

  2. CSS传统布局之display属性+float属性+position属性

    这三个属性是传统网页布局中经常用到的属性. 读这篇文章之前,希望你对css布局模型已经有了一定的了解.因为本文的三个属性是和css三个布局模型紧密联系在一起的.因此,如若你并不了解,我推荐你先看一下c ...

  3. python基础(三)列表、数组、字典

    列表与元组 列表是最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 1 >>> names = ['wangeq','zlx','jack','rose ...

  4. 关于C++数组的几点讨论

    数组名为何物? int main() { , , , , }; int *pnumber = number; cout << sizeof(number) << endl; c ...

  5. JTAG上有多个设备时,该如何接呢?

    首先要了解JTAG管脚相关定义,具有JTAG口的芯片都有如下JTAG引脚定义(是相对芯片): TCK——测试时钟输入: TDI——测试数据输入: TDO——测试数据输出: TMS——测试模式选择,TM ...

  6. hdu_5763_Another Meaning(dp)

    题目链接:hdu_5763_Another Meaning 题意: 一个文本串A,一个模式串B,如果文本串含有模式串B,那么就能组合成多种意思,如下: In the first case, “ heh ...

  7. nefu 519 昨日重现

    昨日重现 Problem : 519 Time Limit : 1000ms Memory Limit : 65536K description 兴安黑熊在高中学习数学时,曾经知道这样一个公式:f(n ...

  8. TextBox只读时不能通过后台赋值取值解决办法

    给页面的TextBox设置ReadOnly="True"时,在后台代码中不能赋值取值,下边几种方法可以避免:  1.不设置ReadOnly,设置onfocus=this.blur( ...

  9. Spring Security-用户密码自定义加密

    public class SunPasswordEncoder implements PasswordEncoder{ //@实现加密的方法,既将明文转换为密文的方法 public String en ...

  10. javascript 浏览器

    hashchange事件 window.location.hash.slice(1) 添加和修改历史记录条目LINKHTML5引进了history.pushState()方法和history.repl ...