Untitled

 avatar
unknown
c_cpp
9 months ago
1.2 kB
17
Indexable
#include <bits/stdc++.h>
using namespace std;
#define ll long long

void solve() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++) cin >> a[i];

  vector<ll> p = a;
  for (int i = 1; i <= n; i++) p[i] ^= p[i - 1];

  vector<ll> all;
  for (ll u : p) all.push_back(u);
  sort(all.begin(), all.end());

  auto get = [&](ll x) {
    return lower_bound(all.begin(), all.end(), x) - all.begin();
  };
  
  vector<vector<int>> where(n + 1);
  for (int i = 0; i <= n; i++) where[get(p[i])].push_back(i);

  ll ans = 0;
  for (int x = 0; x <= n; x++) {
    auto& V = where[x];
    int m = V.size();

    // focus on unequal arrays first
    ll sum_before = 0, num_before = 0;
    for (int i : V) {
      ll y = (i * 1LL * (i + 1)) / 2;
      y -= i * num_before - sum_before;
      num_before += 1;
      sum_before += i;
      ans += y;
    }

    // now, for equal arrays
    ll sum = 0;
    for (int i = 1; i < m; i++) {
      sum += i * 1LL * (V[i] - V[i - 1] - 1);
      ans += sum;
    }
  }

  cout << ans << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tt = 1, tc = 1;
  cin >> tt;
  while (tt--) {
    cout << "Case #" << tc++ << ": ";
    solve();
  }
}
Editor is loading...
Leave a Comment