Submission #503982

# Submission time Handle Problem Language Result Execution time Memory
503982 2022-01-09T11:34:18 Z sidon Road Construction (JOI21_road_construction) C++17
100 / 100
4915 ms 48708 KB
#include <bits/stdc++.h>
using namespace std;
 
#define C(Z) lower_bound(xVals, xVals + N, Z) - xVals
#define int int64_t
 
int N, K, F[250'001];
 
void add(int i, int v) {
	for(i++; i <= N; i += i&-i) F[i] += v;
}
 
int get(int i, int v = 0) {
	for(i++; i >= 1; i -= i&-i) v += F[i];
	return v;
}
 
int32_t main() {
	ios::sync_with_stdio(0), cin.tie(0);
 
	cin >> N >> K;
 
	int X[N], Y[N], byY[N], xVals[N], xPos[N];
 
	for(int i = 0, x, y; i < N; ++i) {
		cin >> x >> y;
		X[i] = x + y;
		Y[i] = x - y;
		byY[i] = i;
		xVals[i] = X[i];
	}
 
	sort(byY, byY + N, [&](int &i, int &j) {
		return Y[i] < Y[j];
	});
	sort(xVals, xVals + N);
 
	for(int i = 0; i < N; ++i)
		xPos[i] = C(X[i]);
 
	int dist = -1, ext;
 
	for(int b = 1LL<<33; b /= 2; ) {
		dist += b;
 
		int f = 0;
		int p = 0;
		fill(F, F + N + 1, 0);
 
		for(int i : byY) {
			while(Y[i] - Y[byY[p]] > dist)
				add(xPos[byY[p++]], -1);
 
			f += get(C(X[i] + dist + 1)-1);
			f -= get(C(X[i] - dist + 0)-1);
 
			add(xPos[i], 1);
		}
 
		if(f >= K) dist -= b;
		else ext = K - f;
	}
	++dist;
 
	set<array<int, 2>> on;
	priority_queue<int> ans;
	map<int, int> done[N];
 
	int p = 0;
 
	for(int i : byY) {
		while(Y[i] - Y[byY[p]] > dist)
			on.erase({X[byY[p]], byY[p]}), ++p;
 
		auto j = on.lower_bound({(X[i] - dist), -1});
		bool nxt = 1;
 
		while(j != end(on) && (*j)[0] <= X[i] + dist) {
			int o = (*j)[1];
			int r = max(abs(X[i] - X[o]), Y[i] - Y[o]);
			if(r == dist && !--ext) {
				--dist;
 
				while(Y[i] - Y[byY[p]] > dist)
					on.erase({X[byY[p]], byY[p]}), ++p;
 
				j = on.lower_bound({(X[i] - dist), -1});
				nxt = 0;
			}
			if(++done[o][i] < 2) ans.push(-r);
			if(nxt) ++j;
			nxt = 1;
		}
 
		on.insert({X[i], i});
	}
 
	while(!empty(ans))
		cout << -ans.top() << '\n', ans.pop();
}

Compilation message

road_construction.cpp: In function 'int32_t main()':
road_construction.cpp:81:17: warning: 'ext' may be used uninitialized in this function [-Wmaybe-uninitialized]
   81 |    if(r == dist && !--ext) {
      |       ~~~~~~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 141 ms 20588 KB Output is correct
2 Correct 144 ms 20620 KB Output is correct
3 Correct 151 ms 20664 KB Output is correct
4 Correct 142 ms 20716 KB Output is correct
5 Correct 145 ms 19504 KB Output is correct
6 Correct 6 ms 572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2156 ms 42588 KB Output is correct
2 Correct 2072 ms 42600 KB Output is correct
3 Correct 123 ms 20528 KB Output is correct
4 Correct 2029 ms 45380 KB Output is correct
5 Correct 2095 ms 45580 KB Output is correct
6 Correct 2035 ms 45636 KB Output is correct
7 Correct 2129 ms 44992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4365 ms 23676 KB Output is correct
2 Correct 4361 ms 23676 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 2213 ms 26588 KB Output is correct
5 Correct 4152 ms 29124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4365 ms 23676 KB Output is correct
2 Correct 4361 ms 23676 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 2213 ms 26588 KB Output is correct
5 Correct 4152 ms 29124 KB Output is correct
6 Correct 4388 ms 28764 KB Output is correct
7 Correct 4286 ms 28852 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 4502 ms 28764 KB Output is correct
11 Correct 1889 ms 26616 KB Output is correct
12 Correct 4260 ms 29140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 20588 KB Output is correct
2 Correct 144 ms 20620 KB Output is correct
3 Correct 151 ms 20664 KB Output is correct
4 Correct 142 ms 20716 KB Output is correct
5 Correct 145 ms 19504 KB Output is correct
6 Correct 6 ms 572 KB Output is correct
7 Correct 1480 ms 31460 KB Output is correct
8 Correct 1378 ms 31340 KB Output is correct
9 Correct 141 ms 20684 KB Output is correct
10 Correct 1474 ms 30568 KB Output is correct
11 Correct 1256 ms 30428 KB Output is correct
12 Correct 1196 ms 31312 KB Output is correct
13 Correct 1030 ms 29892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 20588 KB Output is correct
2 Correct 144 ms 20620 KB Output is correct
3 Correct 151 ms 20664 KB Output is correct
4 Correct 142 ms 20716 KB Output is correct
5 Correct 145 ms 19504 KB Output is correct
6 Correct 6 ms 572 KB Output is correct
7 Correct 2156 ms 42588 KB Output is correct
8 Correct 2072 ms 42600 KB Output is correct
9 Correct 123 ms 20528 KB Output is correct
10 Correct 2029 ms 45380 KB Output is correct
11 Correct 2095 ms 45580 KB Output is correct
12 Correct 2035 ms 45636 KB Output is correct
13 Correct 2129 ms 44992 KB Output is correct
14 Correct 4365 ms 23676 KB Output is correct
15 Correct 4361 ms 23676 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 2213 ms 26588 KB Output is correct
18 Correct 4152 ms 29124 KB Output is correct
19 Correct 4388 ms 28764 KB Output is correct
20 Correct 4286 ms 28852 KB Output is correct
21 Correct 1 ms 316 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 4502 ms 28764 KB Output is correct
24 Correct 1889 ms 26616 KB Output is correct
25 Correct 4260 ms 29140 KB Output is correct
26 Correct 1480 ms 31460 KB Output is correct
27 Correct 1378 ms 31340 KB Output is correct
28 Correct 141 ms 20684 KB Output is correct
29 Correct 1474 ms 30568 KB Output is correct
30 Correct 1256 ms 30428 KB Output is correct
31 Correct 1196 ms 31312 KB Output is correct
32 Correct 1030 ms 29892 KB Output is correct
33 Correct 4915 ms 48708 KB Output is correct
34 Correct 4879 ms 48464 KB Output is correct
35 Correct 4552 ms 47692 KB Output is correct
36 Correct 3522 ms 48572 KB Output is correct
37 Correct 3684 ms 48460 KB Output is correct
38 Correct 3985 ms 47176 KB Output is correct