102182024-03-29 16:21:42111Egyengetőcpp17Wrong answer 0/1001.572s63704 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

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();
		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
1Accepted3ms1896 KiB
2Accepted3ms2128 KiB
subtask20/20
3Accepted4ms2548 KiB
4Accepted6ms2720 KiB
5Accepted8ms2852 KiB
6Wrong answer9ms3248 KiB
7Accepted14ms3232 KiB
8Wrong answer193ms6524 KiB
subtask30/20
9Wrong answer3ms3328 KiB
10Wrong answer6ms3956 KiB
11Accepted23ms4644 KiB
12Accepted25ms4556 KiB
13Wrong answer3ms3660 KiB
14Wrong answer3ms3784 KiB
subtask40/20
15Wrong answer3ms4008 KiB
16Wrong answer4ms4556 KiB
17Wrong answer3ms4504 KiB
18Wrong answer14ms6252 KiB
19Wrong answer193ms7620 KiB
20Accepted289ms12980 KiB
subtask50/40
21Wrong answer3ms4604 KiB
22Wrong answer3ms4876 KiB
23Wrong answer3ms4956 KiB
24Wrong answer3ms5180 KiB
25Runtime error1.572s63704 KiB
26Wrong answer17ms6892 KiB