/ 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 3ms 532.0 KiB
#5 Accepted 7ms 532.0 KiB
#6 Accepted 10ms 600.0 KiB
#7 Accepted 10ms 532.0 KiB
#8 Accepted 38ms 580.0 KiB
#9 Accepted 186ms 1.062 MiB
#10 Accepted 360ms 1.02 MiB
#11 Accepted 361ms 1.031 MiB
#12 Accepted 302ms 980.0 KiB
#13 Accepted 434ms 131.047 MiB
#14 Accepted 436ms 131.047 MiB
#15 Accepted 282ms 49.375 MiB
#16 Accepted 183ms 49.043 MiB
#17 Accepted 309ms 25.77 MiB
#18 Accepted 308ms 13.398 MiB
#19 Accepted 330ms 3.707 MiB
#20 Accepted 302ms 1.488 MiB
#21 Accepted 442ms 2.203 MiB

Code

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


#define int long long

struct node {
        int val = -1, cnt = 0, l = 0, r = 0;
} null;

struct bor {
        vector<node> t;
        int timer;

        void b () {
                t.resize(2);
        }
        
        void add (int x) {
                int v = 1;
                t[v].cnt++;
                for (int i = 30; i >= 0; i--) {
                        if (!(x >> i & 1)) {
                                if (!t[v].l) {
                                        t.push_back(null);
                                        t[v].l = t.size() - 1;
                                }
                                v = t[v].l;
                        }
                        else {
                                if (!t[v].r) {
                                        t.push_back(null);
                                        t[v].r = t.size() - 1;
                                }
                                v = t[v].r;
                        }

                        t[v].cnt++;
                }
        }
        int get (int id, int x) {
                int v = 1;
                int ans = 0;
                for (int i = 30; i >= 0 && v; i--) {
                        if (!(x >> i & 1)) {
                                if (id >> i & 1) {
                                        ans += (t[t[v].l].cnt);
                                        v = t[v].r;
                                }
                                else {
                                        ans += (t[t[v].r].cnt);
                                        v = t[v].l;
                                }
                        }
                        else {
                                if (id >> i & 1) {
                                        v = t[v].l;
                                }
                                else {
                                        v = t[v].r;
                                }
                        }
                }
                return ans;
        }
};

void iusearchbtw () {
        int n; cin >> n;
        vector<int> a(n + 1);
        bor pihal;
        pihal.b();
        int ans = 0;
        for (int i = 1; i <= n; i++) {
                cin >> a[i];

        }
       // for (int i = 1; i <= n; i++) {
       //         for (int j = i + 1; j <= n; j++) {
       //                 if ((a[i] ^ i) < ((i ^ a[j]) ^ j)) {
       //                         cout << i << ' ' << j << endl;
       //                 }
       //         }
       // }
        for (int i = n; i >= 1; i--) {
                ans += pihal.get(i, (a[i] ^ i));
       //         cout << pihal.get(i, (a[i] ^ i)) << ' ';
                
                pihal.add((a[i] ^ i));
        }
       // cout << endl;
       // for (int i = 1; i <= n; i++) cout << (a[i] ^ i) << ' ';
       // cout << endl;
        cout << ans << endl;
}






signed main () {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int tt = 1;	
        cin >> tt;
	while (tt --> 0) iusearchbtw();
}

Information

Submit By
Type
Submission
Problem
P1198 E. Good Signal Pairs
Contest
Brain Booster #10
Language
C++17 (G++ 13.2.0)
Submit At
2025-06-13 17:34:24
Judged At
2025-06-13 17:34:24
Judged By
Score
100
Total Time
442ms
Peak Memory
131.047 MiB