QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#878580#7937. Fast XORtingfractal#RE 6ms13236kbC++171.6kb2025-02-01 16:06:542025-02-01 16:06:54

Judging History

This is the latest submission verdict.

  • [2025-02-01 16:06:54]
  • Judged
  • Verdict: RE
  • Time: 6ms
  • Memory: 13236kb
  • [2025-02-01 16:06:54]
  • 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][N * 3];
vector<int> t[N];

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';
}

詳細信息

Test #1:

score: 100
Accepted
time: 6ms
memory: 13236kb

input:

8
0 1 3 2 5 4 7 6

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 6ms
memory: 13048kb

input:

8
2 0 1 3 4 5 6 7

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Runtime Error

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:


result: