拍了很久都没拍出问题
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
double summ;
int tot, root, INF = 1e9 + 7;
struct Treap{
int l, r, dat, cnt, size;
double val, sum;
}a[N];
int New(double val, int k) {
a[++tot].val = val, a[tot].dat = rand(), a[tot].cnt = a[tot].size = k, a[tot].sum = val * k;
return tot;
}
void Update(int p) {
a[p].size = a[a[p].l].size + a[a[p].r].size + a[p].cnt;
a[p].sum = a[a[p].l].sum + a[a[p].r].sum + a[p].val * a[p].cnt;
}
void Build() {
New(INF, 1), New(-INF, 1);
root = 1, a[1].l = 2;
Update(root);
}
double GetValByRank(int p, int rank) {
if (p == 0) return INF;
if (a[a[p].l].size >= rank)
return GetValByRank(a[p].l, rank);
if (a[a[p].l].size + a[p].cnt >= rank){
summ += a[a[p].l].sum + a[p].val * (rank - a[a[p].l].size - 1);
return a[p].val;
}
summ += a[p].val * a[p].cnt + a[a[p].l].sum;
return GetValByRank(a[p].r, rank - a[a[p].l].size - a[p].cnt);
}
void zig(int &p) {
int q = a[p].l;
a[p].l = a[q].r, a[q].r = p, p = q;
Update(a[p].r);
Update(p);
}
void zag(int &p) {
int q = a[p].r;
a[p].r = a[q].l, a[q].l = p, p = q;
Update(a[p].l);
Update(p);
}
void Insert(int &p, double val, int k) {
if (p == 0) {
p = New(val, k);
return;
}
if (val == a[p].val) {
a[p].cnt += k, Update(p);
return;
}
if (val < a[p].val) {
Insert(a[p].l, val, k);
if (a[p].dat < a[a[p].l].dat) zig(p);
}
else {
Insert(a[p].r, val, k);
if (a[p].dat < a[a[p].r].dat) zag(p);
}
Update(p);
}
int n, aa[N], b[N];
long long tott = 2, sumt;
int main(){
srand(time(0));
freopen("std.in", "r", stdin);
freopen("my.out", "w", stdout);
Build();
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &aa[i]);
for(int i = 1; i <= n; i++)
scanf("%d", &b[i]);
for(int i = 1; i <= n; i++){
if(aa[i] == 0)
sumt += b[i];
else{
Insert(root, -1.0 * b[i] / aa[i], abs(aa[i]));
tott += abs(aa[i]);
sumt += -aa[i] / abs(aa[i]) * b[i];
}
summ = 0;
double mid = GetValByRank(root, (tott + 1) / 2);
printf("%lf\n", sumt - summ - mid * (tott / 2 + 1) + mid * ((tott + 1) / 2 - 1) - summ - 2 * INF);
}
return 0;
}