/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 532.0 KiB

Code

#include <iostream>
#include <vector>

using namespace std;

const int BITS = 30;

struct Node {
    Node* nxt[2];
    int cnt;

    Node() {
        nxt[0] = nxt[1] = nullptr;
        cnt = 0;
    }
};

void insert(Node* root, int x) {
    Node* cur = root;
    for (int i = BITS; i >= 0; --i) {
        int b = (x >> i) & 1;
        if (!cur->nxt[b]) cur->nxt[b] = new Node();
        cur = cur->nxt[b];
        cur->cnt++;
    }
}

long long count_greater(Node* root, int c, int v) {
    long long res = 0;
    Node* cur = root;
    for (int i = BITS; i >= 0; --i) {
        if (!cur) break;

        int cb = (c >> i) & 1;
        int vb = (v >> i) & 1;

        if (cb == 0) {
            int want = vb ^ 1;
            if (cur->nxt[want]) res += cur->nxt[want]->cnt;
            cur = cur->nxt[vb];
        } else {
            cur = cur->nxt[vb ^ 1];
        }
    }
    return res;
}

void clear(Node* node) {
    if (!node) return;
    clear(node->nxt[0]);
    clear(node->nxt[1]);
    delete node;
}

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto& x : a) cin >> x;

    Node* root = new Node();
    long long ans = 0;

    for (int i = n - 1; i >= 0; --i) {
        int val = (i + 1) ^ a[i];
        ans += count_greater(root, val, i + 1);
        insert(root, val);
    }

    cout << ans << '\n';
    clear(root);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) solve();
}

Information

Submit By
Type
Pretest
Problem
P1198 E. Good Signal Pairs
Language
C++17 (G++ 13.2.0)
Submit At
2025-06-13 17:55:22
Judged At
2025-06-13 17:55:22
Judged By
Score
10
Total Time
4ms
Peak Memory
532.0 KiB