102362024-03-29 17:30:29111Egyengetőcpp17Runtime error 60/1002.775s64240 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#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));
	};
	ordered_set<int>v;
	for(int i=0;i<N;i++){
		int y=Data();
		v.insert(y);
		v.insert(y-K);
		a.update(y,M);
		b.update(M/2-y,M);
		int l=0,h=v.size()-1;
		while(h-l>=3){
			int m1=l+(h-l)/3;
			int m2=h-(h-l)/3;
			if(calc(*v.find_by_order(m1))<=calc(*v.find_by_order(m2))){
				h=m2;
			}
			else{
				l=m1;
			}
		}
		int ans=1e18;
		for(int i=l;i<=h;i++){
			ans=min(ans,calc(*v.find_by_order(i)));
		}
		Solution(ans);
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1900 KiB
2Accepted3ms2100 KiB
subtask220/20
3Accepted4ms2524 KiB
4Accepted6ms2728 KiB
5Accepted8ms2980 KiB
6Accepted9ms3356 KiB
7Accepted13ms3060 KiB
8Accepted2.467s7692 KiB
subtask320/20
9Accepted17ms3420 KiB
10Accepted26ms5308 KiB
11Accepted24ms4592 KiB
12Accepted28ms4852 KiB
13Accepted26ms4884 KiB
14Accepted21ms4208 KiB
subtask420/20
15Accepted109ms4072 KiB
16Accepted303ms23692 KiB
17Accepted146ms4244 KiB
18Accepted291ms23544 KiB
19Accepted236ms7520 KiB
20Accepted321ms14224 KiB
subtask50/40
21Runtime error2.319s64240 KiB
22Accepted2.545s4728 KiB
23Accepted2.611s4752 KiB
24Accepted2.775s4556 KiB
25Runtime error1.524s64220 KiB
26Runtime error1.496s64208 KiB