102192024-03-29 16:22:42111Egyengetőcpp17Wrong answer 20/1002.63s63820 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

int dbg=0;

struct Nd{
	int a,d;
	Nd*l,*r;
};

struct ST{
	int n;
	Nd*root;
	
	ST(int n):n(n),root(nullptr){
	}
	
	void update(Nd*&i,int l,int r,int ll,int rr){
		if(l>rr||r<ll){
			return;
		}
		if(!i){
			i=new Nd{};
		}
		if(l>=ll&&r<=rr){
			i->a+=l-ll;
			i->d+=1;
			return;
		}
		update(i->l,l,(l+r)/2,ll,rr);
		update(i->r,(l+r)/2+1,r,ll,rr);
	}
	
	void update(int ll,int rr){
		update(root,0,n-1,ll,rr);
	}
	
	int query(Nd*&i,int l,int r,int ii){
		if(!i||l>ii||r<ii){
			return 0;
		}
		int ans=i->a+i->d*(ii-l);
		if(l==r){
			return ans;
		}
		if(ii<=(l+r)/2){
			ans+=query(i->l,l,(l+r)/2,ii);
		}
		else{
			ans+=query(i->r,(l+r)/2+1,r,ii);
		}
		return ans;
	}
	
	int 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=5e8;
	ST a(M+1),b(M+1);
	auto calc=[&](int x)->int{
		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);
		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;
			}
		}
		int 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
1Accepted3ms1892 KiB
2Accepted3ms2128 KiB
subtask220/20
3Accepted4ms2424 KiB
4Accepted6ms2324 KiB
5Accepted8ms2624 KiB
6Accepted10ms3004 KiB
7Accepted13ms3064 KiB
8Accepted2.375s7376 KiB
subtask30/20
9Accepted16ms3228 KiB
10Wrong answer6ms3800 KiB
11Accepted23ms4380 KiB
12Accepted26ms4604 KiB
13Wrong answer3ms3760 KiB
14Wrong answer3ms3900 KiB
subtask40/20
15Wrong answer3ms3892 KiB
16Wrong answer4ms4192 KiB
17Accepted137ms4056 KiB
18Wrong answer14ms5716 KiB
19Accepted246ms7464 KiB
20Accepted289ms12924 KiB
subtask50/40
21Wrong answer3ms4724 KiB
22Wrong answer3ms4680 KiB
23Wrong answer3ms4688 KiB
24Accepted2.63s4772 KiB
25Runtime error1.549s63820 KiB
26Wrong answer16ms6764 KiB