102572024-03-29 18:40:47111Egyengetőcpp17Time limit exceeded 60/1003.585s40124 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

struct BST {
	struct Node {
		int key;
		int cnt = 1;
		int sum = key;
		int min_key = key;
		int max_key = key;
		int size = 1;
		int height = 1;
		Node* left = nullptr;
		Node* right = nullptr;

		static int get_sum(Node* node) {
			if (node == nullptr) {
				return 0;
			}
			return node->sum;
		}

		static int get_min_key(Node* node) {
			if (node == nullptr) {
				return INT_MAX;
			}
			return node->min_key;
		}

		static int get_max_key(Node* node) {
			if (node == nullptr) {
				return INT_MIN;
			}
			return node->max_key;
		}

		static int get_size(Node* node) {
			if (node == nullptr) {
				return 0;
			}
			return node->size;
		}

		static int get_height(Node* node) {
			if (node == nullptr) {
				return 0;
			}
			return node->height;
		}

		static void update(Node* node) {
			node->sum = node->key * node->cnt + get_sum(node->left) + get_sum(node->right);
			node->min_key = min(node->key, min(get_min_key(node->left), get_min_key(node->right)));
			node->max_key = max(node->key, max(get_max_key(node->left), get_max_key(node->right)));
			node->size = node->cnt + get_size(node->left) + get_size(node->right);
			node->height = 1 + max(get_height(node->left), get_height(node->right));
		}

		static void rotate_left(Node*& node) {
			Node* temp = node->right;
			node->right = temp->left;
			temp->left = node;
			node = temp;
		}

		static void rotate_right(Node*& node) {
			Node* temp = node->left;
			node->left = temp->right;
			temp->right = node;
			node = temp;
		}

		static bool insert(Node*& node, int key) {
			if (node == nullptr) {
				node = new Node{key};
				return true;
			}
			bool inserted;
			if (key < node->key) {
				inserted = insert(node->left, key);
			}
			else if (key > node->key) {
				inserted = insert(node->right, key);
			}
			else {
				inserted = true;
				node->cnt++;
			}
			if (inserted) {
				int hl = get_height(node->left);
				int hr = get_height(node->right);
				if (hr - hl > 1) {
					rotate_left(node);
					update(node->left);
				}
				else if (hl - hr > 1) {
					rotate_right(node);
					update(node->right);
				}
				update(node);
			}
			return inserted;
		}
		
		static int range_count(Node* node, int l, int h) {
			if (node == nullptr) {
				return 0;
			}
			if (node->max_key < l || node->min_key > h) {
				return 0;
			}
			if (node->min_key >= l && node->max_key <= h) {
				return node->size;
			}
			int result = range_count(node->left, l, h) + range_count(node->right, l, h);
			if (node->key >= l && node->key <= h) {
				result += node->cnt;
			}
			return result;
		}

		static int range_sum(Node* node, int l, int h) {
			if (node == nullptr) {
				return 0;
			}
			if (node->max_key < l || node->min_key > h) {
				return 0;
			}
			if (node->min_key >= l && node->max_key <= h) {
				return node->sum;
			}
			int result = range_sum(node->left, l, h) + range_sum(node->right, l, h);
			if (node->key >= l && node->key <= h) {
				result += node->key * node->cnt;
			}
			return result;
		}
	};

	Node* root = nullptr;

	bool insert(int key) {
		return Node::insert(root, key);
	}
	
	int range_count(int min_key, int max_key) {
		return Node::range_count(root, min_key, max_key);
	}

	int range_sum(int min_key, int max_key) {
		return Node::range_sum(root, min_key, max_key);
	}
};

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N=getN(),K=getK(),M=4e8;
	// if(N>1000)return 1;
	ordered_set<int>s;
	BST t;
	auto calc=[&](int x)->int{
		
		// int cc=0;
		// for(int i:s){
			// if(i<x){
				// cc+=x-i;
			// }
			// if(i>x+K){
				// cc+=i-(x+K);
			// }
		// }
		
		int c=0;
		
		c+=-t.range_sum(0,x)+x*t.range_count(0,x);
		
		c+=t.range_sum(x+K,M)-(x+K)*t.range_count(x+K,M);
		
		// if(c!=cc)exit(1);
		
		return c;
	};
	for(int i=0;i<N;i++){
		int y=Data();
		s.insert(y);
		t.insert(y);
		int ans=1e18;
		{
			int l=0,h=i;
			while(l<h){
				int m=(l+h)/2;
				if(m>=i-s.order_of_key(*s.find_by_order(m)+K)){
					h=m;
				}
				else{
					l=m+1;
				}
			}
			ans=min(ans,calc(*s.find_by_order(h)));
			if(h>0)ans=min(ans,calc(*s.find_by_order(h-1)));
		}
		{
			int l=0,h=i;
			while(l<h){
				int m=(l+h)/2;
				if(s.order_of_key(*s.find_by_order(m)-K)>=i-m){
					h=m;
				}
				else{
					l=m+1;
				}
			}
			ans=min(ans,calc(*s.find_by_order(h)-K));
			if(h>0)ans=min(ans,calc(*s.find_by_order(h-1)-K));
		}
		Solution(ans);
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1956 KiB
2Accepted3ms2184 KiB
subtask220/20
3Accepted4ms2272 KiB
4Accepted4ms2548 KiB
5Accepted7ms2616 KiB
6Accepted8ms2952 KiB
7Accepted10ms3192 KiB
8Accepted1.774s17068 KiB
subtask320/20
9Accepted16ms3148 KiB
10Accepted17ms3480 KiB
11Accepted17ms3476 KiB
12Accepted17ms3564 KiB
13Accepted16ms3560 KiB
14Accepted17ms3376 KiB
subtask420/20
15Accepted87ms4060 KiB
16Accepted159ms6104 KiB
17Accepted167ms4556 KiB
18Accepted159ms6232 KiB
19Accepted163ms5820 KiB
20Accepted178ms6436 KiB
subtask50/40
21Accepted3.019s40124 KiB
22Accepted2.671s21056 KiB
23Accepted3.046s23720 KiB
24Time limit exceeded3.585s12372 KiB
25Time limit exceeded3.536s24860 KiB
26Time limit exceeded3.519s26728 KiB