/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 5ms 536.0 KiB
#5 Accepted 13ms 532.0 KiB
#6 Accepted 20ms 544.0 KiB
#7 Accepted 24ms 532.0 KiB
#8 Accepted 40ms 532.0 KiB
#9 Accepted 252ms 720.0 KiB
#10 Accepted 507ms 532.0 KiB
#11 Accepted 533ms 784.0 KiB
#12 Accepted 350ms 696.0 KiB
#13 Accepted 595ms 119.055 MiB
#14 Accepted 589ms 119.062 MiB
#15 Accepted 356ms 28.066 MiB
#16 Accepted 211ms 17.578 MiB
#17 Accepted 357ms 9.652 MiB
#18 Accepted 326ms 5.09 MiB
#19 Accepted 350ms 1.566 MiB
#20 Accepted 384ms 828.0 KiB
#21 Accepted 496ms 1.02 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct Trie {
    static const int rangeSize = 2; // for 0 and 1 (0, 1)

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

    Trie() {
        root = new node();
    }

    void trieInsert(const string &s) {
        node *cur = root;
        for (char ch : s) {
            int x = ch - '0'; //
            if (cur->next[x] == nullptr) {
                cur->next[x] = new node();
            }
            cur = cur->next[x];
            cur->cnt += 1;
        }
        cur->completedNum = true;
    }

    ll trieSearch(const string &s1, const string &s2) {
        ll ans = 0;
        node *cur = root;
        for (int i = 0; i < s1.size(); i++) {
            int x = s1[i] - '0'; 
            int y = s2[i] - '0';
            if(x == 0)
            {
                if(y == 0)
                {
                    if(cur->next[1] != nullptr)ans += cur->next[1]->cnt;
                }
                else{
                    if(cur->next[0] != nullptr)ans += cur->next[0]->cnt;
                }

                if(cur->next[y] != nullptr)
                {
                    cur = cur->next[y];
                }
                else{
                    return ans;
                }
            }
            else{
                if(y == 0)
                {
                    if(cur->next[1] != nullptr)
                    {
                        cur = cur->next[1];
                    }
                    else{
                        return ans;
                    }
                }
                else{
                    if(cur->next[0] != nullptr)
                    {
                        cur = cur->next[0];
                    }
                    else{
                        return ans;
                    }
                }
            }
        }
        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); // Delete all nodes
        root = new node(); // Re-initialize root node for reuse
    }

    ~Trie() { // Destructor 
        reset(root);
    }
} trie;

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    while (tc--) {
        trie.clear(); // <===
        int n, q;
        cin >> n;
        vector<int>v(n);
        for (int i = 0; i < n; i++) {
            cin >> v[i];
        }
        ll ans = 0;
        for(int i = n; i >= 1; i--)
        {
            int val = (v[i - 1] ^ i);
            string s1 = bitset<31> (val).to_string();
            string s2 = bitset<31> (i).to_string();
            ans += trie.trieSearch(s1, s2);
            trie.trieInsert(s1);
        }
        cout << ans << endl;
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1198 E. Good Signal Pairs
Language
C++17 (G++ 13.2.0)
Submit At
2025-06-12 10:16:10
Judged At
2025-06-12 10:16:10
Judged By
Score
100
Total Time
595ms
Peak Memory
119.062 MiB