/ 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 4ms 576.0 KiB
#5 Accepted 4ms 532.0 KiB
#6 Accepted 6ms 536.0 KiB
#7 Accepted 8ms 576.0 KiB
#8 Accepted 13ms 532.0 KiB
#9 Accepted 42ms 788.0 KiB
#10 Accepted 81ms 828.0 KiB
#11 Accepted 81ms 820.0 KiB
#12 Accepted 87ms 576.0 KiB
#13 Accepted 427ms 216.449 MiB
#14 Accepted 420ms 216.531 MiB
#15 Accepted 178ms 72.551 MiB
#16 Accepted 108ms 43.582 MiB
#17 Accepted 131ms 22.152 MiB
#18 Accepted 119ms 11.27 MiB
#19 Accepted 98ms 2.52 MiB
#20 Accepted 89ms 1.27 MiB
#21 Accepted 84ms 1.27 MiB

Code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAXNODES = 30 * 300001;
int Next[2][MAXNODES];    // child pointers: Next[bit][node]
bool created[MAXNODES];   // whether node exists
ll EndCnt[MAXNODES];      // number of values ending at this node
ll SubCnt[MAXNODES];      // number of values passing through this node

int sz;
ll answer;
int BITLEN = 30;

void insert_val(int x) {
    int v = 0;
    for (int b = BITLEN - 1; b >= 0; --b) {
        int c = (x >> b) & 1;
        if (!created[ Next[c][v] ]) {
            Next[c][v] = ++sz;
            created[ sz] = true;
        }
        v = Next[c][v];
        SubCnt[v]++;
    }
    EndCnt[v]++;
}

// count how many y inserted satisfy y > Q
void search(int X, int Q) {
    int v = 0;
    for (int b = BITLEN - 1; b >= 0; --b) {
       //if (!created[v]) return;
        int xb = (X >> b) & 1;
        int qb = (Q >> b) & 1;
        int yb = qb ^ 1;

        if (xb == 0) {
            int alt = Next[yb][v];
            if (created[alt]) answer += SubCnt[alt];
            if(!created[Next[yb^1][v]])break;
            v = Next[yb^1][v];
        } else {
            v = Next[yb][v];
            if(!created[v])break;
        }
    }
  //  if (created[v]) answer += EndCnt[v];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--) {
        int N;
        cin >> N;
        vector<int> a(N);
        for (int i = 0; i < N; ++i) cin >> a[i];

        // Reset trie
        sz = 0;
        answer = 0;
        int nodes = BITLEN * N + 5;
        for (int i = 0; i <= nodes; ++i) {
            created[i] = false;
            Next[0][i] = Next[1][i] = 0;
            SubCnt[i] = EndCnt[i] = 0;
        }
        for (int i = N - 1; i >= 0; --i) {
            int xi = i + 1;
            int fi = xi ^ a[i];
            search(fi, xi);
            insert_val(fi);
        }

        cout << answer << '\n';
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1198 E. Good Signal Pairs
Language
C++17 (G++ 13.2.0)
Submit At
2025-05-23 10:16:04
Judged At
2025-05-23 10:16:04
Judged By
Score
100
Total Time
427ms
Peak Memory
216.531 MiB