Submission #386455

# Submission time Handle Problem Language Result Execution time Memory
386455 2021-04-06T15:09:30 Z LucaDantas Road Construction (JOI21_road_construction) C++17
100 / 100
3936 ms 17292 KB
#include<bits/stdc++.h>
using namespace std;

using ll = long long;

#define all(a) (a).begin(), (a).end()

constexpr int maxn = 3e5+10;
constexpr ll inf = 0xffffffff;

struct BIT
{
	int bit[maxn];
	void upd(int x, int v) {
		for(x++; x < maxn; x += x&-x)
			bit[x] += v;
	}
	int query(int x) {
		int ans = 0;
		for(x++; x > 0; x -= x&-x)
			ans += bit[x];
		return ans;
	}
	int get(int l, int r) { return query(r) - query(l-1); }
	void clear() { memset(bit, 0, sizeof bit); }
} bit;

struct Pt
{
	ll x, y; int pos;
	bool operator<(Pt p) { if(x == p.x) return y < p.y; return x < p.x; }
};

vector<Pt> a;

int n;
vector<pair<ll,int>> comp; // compressed y coordenate

int first_pos(ll v) { return lower_bound(all(comp), pair<ll,int>(v, 0)) - comp.begin(); }

int last_pos(ll v) { return upper_bound(all(comp), pair<ll,int>(v, maxn)) - comp.begin() - 1; }

ll get(ll lim) {
	bit.clear();
	ll ans = 0;
	for(int i = 0, ptr = 0; i < n; i++) {
		for(; ptr < n && a[ptr].x <= a[i].x + lim; ptr++)
			bit.upd(a[ptr].pos, 1);
		bit.upd(a[i].pos, -1);
		ans += bit.get(first_pos(a[i].y - lim), last_pos(a[i].y + lim));
	}
	return ans;
}

int main() {
	int k; scanf("%d %d", &n, &k);
	// Rotate the points to map the manhattan distance to chebyshev distance
	for(int i = 0, x, y; i < n; i++)
		scanf("%d %d", &x, &y), a.push_back({x+y, x-y, 0}), comp.push_back({x-y, i});
	sort(all(comp));
	for(int i = 0; i < n; i++)
		a[comp[i].second].pos = i;
	sort(all(a));
	ll l = 0, r = inf, lim = 0;
	while(l <= r) {
		ll m = (l+r) >> 1;
		if(get(m) >= k) lim = m, r = m-1;
		else l = m+1;
	}
	multiset<pair<ll,ll>> st;
	vector<ll> ans;
	auto dist = [](pair<ll,ll> a, pair<ll,ll> b) {
		return max(abs(a.first - b.first), abs(a.second - b.second));
	};
	for(int i = 0, ptr = 0; i < n; i++) {
		for(; ptr < n && a[ptr].x < a[i].x + lim; ptr++)
			st.insert({a[ptr].y, a[ptr].x});
		st.erase(st.find({a[i].y, a[i].x}));
		auto it = st.upper_bound({a[i].y - lim, inf}); // quero as respostas estritamente menores q ans
		for(;it != st.end() && (*it).first < a[i].y + lim; ++it)
			ans.push_back(dist(*it, {a[i].y, a[i].x}));
	}
	while((int)ans.size() < k)
		ans.push_back(lim);
	sort(all(ans));
	for(ll x : ans)
		printf("%lld\n", x);
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   56 |  int k; scanf("%d %d", &n, &k);
      |         ~~~~~^~~~~~~~~~~~~~~~~
road_construction.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   59 |   scanf("%d %d", &x, &y), a.push_back({x+y, x-y, 0}), comp.push_back({x-y, i});
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 66 ms 6236 KB Output is correct
2 Correct 67 ms 6364 KB Output is correct
3 Correct 73 ms 6276 KB Output is correct
4 Correct 62 ms 6372 KB Output is correct
5 Correct 61 ms 5088 KB Output is correct
6 Correct 8 ms 1644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1671 ms 16524 KB Output is correct
2 Correct 1652 ms 16524 KB Output is correct
3 Correct 67 ms 6244 KB Output is correct
4 Correct 1643 ms 16472 KB Output is correct
5 Correct 1635 ms 16600 KB Output is correct
6 Correct 1630 ms 16524 KB Output is correct
7 Correct 1599 ms 15832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3540 ms 11480 KB Output is correct
2 Correct 3538 ms 11656 KB Output is correct
3 Correct 3 ms 1516 KB Output is correct
4 Correct 1597 ms 11480 KB Output is correct
5 Correct 2907 ms 11480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3540 ms 11480 KB Output is correct
2 Correct 3538 ms 11656 KB Output is correct
3 Correct 3 ms 1516 KB Output is correct
4 Correct 1597 ms 11480 KB Output is correct
5 Correct 2907 ms 11480 KB Output is correct
6 Correct 3577 ms 11480 KB Output is correct
7 Correct 3553 ms 11480 KB Output is correct
8 Correct 3 ms 1516 KB Output is correct
9 Correct 3 ms 1516 KB Output is correct
10 Correct 3587 ms 11480 KB Output is correct
11 Correct 1590 ms 11480 KB Output is correct
12 Correct 2906 ms 11608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 6236 KB Output is correct
2 Correct 67 ms 6364 KB Output is correct
3 Correct 73 ms 6276 KB Output is correct
4 Correct 62 ms 6372 KB Output is correct
5 Correct 61 ms 5088 KB Output is correct
6 Correct 8 ms 1644 KB Output is correct
7 Correct 1397 ms 11500 KB Output is correct
8 Correct 1391 ms 11552 KB Output is correct
9 Correct 71 ms 6372 KB Output is correct
10 Correct 1313 ms 10784 KB Output is correct
11 Correct 1237 ms 10732 KB Output is correct
12 Correct 982 ms 11552 KB Output is correct
13 Correct 901 ms 9964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 6236 KB Output is correct
2 Correct 67 ms 6364 KB Output is correct
3 Correct 73 ms 6276 KB Output is correct
4 Correct 62 ms 6372 KB Output is correct
5 Correct 61 ms 5088 KB Output is correct
6 Correct 8 ms 1644 KB Output is correct
7 Correct 1671 ms 16524 KB Output is correct
8 Correct 1652 ms 16524 KB Output is correct
9 Correct 67 ms 6244 KB Output is correct
10 Correct 1643 ms 16472 KB Output is correct
11 Correct 1635 ms 16600 KB Output is correct
12 Correct 1630 ms 16524 KB Output is correct
13 Correct 1599 ms 15832 KB Output is correct
14 Correct 3540 ms 11480 KB Output is correct
15 Correct 3538 ms 11656 KB Output is correct
16 Correct 3 ms 1516 KB Output is correct
17 Correct 1597 ms 11480 KB Output is correct
18 Correct 2907 ms 11480 KB Output is correct
19 Correct 3577 ms 11480 KB Output is correct
20 Correct 3553 ms 11480 KB Output is correct
21 Correct 3 ms 1516 KB Output is correct
22 Correct 3 ms 1516 KB Output is correct
23 Correct 3587 ms 11480 KB Output is correct
24 Correct 1590 ms 11480 KB Output is correct
25 Correct 2906 ms 11608 KB Output is correct
26 Correct 1397 ms 11500 KB Output is correct
27 Correct 1391 ms 11552 KB Output is correct
28 Correct 71 ms 6372 KB Output is correct
29 Correct 1313 ms 10784 KB Output is correct
30 Correct 1237 ms 10732 KB Output is correct
31 Correct 982 ms 11552 KB Output is correct
32 Correct 901 ms 9964 KB Output is correct
33 Correct 3936 ms 17292 KB Output is correct
34 Correct 3886 ms 17292 KB Output is correct
35 Correct 3672 ms 16632 KB Output is correct
36 Correct 2490 ms 17056 KB Output is correct
37 Correct 2580 ms 17292 KB Output is correct
38 Correct 2632 ms 15848 KB Output is correct