[CF 703D]Mishka and Interesting sum
2016-10-13 22:03
489 查看
Description
给出n个数,m次询问,每次询问区间[l,r]中出现次数为偶数的数的异或和。n,m<=10^6,所有数字<=10^9
Solution
很有必要说明一下,这道题的时限是3.5s所以说NlogN是能过的,(⊙v⊙)嗯
然后还能怎么打?
如果出现次数是奇数,直接求区间异或和就好了。
如果是偶数?那就异或上区间所有出现过的数的异或和就好了。
不喜欢hash,用map搞一波~
不过得离线用树状数组喽~
Code
#include <map> #include <cstdio> #include <cstring> #include <algorithm> #define fo(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int N=1e6+5; map<int,int> h; struct note {int l,r,id;}ask ; int an ,a ,sum ,t ,n,m; bool cmp(note x,note y) {return x.r<y.r;} void add(int x,int y) { for(;x<=n;x+=x&-x) t[x]^=y; } int query(int x) { int ans=0; for(;x;x-=x&-x) ans^=t[x]; return ans; } int main() { scanf("%d",&n); fo(i,1,n) scanf("%d",&a[i]),sum[i]=sum[i-1]^a[i]; scanf("%d",&m); fo(i,1,m) scanf("%d%d",&ask[i].l,&ask[i].r),ask[i].id=i; sort(ask+1,ask+m+1,cmp);int j=0; fo(i,1,n) { if (h[a[i]]) add(h[a[i]],a[i]); add(i,a[i]);h[a[i]]=i; while (ask[j+1].r==i) { j++;int l=ask[j].l,r=ask[j].r; an[ask[j].id]=sum[r]^sum[l-1]^query(r)^query(l-1); } } fo(i,1,m) printf("%d\n",an[i]); }
相关文章推荐
- CF 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 703D Mishka and Interesting sum(树状数组 区间异或)
- Codeforces 703D Mishka and Interesting sum 树状数组
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
- 【29.82%】【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 703D Mishka and Interesting sum (树状数组处理异或和)
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
- CF #365 703D. Mishka and Interesting sum
- Mishka and Interesting sum CodeForces - 703D(离线处理)
- codeforces 703D Mishka and Interesting sum (树状数组区间异或)
- [CF#365 (Div. 2) Mishka and Interesting sum] 线段树离线处理区间不同数
- codeforces 703D D. Mishka and Interesting sum(树状数组)