Submission #1315517

#TimeUsernameProblemLanguageResultExecution timeMemory
1315517vlomaczkRoad Construction (JOI21_road_construction)C++20
5 / 100
10103 ms363292 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename TY>
struct MultiOrderedSet {
	ordered_set<pair<TY, ll>> os;
	ll cnt = 0;
	void insert(TY x) {
		os.insert({x, cnt++});
	}
	void erase(TY x) {
		ll idx = os.order_of_key({x,-1});
		assert(idx < os.size());
		os.erase(*os.find_by_order(idx));
	}
	TY first() { return os.begin()->first; }
	TY last() { return os.rbegin()->first; }
	void clear() {
		while(os.size()) os.erase(*os.begin());
	}
	ll size() { return os.size(); }
	bool empty() { return os.empty(); }
	ll order_of_key(TY x) {
		return os.order_of_key({x, -1});
	}
	TY find_by_order(ll x) {
		return os.find_by_order(x)->first;
	}
	ll amount(TY a, TY b) {
		return order_of_key(b+1) - order_of_key(a);
	}
};

struct Point {
	ll x, y;
};

ll M = 250'010;
vector<ll> cnt(M,1);
ll n;
ll inf = 1e18;

vector<Point> pkt;
ll base = 1<<18;
vector<MultiOrderedSet<ll>> T(2*base); 
vector<ll> X;

void build() {
	for(ll i=0; i<n; ++i) X.push_back(pkt[i].x);
	sort(X.begin(), X.end());
	for(ll i=0; i<n; ++i) {
		ll idx = lower_bound(X.begin(), X.end(), pkt[i].x) - X.begin();
		ll x = idx+base;
		while(x>0) {
			T[x].insert(pkt[i].y);
			x/=2;
		}
	}
}

ll kwadrat(ll x, ll y, ll r) {
	ll a = lower_bound(X.begin(), X.end(), x-r) - X.begin();
	ll b = upper_bound(X.begin(), X.end(), x+r) - X.begin() - 1;
	a += base-1;
	b += base+1;
	ll res = 0;
	while(a/2 != b/2) {
		if(a%2==0) res += T[a+1].amount(y-r, y+r);
		if(b%2==1) res += T[b-1].amount(y-r, y+r);
		a/=2; b/=2;
	}
	return res;
}

ll find_nxt(ll i) {
	ll lo = 0;
	ll hi = 4e9;
	if(cnt[i]==n) return inf;
	while(lo < hi) {
		ll mid = (lo+hi)/2;
		if(kwadrat(pkt[i].x, pkt[i].y, mid) > cnt[i]) hi = mid;
		else lo = mid+1;
	}
	cnt[i]++;
	return lo;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	ll k;
	cin >> n >> k;
	pkt.resize(n);
	for(ll i=0; i<n; ++i) {
		ll x,y;
		cin >> x >> y;
		pkt[i] = {x+y,x-y};
	}
	build();
	priority_queue<pair<ll, ll>> pq;
	for(ll i=0; i<n; ++i) {
		pq.push({-find_nxt(i), i});
	}
	for(ll ile=0; ile<2*k; ++ile){
		if(ile%2==0) cout << -pq.top().first << "\n";
		ll i =pq.top().second;
		pq.pop();
		pq.push({-find_nxt(i), i});
	}

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...