102672024-03-29 20:57:32111Egyengetőcpp17Elfogadva 100/1003.375s30060 KiB
#include <bits/stdc++.h>
using namespace std;

#include "grader.h"

#define int long long

struct BST 
{
	struct Node
	{
		int key;
		int count;
		int sum;
		int min;
		int max;
		int size;
		int height;
		int left;
		int right;
		
		Node()
		{
			key = 0;
			count = 0;
			sum = 0;
			size = 0;
			height = 0;
			left = 0;
			right = 0;
		}
		
		Node(int k)
		{
			key = k;
			count = 1;
			sum = k;
			size = 1;
			height = 1;
			left = 0;
			right = 0;
		}
	};
	
	Node* nodes;
	int root;
	int size;
	
	BST(int n)
	{
		nodes = (Node*)malloc((1 + n) * sizeof(Node));
		nodes[0] = Node();
		root = 0;
		size = 0;
	}
	
	void update(int i)
	{
		if (i == 0)
		{
			return;
		}
		nodes[i].sum = nodes[i].key * nodes[i].count + nodes[nodes[i].left].sum + nodes[nodes[i].right].sum;
		nodes[i].size = nodes[i].count + nodes[nodes[i].left].size + nodes[nodes[i].right].size;
		nodes[i].height = 1 + max(nodes[nodes[i].left].height, nodes[nodes[i].right].height);
	}
	
	void rotate_left(int& i)
	{
		int t = nodes[i].right;
		nodes[i].right = nodes[t].left;
		nodes[t].left = i;
		i = t;
	}
	
	void rotate_right(int& i)
	{
		int t = nodes[i].left;
		nodes[i].left = nodes[t].right;
		nodes[t].right = i;
		i = t;
	}
	
	void insert(int& i, int k)
	{
		if (i == 0)
		{
			i = ++size;
			nodes[i] = Node(k);
			return;
		}
		if (k == nodes[i].key)
		{
			nodes[i].count++;
		}
		else
		{
			if (k < nodes[i].key)
			{
				insert(nodes[i].left, k);
			}
			if (k > nodes[i].key)
			{
				insert(nodes[i].right, k);
			}
			int hl = nodes[nodes[i].left].height;
			int hr = nodes[nodes[i].right].height;
			if (hr - hl > 1)
			{
				rotate_left(i);
				update(nodes[i].left);
			}
			if (hl - hr > 1)
			{
				rotate_right(i);
				update(nodes[i].right);
			}
		}
		update(i);
	}
	
	void insert(int k)
	{
		insert(root, k);
	}
	
	int rank_of_key(int i, int k)
	{
		int result = 0;
		while (i != 0) 
		{
			if (nodes[i].key <= k) 
			{
				result += nodes[i].count + nodes[nodes[i].left].size;
				i = nodes[i].right;
			}
			else 
			{
				i = nodes[i].left;
			}
		}
		return result;
	}
	
	int rank_of_key(int k)
	{
		return rank_of_key(root, k);
	}
	
	int find_by_rank(int i, int r)
	{
		while (i != 0)
		{
			if (r <= nodes[nodes[i].left].size)
			{
				i = nodes[i].left;
				continue;
			}
			r -= nodes[nodes[i].left].size + nodes[i].count;
			if (r > 0)
			{
				i = nodes[i].right;
				continue;
			}
			break;
		}
		return i;
	}
	
	int find_by_rank(int r)
	{
		return nodes[find_by_rank(root, r)].key;
	}
	
	int prefix_sum(int i, int k)
	{
		int result = 0;
		while (i != 0) 
		{
			if (nodes[i].key <= k) 
			{
				result += nodes[i].sum - nodes[nodes[i].right].sum;
				i = nodes[i].right;
			}
			else 
			{
				i = nodes[i].left;
			}
		}
		return result;
	}
	
	int prefix_sum(int k)
	{
		return prefix_sum(root, k);
	}
};

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N=getN(),K=getK(),M=4e8,S=0,C=0;
	BST t(N);
	auto calc=[&](int x)->int{
		int s1=t.prefix_sum(x);
		int c1=t.rank_of_key(x);
		int s2=S-t.prefix_sum(x+K);
		int c2=C-t.rank_of_key(x+K);
		return x*c1-s1+s2-(x+K)*c2;
	};
	for(int i=0;i<N;i++){
		int y=Data();
		t.insert(y);
		S+=y;
		C++;
		int ans=1e18;
		{
			int l=0,h=i;
			while(l<h){
				int m=(l+h)/2;
				if(m>i-t.rank_of_key(t.find_by_rank(m)+K)){
					h=m;
				}
				else{
					l=m+1;
				}
			}
			ans=min(ans,calc(t.find_by_rank(h)));
			ans=min(ans,calc(t.find_by_rank(h+1)));
		}
		{
			int l=0,h=i;
			while(l<h){
				int m=(l+h)/2;
				if(t.rank_of_key(t.find_by_rank(m)-K)>i-m){
					h=m;
				}
				else{
					l=m+1;
				}
			}
			ans=min(ans,calc(t.find_by_rank(h)-K));
			ans=min(ans,calc(t.find_by_rank(h+1)-K));
		}
		Solution(ans);
	}
	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva3ms1896 KiB
2Elfogadva3ms2124 KiB
subtask220/20
3Elfogadva4ms2340 KiB
4Elfogadva4ms2428 KiB
5Elfogadva7ms2440 KiB
6Elfogadva8ms2568 KiB
7Elfogadva9ms2776 KiB
8Elfogadva1.368s4444 KiB
subtask320/20
9Elfogadva14ms3196 KiB
10Elfogadva14ms3308 KiB
11Elfogadva14ms3568 KiB
12Elfogadva17ms3640 KiB
13Elfogadva16ms3860 KiB
14Elfogadva14ms4032 KiB
subtask420/20
15Elfogadva68ms4232 KiB
16Elfogadva133ms5512 KiB
17Elfogadva120ms4312 KiB
18Elfogadva130ms5540 KiB
19Elfogadva138ms5128 KiB
20Elfogadva163ms5596 KiB
subtask540/40
21Elfogadva2.75s22488 KiB
22Elfogadva1.623s4300 KiB
23Elfogadva1.85s4276 KiB
24Elfogadva2.111s4544 KiB
25Elfogadva3.375s30060 KiB
26Elfogadva3.355s29932 KiB