102302024-03-29 17:01:16111Egyengetőcpp17Wrong answer 0/1002.141s65192 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,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){
			if(y<pi){
				l=pi-(pi-y)/max(1ll,i/10);
				h=pi;
			}
			else{
				h=pi+(y-pi)/max(1ll,i/10);
				l=pi;
			}
		}
		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
1Accepted3ms1988 KiB
2Accepted3ms2096 KiB
subtask20/20
3Wrong answer3ms2344 KiB
4Accepted6ms2584 KiB
5Accepted8ms2944 KiB
6Wrong answer8ms2928 KiB
7Wrong answer3ms2720 KiB
8Wrong answer3ms2976 KiB
subtask30/20
9Accepted14ms3108 KiB
10Wrong answer3ms3088 KiB
11Wrong answer4ms3116 KiB
12Accepted18ms3564 KiB
13Accepted17ms3712 KiB
14Wrong answer9ms3528 KiB
subtask40/20
15Wrong answer3ms3392 KiB
16Wrong answer4ms3444 KiB
17Accepted119ms3384 KiB
18Wrong answer4ms3452 KiB
19Wrong answer3ms3408 KiB
20Accepted163ms8316 KiB
subtask50/40
21Runtime error1.958s65192 KiB
22Wrong answer8ms3656 KiB
23Wrong answer3ms3460 KiB
24Accepted2.141s3452 KiB
25Wrong answer4ms3628 KiB
26Wrong answer4ms3492 KiB