#include <bits/stdc++.h>
using namespace std;
#include "grader.h"
#define int long long
template<typename T, unsigned M = UINT_MAX, unsigned S = 4096>
struct FastMap {
T** a = new T*[M / S]{};
inline T& operator[](unsigned i) {
if (a[i / S] == nullptr) {
a[i / S] = new T[S]{};
}
return a[i / S][i % S];
}
};
struct F{
int n;
FastMap<pair<int,int>>t;
F(int n):n(n){
}
void update(int i){
for(int j=i;j<n;j+=j&-j){
int s=j-(j&-j)+1;
int e=j;
t[j].first+=s-i;
t[j].second++;
}
}
int query(int i){
int x=0;
for(int j=i;j;j-=j&-j){
int s=j-(j&-j)+1;
int e=j;
x+=t[j].first+t[j].second*(i-s);
}
return x;
}
};
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N=getN(),K=getK(),M=5e8;
F a(M+1),b(M+1);
auto calc=[&](int x)->int{
return a.query(x)+b.query(M/2-(x+K));
};
for(int i=0;i<N;i++){
int y=Data();
a.update(y);
b.update(M/2-y);
int l=-1e8,h=2e8;
while(h-l>=3){
int m1=l+(h-l)/3;
int m2=h-(h-l)/3;
if(calc(m1)<=calc(m2)){
h=m2;
}
else{
l=m1;
}
}
int ans=1e18;
for(int i=l;i<=h;i++){
ans=min(ans,calc(i));
}
Solution(ans);
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Runtime error | 29ms | 66656 KiB | ||||
2 | Runtime error | 28ms | 66420 KiB | ||||
subtask2 | 0/20 | ||||||
3 | Runtime error | 28ms | 66184 KiB | ||||
4 | Runtime error | 28ms | 66072 KiB | ||||
5 | Runtime error | 28ms | 65836 KiB | ||||
6 | Runtime error | 28ms | 65832 KiB | ||||
7 | Runtime error | 28ms | 65596 KiB | ||||
8 | Runtime error | 28ms | 65592 KiB | ||||
subtask3 | 0/20 | ||||||
9 | Runtime error | 28ms | 65348 KiB | ||||
10 | Runtime error | 28ms | 65120 KiB | ||||
11 | Runtime error | 28ms | 65108 KiB | ||||
12 | Runtime error | 28ms | 64876 KiB | ||||
13 | Runtime error | 28ms | 64864 KiB | ||||
14 | Runtime error | 28ms | 64860 KiB | ||||
subtask4 | 0/20 | ||||||
15 | Runtime error | 28ms | 64868 KiB | ||||
16 | Runtime error | 28ms | 64868 KiB | ||||
17 | Runtime error | 28ms | 64864 KiB | ||||
18 | Runtime error | 28ms | 64864 KiB | ||||
19 | Runtime error | 28ms | 64860 KiB | ||||
20 | Runtime error | 28ms | 64856 KiB | ||||
subtask5 | 0/40 | ||||||
21 | Runtime error | 28ms | 64852 KiB | ||||
22 | Runtime error | 28ms | 64868 KiB | ||||
23 | Runtime error | 28ms | 64872 KiB | ||||
24 | Runtime error | 28ms | 64860 KiB | ||||
25 | Runtime error | 28ms | 64872 KiB | ||||
26 | Runtime error | 28ms | 64876 KiB |