Skip to content

Instantly share code, notes, and snippets.

@sudipt1999
Created August 5, 2019 17:39
Show Gist options
  • Save sudipt1999/18246168857650bb953326c7e40fd2e0 to your computer and use it in GitHub Desktop.
Save sudipt1999/18246168857650bb953326c7e40fd2e0 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long
void preprocess(int arr[], int n, vector<vector<int> >& cnt)
{
int i, j;
for (i = 0; i < 32; i++) {
cnt[i][0] = 0;
for (j = 0; j < n; j++) {
if (j > 0) {
cnt[i][j] = cnt[i][j - 1];
}
if (arr[j] & (1 << i))
cnt[i][j]++;
}
}
}
int findXOR(int L, int R, const vector<vector<int> > cnt)
{
int ans = 0;
int noOfOnes;
int i, j;
for (i = 0; i < 32; i++) {
noOfOnes = cnt[i][R] - ((L > 0) ? cnt[i][L - 1] : 0);
if (noOfOnes & 1) {
ans += (1 << i);
}
}
return ans;
}
int main() {
ll t; cin>>t;
while(t--) {
int n ; cin >>n;
int arr[n]={0};
for(ll i = 0 ; i < n ; i++)
cin>>arr[i];
vector<vector<int> > cnt(32, vector<int>(n));
preprocess(arr, n, cnt);
ll ans = 0;
ll a1,a2;
// first loop for total number of cases
for(ll i = 1 ; i < n ; i++){
for( ll j = 0 ; j < i ; j++){
a1 = findXOR(j, i-1, cnt);
a2 = findXOR(i, i, cnt);
for(ll k = i ; k < n ; k++){
// cout<<" LB : "<<j<<" MID " <<i<<" END : "<<k<<endl;
if(a1 == a2){
//cout<<"A1 : "<<a1<<" A2 : "<<a2<<endl;
ans++;
}
}
}
}
cout<<ans<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment