102212024-03-29 16:31:32111Egyengetőcpp17Runtime error 0/10029ms66656 KiB
#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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Runtime error29ms66656 KiB
2Runtime error28ms66420 KiB
subtask20/20
3Runtime error28ms66184 KiB
4Runtime error28ms66072 KiB
5Runtime error28ms65836 KiB
6Runtime error28ms65832 KiB
7Runtime error28ms65596 KiB
8Runtime error28ms65592 KiB
subtask30/20
9Runtime error28ms65348 KiB
10Runtime error28ms65120 KiB
11Runtime error28ms65108 KiB
12Runtime error28ms64876 KiB
13Runtime error28ms64864 KiB
14Runtime error28ms64860 KiB
subtask40/20
15Runtime error28ms64868 KiB
16Runtime error28ms64868 KiB
17Runtime error28ms64864 KiB
18Runtime error28ms64864 KiB
19Runtime error28ms64860 KiB
20Runtime error28ms64856 KiB
subtask50/40
21Runtime error28ms64852 KiB
22Runtime error28ms64868 KiB
23Runtime error28ms64872 KiB
24Runtime error28ms64860 KiB
25Runtime error28ms64872 KiB
26Runtime error28ms64876 KiB