102242024-03-29 16:45:48111Egyengetőcpp17Wrong answer 0/1003.575s4680 KiB
#include <bits/stdc++.h>
using namespace std;

#include "grader.h"

#define int long long

int dbg=0;

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;
		}
		if(ii<=(l+r)/2){
			ans+=query(i->l,l,(l+r)/2,ii);
		}
		else{
			ans+=query(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));
	};
	vector<int>v;
	for(int i=0;i<N;i++){
		int y=Data();
		v.push_back(y);
		a.update(y,M);
		b.update(M/2-y,M);
		int s=accumulate(v.begin(),v.end(),0ll);
		int l=s/v.size()-10,h=s/v.size()+10;
		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
1Accepted3ms1832 KiB
2Accepted3ms2028 KiB
subtask20/20
3Wrong answer3ms2128 KiB
4Wrong answer4ms2500 KiB
5Wrong answer3ms2436 KiB
6Wrong answer3ms2544 KiB
7Wrong answer3ms2760 KiB
8Wrong answer3ms2988 KiB
subtask30/20
9Accepted17ms2932 KiB
10Wrong answer3ms3048 KiB
11Wrong answer3ms3256 KiB
12Wrong answer4ms3304 KiB
13Wrong answer4ms3552 KiB
14Wrong answer3ms3480 KiB
subtask40/20
15Wrong answer3ms3484 KiB
16Wrong answer3ms3484 KiB
17Accepted194ms3744 KiB
18Wrong answer3ms3484 KiB
19Wrong answer3ms3480 KiB
20Wrong answer4ms3520 KiB
subtask50/40
21Wrong answer3ms3488 KiB
22Wrong answer3ms3612 KiB
23Wrong answer4ms4004 KiB
24Time limit exceeded3.575s4640 KiB
25Wrong answer3ms4300 KiB
26Wrong answer3ms4680 KiB