102322024-03-29 17:03:36111Egyengetőcpp17Runtime error 0/10034ms66648 KiB
#include <bits/stdc++.h>
using namespace std;

#include "grader.h"

#define int long long

struct Nd{
	int a,d,l,r,cc=-1;
};

struct ST{
	int n,s;
	int root;
	Nd*v=new Nd[1000000]{};
	
	ST(int n):n(n),root(0),s(0){
	}
	
	void update(int&i,int l,int r,int ll,int rr){
		if(l>rr||r<ll){
			return;
		}
		if(!i){
			i=++s;
		}
		if(l>=ll&&r<=rr){
			v[i].a+=l-ll;
			v[i].d+=1;
			return;
		}
		update(v[i].l,l,(l+r)/2,ll,rr);
		update(v[i].r,(l+r)/2+1,r,ll,rr);
	}
	
	void update(int ll,int rr){
		update(root,0,n-1,ll,rr);
	}
	
	int query(int&i,int l,int r,int ii){
		if(!i||l>ii||r<ii){
			return 0;
		}
		int ans=v[i].a+v[i].d*(ii-l);
		if(l==r){
			return ans;
		}
		if(ii<=(l+r)/2){
			ans+=query(v[i].l,l,(l+r)/2,ii);
		}
		else{
			ans+=query(v[i].r,(l+r)/2+1,r,ii);
		}
		return ans;
	}
	
	int query(int ii){
		return query(root,0,n-1,ii);
	}
};

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N=getN(),K=getK(),M=4e8;
	ST a(M+1),b(M+1);
	auto calc=[&](int x)->int{
		return a.query(x)+b.query(M/2-(x+K));
	};
	int pi=-1;
	for(int i=0;i<N;i++){
		int y=Data();
		a.update(y,M);
		b.update(M/2-y,M);
		int l=0,h=1e8;
		if(pi!=-1){
			l=min(pi,y),h=max(pi,y);
		}
		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++){
			int a=calc(i);
			if(a<ans){
				ans=a;
				pi=i;
			}
		}
		Solution(ans);
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Runtime error28ms66648 KiB
2Runtime error28ms66420 KiB
subtask20/20
3Runtime error28ms66312 KiB
4Runtime error34ms66060 KiB
5Runtime error28ms65796 KiB
6Runtime error28ms65576 KiB
7Runtime error28ms65344 KiB
8Runtime error28ms65112 KiB
subtask30/20
9Runtime error28ms64876 KiB
10Runtime error28ms64868 KiB
11Runtime error28ms64856 KiB
12Runtime error28ms64848 KiB
13Runtime error32ms64608 KiB
14Runtime error32ms64380 KiB
subtask40/20
15Runtime error28ms64384 KiB
16Runtime error32ms64392 KiB
17Runtime error27ms64156 KiB
18Runtime error27ms64144 KiB
19Runtime error27ms63924 KiB
20Runtime error27ms63896 KiB
subtask50/40
21Runtime error27ms63928 KiB
22Runtime error32ms63932 KiB
23Runtime error32ms63724 KiB
24Runtime error27ms63732 KiB
25Runtime error27ms63732 KiB
26Runtime error27ms63740 KiB