/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 4ms 1.02 MiB
#5 Accepted 15ms 5.27 MiB
#6 Accepted 38ms 9.504 MiB
#7 Accepted 23ms 13.312 MiB
#8 Accepted 47ms 20.617 MiB
#9 Accepted 204ms 112.02 MiB
#10 Accepted 373ms 209.488 MiB
#11 Accepted 373ms 209.391 MiB
#12 Accepted 254ms 128.152 MiB
#13 Accepted 379ms 118.969 MiB
#14 Accepted 382ms 119.02 MiB
#15 Accepted 213ms 82.383 MiB
#16 Accepted 126ms 51.828 MiB
#17 Accepted 211ms 90.344 MiB
#18 Accepted 224ms 95.02 MiB
#19 Accepted 266ms 113.27 MiB
#20 Accepted 260ms 126.754 MiB
#21 Accepted 368ms 192.27 MiB

Code

#include <bits/stdc++.h>

using namespace std;

struct Trie {
  static const int B = 31;
  struct Node {
    Node *nxt[2];
    int cnt;
    Node () {
      fill(begin(nxt), end(nxt), nullptr);
      cnt = 0;
    }
  } *who;
  Trie () { who = new Node(); }
  void insert(int val) {
    Node *mover = who;
    mover -> cnt += 1;
    for (int i = B - 1; ~i; --i) {
      int b = val >> i & 1;
      if (mover -> nxt[b] == nullptr) mover -> nxt[b] = new Node();
      mover = mover -> nxt[b], mover -> cnt += 1;
    }
  }
  int Qry1 (int x, int k) { // Number of values (val ^ x) < k;
    Node *mover = who;
    int ans = 0;
    for (int i = B - 1; ~i; --i) {
      int d = x >> i & 1;
      if (k >> i & 1) {
        if (mover -> nxt[d]) ans += mover -> nxt[d] -> cnt;
        if (mover -> nxt[!d]) mover = mover -> nxt[!d];
        else break;
      } else if (mover -> nxt[d]) mover = mover -> nxt[d];
      else break;
    }
    return ans;
  }
  int Qry2 (int x, int k) { // Number of values (val ^ x) > k;
    Node *mover = who;
    int ans = 0;
    for (int i = B - 1; ~i; --i) {
      int d = x >> i & 1;
      if (~k >> i & 1) {
        if (mover -> nxt[!d]) ans += mover -> nxt[!d] -> cnt;
        if (mover -> nxt[d]) mover = mover -> nxt[d];
        else break;
      } else if (mover -> nxt[!d]) mover = mover -> nxt[!d];
      else break;
    }
    return ans;
  }
};

void solve(int cs) {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int64_t ans = 0;
  Trie trie;
  for (int i = n - 1; ~i; --i) {
    ans += trie.Qry2(i + 1, a[i] ^ (i + 1));
    trie.insert(a[i] ^ (i + 1));
  }
  cout << ans << "\n";
}

int32_t main() {
  cin.tie(0) -> sync_with_stdio(0);
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1198 E. Good Signal Pairs
Language
C++17 (G++ 13.2.0)
Submit At
2025-06-14 01:42:17
Judged At
2025-06-14 01:42:17
Judged By
Score
100
Total Time
382ms
Peak Memory
209.488 MiB