102492024-03-29 18:11:54111Egyengetőcpp17Time limit exceeded 60/1003.591s65252 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_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

#include "grader.h"

// #define int long long

struct Nd{
	int64_t a,d;
	int 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;
			v[i]={};
		}
		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);
	}
	
	int64_t query(int&i,int l,int r,int ii){
		if(!i||l>ii||r<ii){
			return 0;
		}
		int64_t 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;
	}
	
	int64_t 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;
	ordered_set<int>s;
	ST a(M+1),b(M+1);
	auto calc=[&](int x)->int64_t{
		return a.query(x)+b.query(M/2-(x+K));
	};
	for(int i=0;i<N;i++){
		int y=Data();
		s.insert(y);
		a.update(y,M);
		b.update(M/2-y,M);
		int64_t ans=1e18;
		{
			int l=0,h=i;
			while(l<h){
				int m=(l+h)/2;
				if(m>=i-s.order_of_key(*s.find_by_order(m)+K)){
					h=m;
				}
				else{
					l=m+1;
				}
			}
			ans=min(ans,calc(*s.find_by_order(h)));
			if(h>0)ans=min(ans,calc(*s.find_by_order(h-1)));
		}
		{
			int l=0,h=i;
			while(l<h){
				int m=(l+h)/2;
				if(s.order_of_key(*s.find_by_order(m)-K)>=i-m){
					h=m;
				}
				else{
					l=m+1;
				}
			}
			ans=min(ans,calc(*s.find_by_order(h)-K));
			if(h>0)ans=min(ans,calc(*s.find_by_order(h-1)-K));
		}
		Solution(ans);
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1900 KiB
2Accepted3ms2100 KiB
subtask220/20
3Accepted4ms2376 KiB
4Accepted4ms2328 KiB
5Accepted7ms2644 KiB
6Accepted8ms2760 KiB
7Accepted10ms2816 KiB
8Accepted1.733s13956 KiB
subtask320/20
9Accepted17ms2988 KiB
10Accepted17ms3784 KiB
11Accepted17ms3572 KiB
12Accepted17ms3640 KiB
13Accepted17ms3648 KiB
14Accepted17ms3552 KiB
subtask420/20
15Accepted92ms3808 KiB
16Accepted159ms12624 KiB
17Accepted166ms4128 KiB
18Accepted160ms12596 KiB
19Accepted162ms5372 KiB
20Accepted173ms7796 KiB
subtask50/40
21Accepted2.74s64276 KiB
22Accepted2.74s16780 KiB
23Accepted3.072s18636 KiB
24Time limit exceeded3.591s9604 KiB
25Runtime error2.611s65252 KiB
26Runtime error2.39s65240 KiB