/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 344.0 KiB
#4 Accepted 4ms 340.0 KiB
#5 Accepted 10ms 532.0 KiB
#6 Accepted 17ms 532.0 KiB
#7 Accepted 24ms 532.0 KiB
#8 Accepted 35ms 532.0 KiB
#9 Accepted 214ms 788.0 KiB
#10 Accepted 405ms 780.0 KiB
#11 Accepted 401ms 784.0 KiB
#12 Accepted 279ms 692.0 KiB
#13 Wrong Answer 580ms 119.203 MiB
#14 Wrong Answer 584ms 119.02 MiB

Code

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;

struct Trie {
    static const int rangeSize = 2;
    static const char startingChar = '0';

    struct node {
        node *next[rangeSize]; 
        int cnt;
        node() {
            cnt = 0;
            for (int i = 0; i < rangeSize; i++)
                next[i] = nullptr;
        }
    } *root;
    Trie() {
        root = new node();
    }

    void Insert(const string &s) {
        node *cur = root;
        for (char ch : s) {
            int x = ch - startingChar;
            if (cur->next[x] == nullptr) {
                cur->next[x] = new node();
            }
            cur = cur->next[x];
            cur->cnt += 1;
        }
    }
    int Search(const string &s1, const string &s2) {
        node *cur = root;
        int ans = 0;
        for(int p = 0; p < s1.size(); p++) {
            int x = s1[p] - '0';
            int i = s2[p] - '0';
            if(x == 0) {
                if(cur->next[i ^ 1] != nullptr) ans += cur->next[i ^ 1]->cnt;
                if(cur->next[i] == nullptr) return ans;
                cur = cur->next[i];
            }
            else {
                if(cur->next[i ^ 1] == nullptr) return ans;
                cur = cur->next[i ^ 1];
            }
        }
        return ans;
    }
    void reset(node* cur) {
        for(int i = 0; i < rangeSize; i++)
            if(cur->next[i])
                reset(cur->next[i]);
        delete cur;
    }
    void clear() {
        reset(root);
        root = new node();
    }
    ~Trie() { // Destructor 
        reset(root);
    }
} trie;

// ai ^ i < (aj ^ i) ^ j
// ai ^ i < aj ^ j ^ i
// let, x = ai ^ i, y = aj ^ j
// x < y ^ i

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for(auto &i: a) cin >> i;
    int ans = 0;
    trie.clear();
    for(int i = n; i >= 1; i--) {
        int x = a[i - 1] ^ i;
        string s1 = bitset<30> (x).to_string();
        string s2 = bitset<30> (i).to_string();
        ans += trie.Search(s1, s2);
        trie.Insert(s1); // update y
    }
    cout << ans << endl;
    return;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1198 E. Good Signal Pairs
Language
C++17 (G++ 13.2.0)
Submit At
2025-05-22 11:17:30
Judged At
2025-05-22 11:17:30
Judged By
Score
12
Total Time
584ms
Peak Memory
119.203 MiB