社区讨论
新人求助
P5283[十二省联考 2019] 异或粽子参与者 3已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mi7x9olb
- 此快照首次捕获于
- 2025/11/21 05:07 4 个月前
- 此快照最后确认于
- 2025/11/21 05:07 4 个月前
请问我这个为啥开了O2以后RE,不开就没事(不过不开就T了)
CPP#include <bits/stdc++.h>
#define maxn 500010
#define ll long long
using namespace std;
struct data {
int id, k;
ll val;
data(int _id = 0, int _k = 0, ll _val = 0) {
id = _id, k = _k, val = _val;
}
bool operator < (const data &rhs) const {
return val < rhs.val;
}
};
struct node {
int l, r, s;
} t[maxn * 40];
int rt[maxn], n, k, cnt = 0;
ll a[maxn], ans = 0;
priority_queue<data> pq;
template <class T> void read(T &x) {
char ch = x = 0;
bool fl = false;
while (!isdigit(ch))
fl |= ch == '-', ch = getchar();
while (isdigit(ch))
x = x * 10 + ch - '0', ch = getchar();
x = fl ? -x : x;
}
void insert(int &x, int y, ll val, int d) {
t[x = ++cnt] = t[y], t[x].s++;
if (!~d)
return;
if (~val >> d & 1)
insert(t[x].l, t[y].l, val, d - 1);
else
insert(t[x].r, t[y].r, val, d - 1);
}
ll query(int x, ll val, int k, ll tmp, int d) {
if (!~d)
return val ^ tmp;
if (t[x].s < k)
return -1;
if (~val >> d & 1) {
if (t[t[x].r].s >= k)
query(t[x].r, val, k, tmp ^ (1LL << d), d - 1);
else
query(t[x].l, val, k - t[t[x].r].s, tmp, d - 1);
} else {
if (t[t[x].l].s >= k)
query(t[x].l, val, k, tmp, d - 1);
else
query(t[x].r, val, k - t[t[x].l].s, tmp ^ (1LL << d), d - 1);
}
}
int main() {
read(n), read(k);
for (int i = 1; i <= n; i++) {
read(a[i]), a[i] ^= a[i - 1];
}
insert(rt[0], 0, 0, 32);
for (int i = 1; i <= n; i++) {
insert(rt[i], rt[i - 1], a[i], 32);
}
for (int i = 1; i <= n; i++) {
ll res = query(rt[i - 1], a[i], 1, 0, 32);
pq.push(data(i, 1, res));
}
while (k--) {
data tmp = pq.top();
ans += tmp.val, pq.pop();
ll res = query(rt[tmp.id - 1], a[tmp.id], tmp.k + 1, 0, 32);
if (~res)
pq.push(data(tmp.id, tmp.k + 1, res));
}
printf("%lld\n", ans);
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...