102512024-03-29 18:32:41111Egyengetőcpp17Wrong answer 0/10017ms5244 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>;

struct BST {
	struct Node {
		int key;
		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 + 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 = 1 + 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 = false;
			}
			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++;
			}
			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;
			}
			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);
	}
};

#include "grader.h"

#define int long long

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 c=0;
		
		c+=-t.range_sum(0,x)+x*t.range_count(0,x);
		
		return c+t.range_sum(x+K,M)-(x+K)*t.range_count(x+K,M);
	};
	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
1Accepted3ms1760 KiB
2Accepted3ms1940 KiB
subtask20/20
3Wrong answer3ms2288 KiB
4Accepted4ms2388 KiB
5Accepted7ms2724 KiB
6Wrong answer4ms2776 KiB
7Wrong answer3ms2984 KiB
8Runtime error3ms3200 KiB
subtask30/20
9Wrong answer3ms3404 KiB
10Wrong answer7ms3652 KiB
11Wrong answer3ms3764 KiB
12Accepted16ms4080 KiB
13Accepted17ms4208 KiB
14Wrong answer3ms4092 KiB
subtask40/20
15Runtime error3ms4052 KiB
16Runtime error3ms4184 KiB
17Runtime error3ms4400 KiB
18Runtime error3ms4744 KiB
19Runtime error3ms4744 KiB
20Runtime error3ms4808 KiB
subtask50/40
21Runtime error3ms4960 KiB
22Runtime error3ms4948 KiB
23Runtime error3ms4984 KiB
24Runtime error3ms4992 KiB
25Runtime error3ms5112 KiB
26Runtime error3ms5244 KiB