/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 320.0 KiB
#3 Accepted 1ms 392.0 KiB
#4 Accepted 4ms 532.0 KiB
#5 Accepted 10ms 572.0 KiB
#6 Accepted 17ms 580.0 KiB
#7 Accepted 47ms 592.0 KiB
#8 Accepted 34ms 612.0 KiB
#9 Accepted 242ms 788.0 KiB
#10 Accepted 403ms 792.0 KiB
#11 Accepted 399ms 784.0 KiB
#12 Accepted 294ms 560.0 KiB
#13 Accepted 584ms 120.27 MiB
#14 Accepted 583ms 120.332 MiB
#15 Accepted 401ms 28.504 MiB
#16 Accepted 237ms 18.008 MiB
#17 Accepted 455ms 9.539 MiB
#18 Accepted 369ms 5.391 MiB
#19 Accepted 341ms 1.566 MiB
#20 Accepted 329ms 788.0 KiB
#21 Accepted 460ms 1.199 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;
        }
    }
    ll Search(const string &s1, const string &s2) {
        node *cur = root;
        ll 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() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    for(auto &i: a) cin >> i;
    ll ans = 0;
    trie.clear();
    for(ll i = n; i >= 1; i--) {
        ll x = a[i - 1] ^ i;
        string s1 = bitset<31> (x).to_string();
        string s2 = bitset<31> (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:16:23
Judged At
2025-05-22 11:16:23
Judged By
Score
100
Total Time
584ms
Peak Memory
120.332 MiB