102502024-03-29 18:15:49111Egyengetőcpp17Wrong answer 20/1002.2s64408 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 a1,d1,a2,d2;
	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 update1(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].a1+=l-ll;
			v[i].d1+=1;
			return;
		}
		update1(v[i].l,l,(l+r)/2,ll,rr);
		update1(v[i].r,(l+r)/2+1,r,ll,rr);
	}
	
	void update1(int ll,int rr){
		update1(root,0,n-1,ll,rr);
	}
	
	void update2(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].a2+=rr-r;
			v[i].d2+=1;
			return;
		}
		update2(v[i].l,l,(l+r)/2,ll,rr);
		update2(v[i].r,(l+r)/2+1,r,ll,rr);
	}
	
	void update2(int ll,int rr){
		update2(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].a1+v[i].d1*(ii-l)+v[i].a2+v[i].d2*(r-ii);
		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);
	auto calc=[&](int x)->int64_t{
		return a.query(x);
	};
	for(int i=0;i<N;i++){
		int y=Data();
		s.insert(y);
		a.update1(y+K,M);
		a.update2(0,y);
		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
1Accepted3ms1892 KiB
2Wrong answer3ms2224 KiB
subtask220/20
3Accepted4ms2228 KiB
4Accepted4ms2508 KiB
5Accepted7ms2800 KiB
6Accepted8ms2828 KiB
7Accepted9ms2900 KiB
8Accepted1.713s13740 KiB
subtask30/20
9Wrong answer3ms2952 KiB
10Wrong answer3ms3060 KiB
11Accepted17ms3920 KiB
12Wrong answer3ms3360 KiB
13Wrong answer3ms3428 KiB
14Wrong answer3ms3360 KiB
subtask40/20
15Wrong answer3ms3608 KiB
16Wrong answer4ms3812 KiB
17Wrong answer3ms3568 KiB
18Wrong answer9ms4632 KiB
19Accepted156ms5816 KiB
20Wrong answer3ms3696 KiB
subtask50/40
21Wrong answer3ms3856 KiB
22Wrong answer3ms3940 KiB
23Wrong answer3ms4100 KiB
24Wrong answer3ms4180 KiB
25Runtime error2.2s64408 KiB
26Wrong answer3ms4260 KiB