102312024-03-29 17:02:18111Egyengetőcpp17Wrong answer 0/1002.48s64716 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,(int)sqrt(i));
				h=pi;
			}
			else{
				h=pi+(y-pi)/max(1ll,(int)sqrt(i));
				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
1Accepted3ms2028 KiB
2Wrong answer3ms2236 KiB
subtask20/20
3Wrong answer3ms2580 KiB
4Accepted6ms2812 KiB
5Accepted8ms2948 KiB
6Wrong answer9ms3056 KiB
7Wrong answer3ms3084 KiB
8Wrong answer3ms3296 KiB
subtask30/20
9Accepted14ms3252 KiB
10Wrong answer3ms3512 KiB
11Wrong answer3ms3472 KiB
12Accepted18ms4092 KiB
13Accepted18ms4176 KiB
14Wrong answer9ms3908 KiB
subtask40/20
15Wrong answer3ms4060 KiB
16Wrong answer3ms4168 KiB
17Accepted120ms4112 KiB
18Wrong answer3ms4020 KiB
19Wrong answer3ms4036 KiB
20Accepted182ms8836 KiB
subtask50/40
21Runtime error2.48s64716 KiB
22Wrong answer9ms4264 KiB
23Wrong answer3ms4300 KiB
24Accepted2.163s4280 KiB
25Wrong answer3ms4156 KiB
26Wrong answer3ms4408 KiB