P3917 异或序列
题意
给出序列A_1,A_2,......,A_NA1,A2,⋯,AN,求所有子区间的xor和(1<=n<=1e5,0<=A[i]<=1e9)
分析
按位算贡献即可,有两种情况,和wannafly4的B差不多
#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn = 1e5 + 10; ll a[maxn]; ll n; ll cnt[3]; int main() { scanf("%lld", &n); for(int i = 1; i <= n; i++) { scanf("%lld", &a[i]); a[i]^=a[i-1]; } ll sum = 0; for(int i = 0; i <= 32; i++) { ll ans = 0; cnt[0]=0, cnt[1]=0; for(int j = 1; j <= n; j++) { cnt[((a[j]>>i)&1)]++; if(((a[j]>>i)&1)==1) { ans++; ans+=cnt[!((a[j]>>i)&1)]; } else { ans+=cnt[!((a[j]>>i)&1)]; } } sum=sum+ans*(1<<i); } printf("%lld ", sum); return 0; }