/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 4ms 1.246 MiB
#5 Accepted 11ms 5.09 MiB
#6 Accepted 20ms 9.27 MiB
#7 Accepted 28ms 13.066 MiB
#8 Accepted 40ms 20.52 MiB
#9 Accepted 219ms 111.801 MiB
#10 Accepted 410ms 209.387 MiB
#11 Accepted 416ms 209.477 MiB
#12 Accepted 284ms 127.961 MiB
#13 Accepted 508ms 120.316 MiB
#14 Accepted 509ms 120.223 MiB
#15 Accepted 290ms 82.77 MiB
#16 Accepted 167ms 51.836 MiB
#17 Accepted 266ms 90.52 MiB
#18 Accepted 268ms 95.02 MiB
#19 Accepted 274ms 113.27 MiB
#20 Accepted 292ms 126.816 MiB
#21 Accepted 397ms 192.023 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 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:27:17
Judged At
2025-06-24 13:27:17
Judged By
Score
100
Total Time
509ms
Peak Memory
209.477 MiB