Codeforces 703D Mishka and Interesting sum 树状数组
2016-08-05 20:07
555 查看
题目:http://codeforces.com/contest/703/problem/D
题意:
给你n个数,m个区间询问 求区间出现次数为偶数次的数的异或和
分析:
如果是奇数次呢?我们知道a^a=0 所以直接前缀异或和就可以处理。所以思考有没有一种反异或运算呢?自己模拟一遍发现这样是错误的。换一个思路考虑,将奇数次变为偶数次来处理,只需要计算出所要查询的区间内不同的数的异或和a 再 与这个区间的前缀异或和n做一次异或运算得到b,就能够将奇数次变为偶数次来处理(a^b=n可以得到b=a^n),那么如何快速计算一个区间内不同的数的异或和呢?离线处理,结构体存储每个查询区间的左右边界,按照右边界排序,从左向右遍历序列 树状数组维护 不断的将数添加到树状数组,若当前位置的数存在前驱,则删除前驱 (删除就是再进行一次异或a^a=0) 对于共右边界的查询区间 一次遍历得到答案,然后继续遍历。
以上分析copy自:http://www.cnblogs.com/hsd-/p/5739834.html
题意:
给你n个数,m个区间询问 求区间出现次数为偶数次的数的异或和
分析:
如果是奇数次呢?我们知道a^a=0 所以直接前缀异或和就可以处理。所以思考有没有一种反异或运算呢?自己模拟一遍发现这样是错误的。换一个思路考虑,将奇数次变为偶数次来处理,只需要计算出所要查询的区间内不同的数的异或和a 再 与这个区间的前缀异或和n做一次异或运算得到b,就能够将奇数次变为偶数次来处理(a^b=n可以得到b=a^n),那么如何快速计算一个区间内不同的数的异或和呢?离线处理,结构体存储每个查询区间的左右边界,按照右边界排序,从左向右遍历序列 树状数组维护 不断的将数添加到树状数组,若当前位置的数存在前驱,则删除前驱 (删除就是再进行一次异或a^a=0) 对于共右边界的查询区间 一次遍历得到答案,然后继续遍历。
以上分析copy自:http://www.cnblogs.com/hsd-/p/5739834.html
#include<bits/stdc++.h> using namespace std; const int N=1e6+9; int a ,sum ,pre ,c ,res ; int n,m; struct seg { int l,r,idx; bool operator < (const seg& rhs) const { return r<rhs.r; } }p ; int lowbit(int x){ return x&(-x); } void add(int x,int v) { for(int i=x;i<=n;i+=lowbit(i)) c[i]^=v; } int getsum(int x) { int ans=0; for(int i=x;i>=1;i-=lowbit(i)) ans^=c[i]; return ans; } int main() { map<int,int>mp; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); sum[i]=sum[i-1]^a[i]; pre[i]=mp[a[i]]; mp[a[i]]=i; } scanf("%d",&m); for(int i=1;i<=m;i++){ scanf("%d%d",&p[i].l,&p[i].r); p[i].idx=i; } sort(p+1,p+1+m); int j=1; for(int i=1;i<=m;i++){ for(;j<=n&&j<=p[i].r;j++){ if(pre[j])add(pre[j],a[j]); add(j,a[j]); } res[p[i].idx]=sum[p[i].r]^sum[p[i].l-1]^getsum(p[i].r)^getsum(p[i].l-1); } for(int i=1;i<=m;i++)printf("%d\n",res[i]); return 0; }
相关文章推荐
- codeforces 703D D. Mishka and Interesting sum(树状数组)
- codeforces 703D Mishka and Interesting sum (树状数组区间异或)
- CodeForces 703D Mishka and Interesting sum (树状数组处理异或和)
- Codeforces 703D Mishka and Interesting sum (树状数组求区间内不同的数的异或和)
- CodeForces 703D Mishka and Interesting sum(树状数组 区间异或)
- CodeForces 703D Mishka and Interesting sum (树状数组 区间偶数异或)
- 【codeforces 703 D】【离线询问 树状数组 前驱思想 前缀异或和】D. Mishka and Interesting sum【 区间内出现次数偶数的数的异或和】
- codeforces 703D Mishka and Interesting sum(数状数组维护前缀)
- codeforces CF703D Mishka and Interesting sum 树状数组
- cf703D. Mishka and Interesting sum(树状数组)
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
- CodeForces 703D Mishka and Interesting sum
- CodeForces 703D Mishka and Interesting sum(线段树+异或)
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum (树状数组维护异或值) ★ ★
- CodeForces 703D Mishka and Interesting sum
- 【29.82%】【codeforces 703D】Mishka and Interesting sum
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum(离线树状数组)
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)