[十二省联考2019] 异或粽子

Description

传送门

Solution

1538953-20190408194830377-1714021643.png

by 讲的很好还最可爱哒gxb

1538953-20190409212406364-355681517.png

Code

#include <queue>
#include <cstdio>

const int N = 500005, M = 20000000;
typedef long long LL;
struct Pair {
    LL x; int y, z;
    bool operator < (const Pair & rhs) const {
        return x < rhs.x;
    }
};
LL a[N], ans;
int ch[M][2], sum[M], sz;
std::priority_queue<Pair> q;

LL read() {
    LL x = 0; char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    return x;
}
void insert(LL y) {
    int x = 0;
    for (int i = 31; ~i; --i) {
        if ((y >> i) & 1) {
            if (!ch[x][1]) ch[x][1] = ++sz;
            x = ch[x][1], ++sum[x];
        } else {
            if (!ch[x][0]) ch[x][0] = ++sz;
            x = ch[x][0], ++sum[x];
        }
    }
}
LL query(LL y, int k) {
    int x = 0; LL z = 0;
    for (int i = 31; ~i; --i) {
        int c = (y >> i) & 1;
        if (sum[ch[x][c ^ 1]] >= k) z = (z << 1) | 1, x = ch[x][c ^ 1];
        else z = z << 1, k -= sum[ch[x][c ^ 1]], x = ch[x][c];
    }
    return z;
}
int main() {
    int n = read(), k = read() << 1;
    insert(0);
    for (int i = 1; i <= n; ++i) a[i] = a[i - 1] ^ read(), insert(a[i]);
    for (int i = 0; i <= n; ++i) q.push((Pair){query(a[i], 1), i, 1});
    while (k--) {
        Pair now = q.top();
        ans += now.x, q.pop();
        if (n < now.z + 1) continue;
        q.push((Pair){query(a[now.y], now.z + 1), now.y, now.z + 1});
    }
    printf("%lld\n", ans >> 1);
    return 0;
}

转载于:https://www.cnblogs.com/fly-in-milkyway/p/10672925.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值