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. Spring 表单提交,后台自动封装有集合对象的对象

    from表单: <form action="${pageContext.request.contextPath}/vote/save" method="post&q ...

  2. opencv使用convexityDefects计算轮廓凸缺陷

    引自:http://www.xuebuyuan.com/1684976.html http://blog.csdn.net/lichengyu/article/details/38392473 htt ...

  3. Android中的Fragment页面切换和selector选择器

    效果如图: 提示:下面是用的整个的图片 下面看代码: //--------------------这是主页面布局文件----------------------- <?xml version=& ...

  4. Chapter 2 Open Book——14

    I backpedaled. "They seemed nice enough to me. I just noticed they keptto themselves. 我改口说道,他们看 ...

  5. pdo 抽象层连接数据库

    <?php   header("content-type:text/html; charset=utf8");    try{          $pdo=new PDO(' ...

  6. 新随笔ps泡泡制作

    http://jingyan.baidu.com/article/4d58d5413568a79dd4e9c016.html

  7. Java C# .net 和 C C++ 跨平台的区别

    当前主流的手机平台很多,而各个主流的平台的语言支持大同小异:如 1.windows系统WP8上主要支持 VB, C#, c/c++, 2.苹果系统ios上支持 object-c, c/c++ 3.an ...

  8. echo json数据给ajax后, 需要加上exit,防止往下执行,带上其他数据,到时ajax失败

    01返回json数据给ajax后需要加上exit.返回json数据前不能有其他输出 function apply(){ if(IS_POST){$info['status'] = 1; echo js ...

  9. KMP算法类习题——字符串匹配

    Description For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p- ...

  10. ios做的两个矩形相交叉

    #import "ViewController.h" @interface ViewController (){    UIView *_gee;  //定义的实例变量    UI ...