/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 576.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 4ms 1.016 MiB
#5 Accepted 11ms 5.059 MiB
#6 Accepted 19ms 9.344 MiB
#7 Accepted 27ms 13.27 MiB
#8 Accepted 41ms 20.52 MiB
#9 Accepted 244ms 112.02 MiB
#10 Accepted 451ms 209.469 MiB
#11 Accepted 410ms 209.395 MiB
#12 Accepted 306ms 128.086 MiB
#13 Accepted 515ms 120.363 MiB
#14 Accepted 506ms 120.379 MiB
#15 Accepted 288ms 82.773 MiB
#16 Accepted 172ms 52.02 MiB
#17 Accepted 264ms 90.523 MiB
#18 Accepted 259ms 95.094 MiB
#19 Accepted 273ms 113.051 MiB
#20 Accepted 305ms 126.695 MiB
#21 Accepted 389ms 192.121 MiB

Code

#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif

using namespace std;
#define int long long
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';

const int MX = 30, N = 3e5 + 5;
int a[N];

struct trie {
    int tot;
    trie *a[2];

    trie() : tot(0) {
        a[0] = a[1] = nullptr;
    }
};

trie root;

void insert(int x) {
    trie *tmp = &root;
    tmp->tot++;
    for (int i = MX; ~i; --i) {
        bool f = (x >> i) & 1;
        if (tmp->a[f] == nullptr) tmp->a[f] = new trie();
        tmp = tmp->a[f];
        tmp->tot++;
    }
}

void erase(int x) {
    trie *tmp = &root;
    tmp->tot--;
    for (int i = MX; ~i; --i) {
        bool f = (x >> i) & 1;
        tmp = tmp->a[f];
        tmp->tot--;
    }
}

int calc(int i) {
    trie *tmp = &root;
    int ret = 0, left = i ^ a[i];
    for (int mask = MX; ~mask; --mask) {
        if (tmp == nullptr) break;
        int l = (left >> mask) & 1;
        if (l == 1) {
            int j = ((i >> mask) & 1) ^ 1;
            tmp = tmp->a[j];
        }
        else {
            int j = ((i >> mask) & 1) ^ 1;
            if (tmp->a[j] != nullptr) ret += tmp->a[j]->tot;
            j ^= 1;
            tmp = tmp->a[j];
        }
    }
    return ret;
}

void shelby() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    root = trie();
    for (int i = 1; i <= n; ++i) insert(a[i] ^ i);
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        erase(a[i] ^ i);
        ans += calc(i);
    }
    cout << ans << '\n';
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case " << _ << ": ";
        shelby();
    }
}

Information

Submit By
Type
Submission
Problem
P1198 E. Good Signal Pairs
Language
C++17 (G++ 13.2.0)
Submit At
2025-06-24 13:26:14
Judged At
2025-06-24 13:26:14
Judged By
Score
100
Total Time
515ms
Peak Memory
209.469 MiB