QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#878581#7937. Fast XORtingfractal#WA 193ms58464kbC++171.6kb2025-02-01 16:07:412025-02-01 16:07:42

Judging History

This is the latest submission verdict.

  • [2025-02-01 16:07:42]
  • Judged
  • Verdict: WA
  • Time: 193ms
  • Memory: 58464kb
  • [2025-02-01 16:07:41]
  • Submitted

answer

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

#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int N = (1<<18);

int n, a[N];
int sz = 1;
int go[2][N * 3];
long long ans[2][20];
vector<int> t[N * 3];

void add(int x, int pos) {
    int v = 1;
    for (int i = 18; i >= 0; --i) {
        int b = (x >> i) & 1;
        if (go[b][v] == 0) go[b][v] = ++sz;
        v = go[b][v];
        t[v].push_back(pos);
    }
}

long long calc(vector<int> &v, vector<int> &u) {
    long long ret = 0;
    for (int i = 0, j = 0; i < v.size(); ++i) {
        while (j < u.size() && u[j] < v[i]) {
            j++;
        }
        ret += j;
    }
    return ret;
    if (v.size() && u.size()) {
        for (auto x : v) cout << x << " ";
        cout << '\n';
        for (auto x : u) cout << x << " ";
        cout << '\n';
        cout << ret << '\n';
        cout << "________\n";
    }
}

void dfs(int v = 1, int bit = 18) {
    if (bit == -1) return;
    long long x = calc(t[go[0][v]], t[go[1][v]]);
    ans[0][bit] += x;
    ans[1][bit] += sz(t[go[0][v]]) * sz(t[go[1][v]]) - x;
    dfs(go[0][v], bit-1);
    dfs(go[1][v], bit-1);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        add(a[i], i);
    }
    dfs();
    long long Ans[2] = {};
    for (int i = 0; i <= 18; ++i)  {
        Ans[0] += ans[0][i];
        Ans[1] += min(ans[0][i], ans[1][i]);
    }
    cout << min(Ans[0], Ans[1] + 1) << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 23852kb

input:

8
0 1 3 2 5 4 7 6

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 4ms
memory: 24960kb

input:

8
2 0 1 3 4 5 6 7

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Wrong Answer
time: 193ms
memory: 58464kb

input:

262144
47482 131703 90418 122675 166494 247529 196154 16950 66501 50357 246808 25929 10418 50538 26955 151884 63776 58023 20073 26544 74785 44064 41836 148543 87920 54172 3270 131495 130960 112122 167229 215767 77499 195004 21391 11039 168999 256346 109690 180904 172679 157200 78594 201857 52784 147...

output:

-8560636889

result:

wrong answer 1st numbers differ - expected: '17137565829', found: '-8560636889'