fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int testCases;
  9. cin >> testCases;
  10.  
  11. while (testCases--) {
  12. int n;
  13. cin >> n;
  14.  
  15. vector<int> arr(n + 1);
  16. for (int i = 1; i <= n; i++) cin >> arr[i];
  17.  
  18. vector<int> prefixXor(n + 1, 0);
  19. for (int i = 1; i <= n; i++) {
  20. prefixXor[i] = prefixXor[i - 1] ^ arr[i];
  21. }
  22.  
  23. vector<int> leftZeroXorCount(n + 1, 0);
  24. vector<int> rightZeroXorCount(n + 1, 0);
  25.  
  26. int currentXor = 0;
  27. unordered_map<int, int> xorFrequency;
  28. xorFrequency[currentXor] = 1;
  29.  
  30. long long totalZeroXorSubarrays = 0;
  31.  
  32. for (int i = 1; i <= n; i++) {
  33. currentXor ^= arr[i];
  34. totalZeroXorSubarrays += xorFrequency[currentXor];
  35. xorFrequency[currentXor]++;
  36. }
  37.  
  38. for (int i = 1; i <= n; i++) {
  39. int count = 0;
  40. for (int l = 1; l <= i; l++) {
  41. if ((prefixXor[i] ^ prefixXor[l - 1]) == 0) {
  42. count++;
  43. }
  44. }
  45. leftZeroXorCount[i] = count;
  46. }
  47.  
  48. for (int i = n; i >= 1; i--) {
  49. int count = 0;
  50. for (int r = i; r <= n; r++) {
  51. if ((prefixXor[r] ^ prefixXor[i - 1]) == 0) {
  52. count++;
  53. }
  54. }
  55. rightZeroXorCount[i] = count;
  56. }
  57.  
  58. for (int i = 1; i < n; i++) {
  59. totalZeroXorSubarrays += (long long)leftZeroXorCount[i] * rightZeroXorCount[i + 1];
  60. }
  61.  
  62. long long totalTriplets = (1LL * n * (n + 1) * (n + 2)) / 6;
  63.  
  64. long long result = totalTriplets - totalZeroXorSubarrays;
  65.  
  66. cout << result << "\n";
  67. }
  68.  
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0.01s 5288KB
stdin
6
2
1 1
1 1
2
4 8
1 1
5
1 1 727 1 1
1 1 1 1 1
2
3 11
1 1
3
2 7 11
1 1 1
3
7 12 13
1 1 1
stdout
3
1
4
1
33
1