WA 20 求助
查看原帖
WA 20 求助
360265
Galois_Field_1048576楼主2023/5/1 20:57
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 64 * (500000 + 5) + 5;
struct Trie {
    int a[N][2], vis[N];
    int tot;
    void clear() {
        memset(a, -1, sizeof a); tot = 0;
    }
    void insert(int x) {
        int id = 0;
        for (int i = 32; i >= 0; -- i) {
            vis[id] ++;
            if (a[id][(x >> i) & 1] == -1) {
                a[id][(x >> i) & 1] = ++ tot;
            }
            id = a[id][(x >> i) & 1];
        }
        vis[id] ++;
    }
    int max(int y) {
        int id = 0; int z = 0;
        for (int i = 32; i >= 0; -- i) {
            int bit = (y >> i) & 1, choose;
            if (a[id][bit ^ 1] != -1) id = a[id][bit ^ 1], choose = bit ^ 1;
            else id = a[id][bit], choose = bit;
            z |= (choose << i);
        }
        return z;
    }
    int rank(int k, int y) {
        int id = 0; int z = 0;
        for (int i = 32; i >= 0; -- i) {
            int bit = (y >> i) & 1, choose;
            if (a[id][0] == -1) id = a[id][1], choose = 1;
            else if (a[id][1] == -1) id = a[id][0], choose = 0;
            else {
                if (k <= vis[a[id][bit ^ 1]])
                    id = a[id][bit ^ 1], choose = bit ^ 1;
                else
                    id = a[id][bit], k -= vis[a[id][bit ^ 1]], choose = bit;
            }
            z |= (choose << i);
        }
        return z;
    }
};
Trie t;

priority_queue<pair<pair<int, int>, int>> pq;

int a[500005], pre[500005];

signed main() {

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k, ans = 0;
    cin >> n >> k;
    t.clear();
    t.insert(0);
    for (int i = 1; i <= n; ++ i)
        cin >> a[i], a[i] ^= a[i - 1], t.insert(a[i]);
    for (int i = 0; i <= n; ++ i)
        pq.push({{t.max(a[i]) ^ a[i], i}, 1});
    for (int i = 1; i <= k * 2; ++ i) {
        int x = pq.top().first.second, y = pq.top().first.first, z = pq.top().second;
        cout << x << " " <<y << " " << z << endl;
        pq.pop();
        if (z != n + 1) pq.push({{t.rank(z + 1, a[x]) ^ a[x], x}, z + 1});
        ans += y;
    }
    cout << ans / 2 << endl;
}

2023/5/1 20:57
加载中...