617E - XOR and Favorite Number
2017-08-05 14:46
295 查看
莫队,用统计每个块内每个值出现了几次。
对数列进行前缀异或和,每个查询因此相当于问a[l-1]^a[r]是否为k。
因此l移动时统计a[l-1]^k出现的次数,r移动时统计a[r]^k出现的次数。
没了。
傻逼一样不知道调些什么。
对数列进行前缀异或和,每个查询因此相当于问a[l-1]^a[r]是否为k。
因此l移动时统计a[l-1]^k出现的次数,r移动时统计a[r]^k出现的次数。
没了。
傻逼一样不知道调些什么。
#include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<map> #include<vector> #include<algorithm> using namespace std; #define rep(i,j,k) for(i=j;i<=k;++i) #define per(i,j,k) for(i=j;i>=k;--i) #define sqr(x) ((x)*(x)) #define G getchar() #define LL long long #define pll pair<LL,LL> #define mkp make_pair #define X first #define Y second #define N 100005 #define inf 2000005 struct QUERY{int L,R,wz;}qu ; int n,m,k,a ,s,t,mo ,cnt[inf]; LL ans ,now; int read(){ int x=0;char ch=G; for(;ch<48||ch>57;ch=G); for(;ch>47&&ch<58;ch=G)x=x*10+ch-48; return x; } bool cmp(QUERY x,QUERY y){ return mo[x.L]==mo[y.L]?x.R<y.R:mo[x.L]<mo[y.L]; } int main(){ int i,l,r; n=read();m=read();k=read(); s=ceil(sqrt(n)); rep(i,1,n)mo[i]=t+=i%s==1,a[i]=read()^a[i-1]; rep(i,1,m){ l=read();r=read();qu[i]=(QUERY){l,r,i}; } sort(qu+1,qu+m+1,cmp); l=r=qu[1].L;now=k==(a[l-1]^a[r]); rep(i,1,m){ while(r<qu[i].R){ ++cnt[a[r++]]; now+=cnt[a[r]^k]; if((a[l-1]^a[r])==k)++now; } while(r>qu[i].R){ now-=cnt[a[r]^k]; if((a[l-1]^a[r])==k)--now; --cnt[a[--r]]; } while(l>qu[i].L){ ++cnt[a[--l]]; now+=cnt[a[l-1]^k]; if((a[l-1]^a[r])==k)++now; } while(l<qu[i].L){ now-=cnt[a[l-1]^k]; if((a[l-1]^a[r])==k)--now; --cnt[a[l++]]; } ans[qu[i].wz]=now; } rep(i,1,m)printf("%lld\n",ans[i]); return 0; }
相关文章推荐
- [CQOI 2018]异或序列&[Codeforces 617E]XOR and Favorite Number
- 【CodeForces-617E】XOR and Favorite Number 莫队(好玩题)
- 【CF 617E】 XOR and Favorite Number (Mo's algorithm)
- CodeForces - 617E:XOR and Favorite Number (莫队算法入门题)
- 【第400篇题解纪念2016年10月28日】【28.10%】【codeforces 617E】XOR and Favorite Number
- 【莫队算法】[CodeForces - 617E/Round #340]XOR and Favorite Number
- Codeforces 617E:XOR and Favorite Number 莫队算法
- 莫队算法(CF #340 (Div. 2) E. XOR and Favorite Number)
- CodeForces 617 E. XOR and Favorite Number
- 【CodeForces617E】XOR and Favorite Number
- E. XOR and Favorite Number
- E. XOR and Favorite Number
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number
- Codeforces Round #340 (Div. 2)E. XOR and Favorite Number 莫队算法
- [CF617E]XOR and Favorite Number
- Codeforces Round #340 (Div. 2) E - XOR and Favorite Number (莫队算法)
- cf617 E. XOR and Favorite Number【莫队算法】
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
- [CF617E]XOR and Favorite Number/[CQOI2018]异或序列
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number