102392024-03-29 17:34:35111Egyengetőcpp17Wrong answer 40/1002.819s64328 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[900000]{};
	
	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
1Accepted28ms58180 KiB
2Accepted28ms58504 KiB
subtask220/20
3Accepted28ms58712 KiB
4Accepted30ms58928 KiB
5Accepted32ms59144 KiB
6Accepted35ms59100 KiB
7Accepted37ms59160 KiB
8Accepted2.503s60580 KiB
subtask320/20
9Accepted37ms59664 KiB
10Accepted46ms60092 KiB
11Accepted43ms60132 KiB
12Accepted46ms60260 KiB
13Accepted46ms60256 KiB
14Accepted41ms60060 KiB
subtask40/20
15Accepted129ms60056 KiB
16Accepted317ms62120 KiB
17Accepted174ms60496 KiB
18Wrong answer294ms62484 KiB
19Accepted273ms61060 KiB
20Accepted337ms62296 KiB
subtask50/40
21Runtime error810ms64328 KiB
22Accepted2.493s60600 KiB
23Accepted2.809s60856 KiB
24Accepted2.819s60800 KiB
25Wrong answer578ms62584 KiB
26Wrong answer259ms62480 KiB