Accepted
Code
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct node {
int val = -1, cnt = 0, l = 0, r = 0;
} null;
struct bor {
vector<node> t;
int timer;
void b () {
t.resize(2);
}
void add (int x) {
int v = 1;
t[v].cnt++;
for (int i = 30; i >= 0; i--) {
if (!(x >> i & 1)) {
if (!t[v].l) {
t.push_back(null);
t[v].l = t.size() - 1;
}
v = t[v].l;
}
else {
if (!t[v].r) {
t.push_back(null);
t[v].r = t.size() - 1;
}
v = t[v].r;
}
t[v].cnt++;
}
}
int get (int id, int x) {
int v = 1;
int ans = 0;
for (int i = 30; i >= 0 && v; i--) {
if (!(x >> i & 1)) {
if (id >> i & 1) {
ans += (t[t[v].l].cnt);
v = t[v].r;
}
else {
ans += (t[t[v].r].cnt);
v = t[v].l;
}
}
else {
if (id >> i & 1) {
v = t[v].l;
}
else {
v = t[v].r;
}
}
}
return ans;
}
};
void iusearchbtw () {
int n; cin >> n;
vector<int> a(n + 1);
bor pihal;
pihal.b();
int ans = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
// for (int i = 1; i <= n; i++) {
// for (int j = i + 1; j <= n; j++) {
// if ((a[i] ^ i) < ((i ^ a[j]) ^ j)) {
// cout << i << ' ' << j << endl;
// }
// }
// }
for (int i = n; i >= 1; i--) {
ans += pihal.get(i, (a[i] ^ i));
// cout << pihal.get(i, (a[i] ^ i)) << ' ';
pihal.add((a[i] ^ i));
}
// cout << endl;
// for (int i = 1; i <= n; i++) cout << (a[i] ^ i) << ' ';
// cout << endl;
cout << ans << endl;
}
signed main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
cin >> tt;
while (tt --> 0) iusearchbtw();
}
Information
- Submit By
- Type
- Submission
- Problem
- P1198 E. Good Signal Pairs
- Contest
- Brain Booster #10
- Language
- C++17 (G++ 13.2.0)
- Submit At
- 2025-06-13 17:34:24
- Judged At
- 2025-06-13 17:34:24
- Judged By
- Score
- 100
- Total Time
- 442ms
- Peak Memory
- 131.047 MiB