102462024-03-29 18:08:47111Egyengetőcpp17Wrong answer 0/1001.174s26428 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

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);
	}
	
	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+(int64_t)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
2Accepted3ms2232 KiB
subtask20/20
3Accepted4ms2456 KiB
4Accepted4ms2676 KiB
5Accepted7ms2988 KiB
6Wrong answer4ms3036 KiB
7Wrong answer3ms3168 KiB
8Wrong answer6ms3192 KiB
subtask30/20
9Wrong answer3ms3120 KiB
10Wrong answer12ms3600 KiB
11Wrong answer6ms3192 KiB
12Accepted17ms3680 KiB
13Accepted17ms3884 KiB
14Wrong answer6ms3676 KiB
subtask40/20
15Wrong answer3ms3804 KiB
16Wrong answer48ms6480 KiB
17Wrong answer3ms4252 KiB
18Wrong answer63ms7568 KiB
19Wrong answer6ms4716 KiB
20Accepted174ms7988 KiB
subtask50/40
21Wrong answer1.174s26428 KiB
22Wrong answer3ms4656 KiB
23Wrong answer3ms4548 KiB
24Wrong answer3ms4880 KiB
25Wrong answer34ms6616 KiB
26Wrong answer61ms7936 KiB