重排
闲话:赛时不如三岁小孩。
思路
对于三岁小孩问题,最优的方案是,把 排序,从中位数砍开,交错排列,这样会形成一个一上一下的锯齿形结构,其中每个数和前 or 后一个数计算时会对答案共享两次。当然,首尾的多的贡献要减掉。减掉什么好呢?若 为偶数,当然是最中间的两个数的差,否则就是 的中位数和他的前后驱(指排名在它旁边的)的差,取小值。
所以有
然后回到原题,想想用什么维护。
对于中位数题,对顶堆是一种不错的选择。这东西支持插入一个数,并查询排名为 的数的值。具体的,我们拿一个大根堆,一个小根堆,并让大根堆里的所有数小于小根堆里的所有数。插入时根据堆顶决定插入哪个堆,查询时调整大根堆大小(把大根堆的堆顶塞到小根堆或者把小根堆的堆顶塞到大根堆直到大根堆的大小等于 ),然后输出大根堆堆顶。
当然这东西直接去挑战平衡树肯定不行,这东西查一次任意 排名最坏情况是 ,但求中位数只有 。
以下丢个模板。
class PrioSight { // 乱取的名字
private:
priority_queue<long long> q1;
priority_queue<long long,vector<long long>,greater<long long> > q2;
long long sum1,sum2; int cnt;
void resiz(int k) {
// 调整 q1 大小
while (q1.size() < k) {
long long tp = q2.top();
q2.pop(); q1.push(tp);
}
while (q1.size() > k) {
long long tp = q1.top();
q1.pop(); q2.push(tp);
}
}
public:
void ins(long long x) {
// 插入根据堆顶情况决定插入到哪里。
if (q1.empty()) {
if (!q2.empty() && q2.top() <= x) {
q2.push(x);
} else {
q1.push(x);
}
cnt++; return;
}
if (q1.top() > x) {
q1.push(x);
} else {
q2.push(x);
}
cnt++;
}
long long get_kth(int k) {
// 查询排名
resiz(k); return q1.top();
}
};但是本题要维护两个和,加上 sum1,sum2 维护两个堆里的数的和即可。
ACCode
#include <iostream>
#include <queue>
using namespace std;
#define min(x,y) (x<y?x:y)
int readint(); long long readll(); void write(long long x);
class PrioSight {
// Chinese name "Yes top yes", impossible to find the real name.
private:
priority_queue<long long> q1;
priority_queue<long long,vector<long long>,greater<long long> > q2;
long long sum1,sum2; int cnt;
void resiz(int k) {
// let q1 have k numbers
while (q1.size() < k) {
long long tp = q2.top();
q2.pop(); sum2 -= tp;
sum1 += tp; q1.push(tp);
}
while (q1.size() > k) {
long long tp = q1.top();
q1.pop(); sum1 -= tp;
sum2 += tp; q2.push(tp);
}
}
public:
void ins(long long x) {
// insert a number
if (q1.empty()) {
if (!q2.empty() && q2.top() <= x) {
q2.push(x);
sum2 += x;
} else {
q1.push(x);
sum1 += x;
}
cnt++; return;
}
if (q1.top() > x) {
q1.push(x);
sum1 += x;
} else {
q2.push(x);
sum2 += x;
} // add it to the fit one
cnt++;
}
long long get_kth(int k) {
// the k-th smallest number.
resiz(k); return q1.top();
}
long long sum_min(int k) {
// sum of k of the smallest numbers.
resiz(k); return sum1;
}
long long sum_max(int k) {
// sum of k of the largest numbers.
resiz(cnt-k); return sum2;
}
} GXR;
const int maxn = 3e6+5;
long long b[maxn];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int m = readint(), typ = readint();
for (int i = 1; i <= m; i++)
b[i] = readll();
long long lans = 0, tans = 0;
for (int n = 1; n <= m; n++) {
long long nw = b[n] ^ (typ ? lans : 0ll);
GXR.ins(nw);
if (n == 1) {
continue;
} else if (n & 1) {
long long A = GXR.sum_min(n/2);
long long B = GXR.sum_max(n/2);
long long C = GXR.get_kth(n/2+0);
long long M = GXR.get_kth(n/2+1);
long long D = GXR.get_kth(n/2+2);
lans = 2*(B-A)-min(D-M,M-C);
tans ^= lans;
} else {
long long A = GXR.sum_min(n/2);
long long B = GXR.sum_max(n/2);
long long C = GXR.get_kth(n/2+0);
long long D = GXR.get_kth(n/2+1);
lans = 2*(B-A)-(D-C);
tans ^= lans;
}
}
write(tans);
return 0;
}
// qread
int readint() {
int x = 0;
char c = getchar_unlocked();
while (c < '0' || c > '9') c = getchar_unlocked();
while (c >= '0' && c <= '9') {
x = (x<<1)+(x<<3)+(c^'0');
c = getchar_unlocked();
}
return x;
}
long long readll() {
long long x = 0;
char c = getchar_unlocked();
while (c < '0' || c > '9') c = getchar_unlocked();
while (c >= '0' && c <= '9') {
x = (x<<1)+(x<<3)+(c^'0');
c = getchar_unlocked();
}
return x;
}
void write(long long x) {
if (x >= 10) write(x/10);
putchar((x%10)^'0');
}