1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public: int countTriplets(vector<int>& arr) { const int n = arr.size(); vector<int> dp(n + 1); for (int i = 0; i < n; ++i) dp[i + 1] = dp[i] ^ arr[i]; int ans = 0; unordered_map<int, int> freq, sum; for (int i = 0; i < n; ++i) { if (freq.find(dp[i + 1]) != freq.end()) ans += freq[dp[i + 1]] * i - sum[dp[i + 1]]; ++freq[dp[i]]; sum[dp[i]] += i; } return ans; } };
|