102132024-03-29 15:55:10111Egyengetőcpp17Time limit exceeded 40/1003.589s64300 KiB
#include <bits/stdc++.h>
using namespace std;

#include "grader.h"

#define int long long

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

struct ST{
	int n;
	Nd*root;
	
	ST(int n):n(n),root(nullptr){
	}
	
	void update(Nd*&i,int l,int r,int ll,int rr){
		if(l>rr||r<ll){
			return;
		}
		if(!i){
			i=new Nd{};
		}
		if(l>=ll&&r<=rr){
			i->a+=l-ll;
			i->d+=1;
			return;
		}
		update(i->l,l,(l+r)/2,ll,rr);
		update(i->r,(l+r)/2+1,r,ll,rr);
	}
	
	void update(int ll,int rr){
		update(root,0,n-1,ll,rr);
	}
	
	int query(Nd*&i,int l,int r,int ii){
		if(!i||l>ii||r<ii){
			return 0;
		}
		int ans=i->a+i->d*(ii-l);
		if(l==r){
			return ans;
		}
		return ans+query(i->l,l,(l+r)/2,ii)+query(i->r,(l+r)/2+1,r,ii);
	}
	
	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=1e10;
	ST 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,M);
		b.update(M/2-y,M);
		int l=-1e9,h=1e9;
		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
1Accepted3ms1892 KiB
2Accepted3ms2024 KiB
subtask20/20
3Accepted6ms2448 KiB
4Accepted8ms2560 KiB
5Accepted13ms2876 KiB
6Accepted17ms3020 KiB
7Accepted21ms2752 KiB
8Time limit exceeded3.509s3684 KiB
subtask320/20
9Accepted34ms3108 KiB
10Accepted41ms5000 KiB
11Accepted39ms4404 KiB
12Accepted39ms4440 KiB
13Accepted39ms4532 KiB
14Accepted37ms4408 KiB
subtask420/20
15Accepted199ms3940 KiB
16Accepted411ms20792 KiB
17Accepted314ms4036 KiB
18Accepted412ms21028 KiB
19Accepted377ms6472 KiB
20Accepted388ms11664 KiB
subtask50/40
21Runtime error3.401s64300 KiB
22Time limit exceeded3.556s3672 KiB
23Time limit exceeded3.555s3804 KiB
24Time limit exceeded3.589s3604 KiB
25Runtime error2.506s64284 KiB
26Runtime error2.515s64272 KiB