답안 #893478

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
893478 2023-12-27T05:40:50 Z vjudge1 Road Construction (JOI21_road_construction) C++17
100 / 100
6668 ms 131576 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(a) a.begin(), a.end()
#define ff first
#define ss second
const int mod = 1e16;

struct node{
	vector<pair<int, int> > v;
};
vector<int> a;
vector<int> ALL;
int fl;
int FX, FY;
struct ST{
	int n;
	 vector<node> tree;
	ST(int sz){
		n = sz;
		tree.resize(n * 4);
		for(int i = 0;i < n * 4; i++){
			tree[i] = {};
		}
	}
	node connect(node A, node B){
		node res;
		res.v = {};
		merge(all(A.v), all(B.v), back_inserter(res.v));
		return res;
	}
	
	
	void build(int v, int vl, int vr, vector<int> &b){
		if(vl == vr){
			tree[v].v = {{b[vl], vl}};
		}else{
			int mid = (vl + vr)>>1;
			build(v<<1, vl, mid, b);
			build(v<<1|1, mid+1, vr,b);
			tree[v] = connect(tree[v<<1], tree[v<<1|1]);
		}
	}
	
	node get(int l, int r, int v, int vl, int vr){
		if(l > vr or vl > r) return {};
		if(l <= vl and r >= vr){
			return tree[v];
		}
		int mid = (vl + vr)>>1;
		return connect(get(l, r, v<<1, vl, mid), get(l, r, v<<1|1, mid+1, vr));
	}
	
	int cnt_small(int l, int r, int x, int y, int v, int vl, int vr){
		if(l > vr or vl > r) return 0LL;
		if(l <= vl and r >= vr){
			
			int it = upper_bound(all(tree[v].v), make_pair(y, mod)) - tree[v].v.begin();
			int it1 = upper_bound(all(tree[v].v), make_pair(x - 1, mod)) - tree[v].v.begin();
			if(fl){
				for(int i = it1; i < it; i++){
					int idx = tree[v].v[i].ss;
					ALL.push_back(max(abs(tree[v].v[i].ff - FY), abs(a[idx] - FX)));
				} 
			}
			return it - it1;
		}
		int mid = (vl + vr)>>1;
		return cnt_small(l, r, x, y, v<<1, vl, mid) + cnt_small(l, r, x, y, v<<1|1, mid+1, vr);
	}
	
};
int n, k;
int calc(vector<pair<int, int> > &v, int sum, ST &seg){
	int cnt = 0;
	for(int i = 1;i <= n; i++){
		int x = v[i].ff, y = v[i].ss;
		int r = i-1, l = lower_bound(all(a), x - sum) - a.begin();
		if(l > r){
			continue;
		}
		int fn = y - sum, fn1 = y + sum;
		int smaller = seg.cnt_small(l, r, fn, fn1, 1, 1, n);
		cnt = cnt + smaller;
		
	}
	return cnt;
}


signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	 cin >> n;
	cin >> k;
	vector<pair<int, int> > v(n + 1);
	v[0] = {INT_MIN, INT_MIN};
	for(int i = 1;i <= n; i++){
		cin >> v[i].ff >> v[i].ss;
		int x = v[i].ff, y = v[i].ss;
		v[i].ff = x + y;
		v[i].ss = x-y;
	}
	sort(all(v), [&](auto A, auto B){
		return A.ff < B.ff;
	});
	ST seg(n+1);
	vector<int> b(n + 1);
	a.resize(n+1);
	a[0] = INT_MIN;	
	for(int i = 1;i <= n; i++){
		b[i] = v[i].ss;
		a[i] = v[i].ff;
	}
	seg.build(1, 1, n, b);
	
	//cout << calc(v, 2, seg) << '\n';
	int l = 0, r = (int)1e10;
	while(l + 1 < r){
		int mid = (l + r)>>1;
		int CNT = calc(v, mid, seg);
		if(CNT > k) r = mid;
		else l = mid;
	}
	l = r;
	//cout << "STOP Distance  : " << l << '\n';
	fl = 1;
	for(int i = 1;i <= n; i++){
		int x = v[i].ff, y = v[i].ss;
		int rr = i-1, ll = lower_bound(all(a), x - l) - a.begin();
		if(ll > rr){
			continue;
		}
		FX = x, FY = y;
		int fn = y - l, fn1 = y + l;
		int smaller = seg.cnt_small(ll, rr, fn, fn1, 1, 1, n);
	}
	sort(all(ALL));
	while((int)ALL.size() > k) ALL.pop_back();
	for(int x : ALL) cout << x << '\n';
	
	
	return 0;
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:137:7: warning: unused variable 'smaller' [-Wunused-variable]
  137 |   int smaller = seg.cnt_small(ll, rr, fn, fn1, 1, 1, n);
      |       ^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 5272 KB Output is correct
2 Correct 45 ms 5316 KB Output is correct
3 Correct 35 ms 5328 KB Output is correct
4 Correct 34 ms 5336 KB Output is correct
5 Correct 37 ms 4288 KB Output is correct
6 Correct 9 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2457 ms 123892 KB Output is correct
2 Correct 2436 ms 125044 KB Output is correct
3 Correct 32 ms 5328 KB Output is correct
4 Correct 2208 ms 126164 KB Output is correct
5 Correct 2725 ms 126672 KB Output is correct
6 Correct 2714 ms 126680 KB Output is correct
7 Correct 2549 ms 125724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4360 ms 124196 KB Output is correct
2 Correct 4309 ms 125044 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2508 ms 125564 KB Output is correct
5 Correct 5560 ms 125396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4360 ms 124196 KB Output is correct
2 Correct 4309 ms 125044 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2508 ms 125564 KB Output is correct
5 Correct 5560 ms 125396 KB Output is correct
6 Correct 4491 ms 124468 KB Output is correct
7 Correct 4407 ms 125292 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 4356 ms 124732 KB Output is correct
11 Correct 2502 ms 125300 KB Output is correct
12 Correct 5383 ms 124984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 5272 KB Output is correct
2 Correct 45 ms 5316 KB Output is correct
3 Correct 35 ms 5328 KB Output is correct
4 Correct 34 ms 5336 KB Output is correct
5 Correct 37 ms 4288 KB Output is correct
6 Correct 9 ms 856 KB Output is correct
7 Correct 2426 ms 55100 KB Output is correct
8 Correct 2486 ms 55016 KB Output is correct
9 Correct 34 ms 5344 KB Output is correct
10 Correct 1699 ms 54324 KB Output is correct
11 Correct 1359 ms 54408 KB Output is correct
12 Correct 2009 ms 56628 KB Output is correct
13 Correct 2062 ms 56880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 5272 KB Output is correct
2 Correct 45 ms 5316 KB Output is correct
3 Correct 35 ms 5328 KB Output is correct
4 Correct 34 ms 5336 KB Output is correct
5 Correct 37 ms 4288 KB Output is correct
6 Correct 9 ms 856 KB Output is correct
7 Correct 2457 ms 123892 KB Output is correct
8 Correct 2436 ms 125044 KB Output is correct
9 Correct 32 ms 5328 KB Output is correct
10 Correct 2208 ms 126164 KB Output is correct
11 Correct 2725 ms 126672 KB Output is correct
12 Correct 2714 ms 126680 KB Output is correct
13 Correct 2549 ms 125724 KB Output is correct
14 Correct 4360 ms 124196 KB Output is correct
15 Correct 4309 ms 125044 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2508 ms 125564 KB Output is correct
18 Correct 5560 ms 125396 KB Output is correct
19 Correct 4491 ms 124468 KB Output is correct
20 Correct 4407 ms 125292 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 4356 ms 124732 KB Output is correct
24 Correct 2502 ms 125300 KB Output is correct
25 Correct 5383 ms 124984 KB Output is correct
26 Correct 2426 ms 55100 KB Output is correct
27 Correct 2486 ms 55016 KB Output is correct
28 Correct 34 ms 5344 KB Output is correct
29 Correct 1699 ms 54324 KB Output is correct
30 Correct 1359 ms 54408 KB Output is correct
31 Correct 2009 ms 56628 KB Output is correct
32 Correct 2062 ms 56880 KB Output is correct
33 Correct 6668 ms 124788 KB Output is correct
34 Correct 6520 ms 124116 KB Output is correct
35 Correct 4802 ms 124208 KB Output is correct
36 Correct 5553 ms 131020 KB Output is correct
37 Correct 5822 ms 131576 KB Output is correct
38 Correct 5967 ms 125144 KB Output is correct