102382024-03-29 17:33:32111Egyengetőcpp17Runtime error 0/10041ms65480 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;
	ST a(M+1),b(M+1);
	auto calc=[&](int x)->int64_t{
		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;
			}
		}
		int64_t 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
1Accepted25ms64504 KiB
2Accepted25ms64772 KiB
subtask20/20
3Accepted26ms64804 KiB
4Accepted28ms64780 KiB
5Accepted29ms65044 KiB
6Accepted37ms65252 KiB
7Accepted41ms65480 KiB
8Runtime error35ms65472 KiB
subtask30/20
9Runtime error35ms65256 KiB
10Runtime error35ms65016 KiB
11Runtime error35ms65008 KiB
12Runtime error35ms65000 KiB
13Runtime error35ms64760 KiB
14Runtime error34ms64756 KiB
subtask40/20
15Runtime error32ms64748 KiB
16Runtime error34ms64516 KiB
17Runtime error34ms64504 KiB
18Runtime error34ms64468 KiB
19Runtime error28ms64212 KiB
20Runtime error28ms63972 KiB
subtask50/40
21Runtime error34ms63920 KiB
22Runtime error28ms63976 KiB
23Runtime error28ms63972 KiB
24Runtime error27ms63772 KiB
25Runtime error28ms63732 KiB
26Runtime error28ms63704 KiB