/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 764.0 KiB
#2 Wrong Answer 1ms 532.0 KiB
#3 Wrong Answer 1ms 532.0 KiB

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 = 0;
    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) {
        debug(i);
        erase(a[i] ^ i);
        debug(ans);
        ans += calc(i);
        debug(ans);
    }
    cout << ans << '\n';
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    trie root = trie();
    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:14:10
Judged At
2025-06-24 13:14:10
Judged By
Score
1
Total Time
1ms
Peak Memory
764.0 KiB