102262024-03-29 16:52:25111Egyengetőcpp17Runtime error 0/1002.641s4532 KiB
#include <bits/stdc++.h>
using namespace std;

#include "grader.h"

// #define int long long

struct Nd{
	int a,d,l,r;
};

struct ST{
	int n;
	int root;
	vector<Nd>v;
	
	ST(int n):n(n),root(0),v(1){
	}
	
	void update(int&i,int l,int r,int ll,int rr){
		if(l>rr||r<ll){
			return;
		}
		if(!i){
			i=v.size();
			v.emplace_back();
		}
		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);
	}
	
	int64_t query(int&i,int l,int r,int ii){
		if(!i||l>ii||r<ii){
			return 0;
		}
		int64_t ans=v[i].a+(int64_t)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;
	}
	
	int64_t 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)->int64_t{
		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;
			}
		}
		int64_t ans=1e18;
		for(int i=l;i<=h;i++){
			int64_t a=calc(i);
			if(a<ans){
				ans=a;
				pi=i;
			}
		}
		Solution(ans);
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1832 KiB
2Accepted3ms2032 KiB
subtask20/20
3Accepted4ms2304 KiB
4Accepted7ms2516 KiB
5Accepted8ms2772 KiB
6Accepted10ms2716 KiB
7Accepted13ms2656 KiB
8Runtime error67ms3676 KiB
subtask30/20
9Accepted14ms2976 KiB
10Runtime error24ms3884 KiB
11Accepted25ms3732 KiB
12Accepted25ms3604 KiB
13Accepted25ms3696 KiB
14Accepted18ms3632 KiB
subtask40/20
15Accepted105ms3416 KiB
16Runtime error16ms4344 KiB
17Accepted133ms3536 KiB
18Runtime error16ms4212 KiB
19Runtime error68ms4276 KiB
20Runtime error52ms4040 KiB
subtask50/40
21Runtime error50ms4256 KiB
22Accepted1.756s3592 KiB
23Accepted2.641s3764 KiB
24Accepted2.411s3504 KiB
25Runtime error17ms4532 KiB
26Runtime error16ms4356 KiB