102482024-03-29 18:10:44111Egyengetőcpp17Wrong answer 60/1003.588s26392 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{
	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;
			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+(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
1Accepted3ms2032 KiB
2Accepted3ms2236 KiB
subtask220/20
3Accepted4ms2348 KiB
4Accepted6ms2576 KiB
5Accepted7ms2944 KiB
6Accepted8ms2916 KiB
7Accepted10ms2952 KiB
8Accepted1.707s13388 KiB
subtask320/20
9Accepted17ms3336 KiB
10Accepted17ms4088 KiB
11Accepted17ms4016 KiB
12Accepted17ms4220 KiB
13Accepted17ms4572 KiB
14Accepted17ms4320 KiB
subtask420/20
15Accepted90ms4648 KiB
16Accepted158ms10868 KiB
17Accepted170ms5284 KiB
18Accepted158ms11132 KiB
19Accepted158ms6304 KiB
20Accepted174ms7860 KiB
subtask50/40
21Wrong answer1.246s26392 KiB
22Accepted2.578s17844 KiB
23Accepted3.141s19592 KiB
24Time limit exceeded3.588s10900 KiB
25Wrong answer504ms19704 KiB
26Wrong answer462ms19540 KiB