Submission #521895

# Submission time Handle Problem Language Result Execution time Memory
521895 2022-02-03T11:45:49 Z amunduzbaev Road Construction (JOI21_road_construction) C++17
100 / 100
9532 ms 128808 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
typedef long long ll;

const int N = 75e4 + 5;
struct BIT{
	int tree[N];
	
	void add(int i, int v){ i++;
		for(;i<N;i+=(i&-i)) tree[i] += v;
	}
	
	int get(int i){ i++;
		int r = 0;
		for(;i>0;i-=(i&-i)) r += tree[i];
		return r;
	}
	int get(int l, int r) { return get(r) - get(l - 1); }
}bit;
 
vector<ll> rr;
int a[N][2];
ll Dis(int i, int j){ 
	return 1ll * abs(a[i][0] - a[j][0]) + abs(a[i][1] - a[j][1]); 
} 
 
struct ST{
	set<int> tree[1 << 21];
	void add(int l, int r, int i, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return;
		if(lx >= l && rx <= r){ tree[x].insert(i); return; }
		int m = (lx + rx) >> 1;
		add(l, r, i, lx, m, x<<1);
		add(l, r, i, m+1, rx, x<<1|1);
	}
	
	void del(int l, int r, int i, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return;
		if(lx >= l && rx <= r) { tree[x].erase(i); return; }
		int m = (lx + rx) >> 1;
		del(l, r, i, lx, m, x<<1);
		del(l, r, i, m+1, rx, x<<1|1);
	}
	
	void get(int I, int i, int lx = 0, int rx = N, int x = 1){
		for(auto j : tree[x]){
			if(i < j) rr.push_back(Dis(i, j));
		}
		
		if(lx == rx) return;
		int m = (lx + rx) >> 1;
		if(I <= m) get(I, i, lx, m, x<<1);
		else get(I, i, m+1, rx, x<<1|1);
	}
}tree;
 
int n, k; 
ar<ll, 3> x[N], y[N];
int ox[N], oy[N];

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	cin>>n>>k;
	for(int i=0;i<n;i++) cin>>a[i][0]>>a[i][1];
	vector<int> q(n);
	vector<int> px(n*3), py(n*3);
	for(int i=0;i<n;i++){
		q[i] = i;
		ox[i] = a[i][0] + a[i][1];
		oy[i] = a[i][0] - a[i][1];
		px[i*3] = i<<2, px[i*3+1] = i<<2|1, px[i*3+2] = i<<2|2;
		py[i*3] = i<<2, py[i*3+1] = i<<2|1, py[i*3+2] = i<<2|2;
	}
	sort(q.begin(), q.end(), [&](int i, int j) { return (ox[i] < ox[j]); });
	ll d = -1;
	
	auto check = [&](ll d, int t){ 
		for(int i=0;i<n;i++){
			x[i][0] = ox[i], y[i][0] = oy[i];
			x[i][1] = x[i][0] - d, x[i][2] = x[i][0] + d;
			y[i][1] = y[i][0] - d, y[i][2] = y[i][0] + d;
		}
		
		sort(px.begin(), px.end(), [&](int i, int j) { return (x[i>>2][i&3] < x[j>>2][j&3]); });
		sort(py.begin(), py.end(), [&](int i, int j) { return (y[i>>2][i&3] < y[j>>2][j&3]); });
		for(int i=0, last=0;i<3*n;){
			int j = i;
			while(j < 3*n && x[px[j]>>2][px[j]&3] == x[px[i]>>2][px[i]&3]){
				j++;
			} while(i<j){
				x[px[i]>>2][px[i]&3] = last, i++;
			} last++;
		}
		for(int i=0, last=0;i<3*n;){
			int j = i;
			while(j < 3*n && y[py[j]>>2][py[j]&3] == y[py[i]>>2][py[i]&3]){
				j++;
			} while(i<j){
				y[py[i]>>2][py[i]&3] = last, i++;
			} last++;
		}
		
		if(!t) return false;
		memset(bit.tree, 0, sizeof bit.tree);
		ll res = 0;
		int j=0, k=0;
		for(auto i : q){
			while(j < n && x[i][0] >= x[q[j]][1]){
				res -= bit.get(y[q[j]][1], y[q[j]][2]);
				j++;
			} while(k < n && x[i][0] > x[q[k]][2]){
				res += bit.get(y[q[k]][1], y[q[k]][2]);
				k++;
			} bit.add(y[i][0], 1);
		} 
		while(j < n) res -= bit.get(y[q[j]][1], y[q[j]][2]), j++;
		while(k < n) res += bit.get(y[q[k]][1], y[q[k]][2]), k++;
		res = (res - n) >> 1;
		return (res >= ::k);
	};
	
	{
		ll l = 1, r = 4e9;
		while(l < r){
			ll m = (l + r) >> 1;
			if(check(m, 1)) r = m;
			else l = m + 1;
		} d = l;
	}
	
	ll D = d; d--;
	check(d, 0);
	
	int j=0, l=0;
	for(auto i : q){
		while(j < n && x[i][0] >= x[q[j]][1]){
			tree.add(y[q[j]][1], y[q[j]][2], q[j]);
			j++;
		} while(l < n && x[i][0] > x[q[l]][2]){
			tree.del(y[q[l]][1], y[q[l]][2], q[l]);
			l++;
		} tree.get(y[i][0], i);
	}
	
	while((int)rr.size() < k) rr.push_back(D);
	assert((int)rr.size() == k);
	sort(rr.begin(), rr.end());
	for(auto x : rr) cout<<x<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 101 ms 106800 KB Output is correct
2 Correct 108 ms 106760 KB Output is correct
3 Correct 101 ms 106836 KB Output is correct
4 Correct 97 ms 106868 KB Output is correct
5 Correct 98 ms 105652 KB Output is correct
6 Correct 57 ms 101836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7242 ms 127608 KB Output is correct
2 Correct 7199 ms 127484 KB Output is correct
3 Correct 106 ms 106908 KB Output is correct
4 Correct 7809 ms 127728 KB Output is correct
5 Correct 6558 ms 127608 KB Output is correct
6 Correct 6625 ms 127608 KB Output is correct
7 Correct 6205 ms 126888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7546 ms 124340 KB Output is correct
2 Correct 7645 ms 124288 KB Output is correct
3 Correct 46 ms 101740 KB Output is correct
4 Correct 6616 ms 124296 KB Output is correct
5 Correct 5183 ms 124416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7546 ms 124340 KB Output is correct
2 Correct 7645 ms 124288 KB Output is correct
3 Correct 46 ms 101740 KB Output is correct
4 Correct 6616 ms 124296 KB Output is correct
5 Correct 5183 ms 124416 KB Output is correct
6 Correct 7426 ms 124296 KB Output is correct
7 Correct 7667 ms 124288 KB Output is correct
8 Correct 47 ms 101720 KB Output is correct
9 Correct 48 ms 101704 KB Output is correct
10 Correct 7688 ms 124288 KB Output is correct
11 Correct 6510 ms 124296 KB Output is correct
12 Correct 5040 ms 124420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 106800 KB Output is correct
2 Correct 108 ms 106760 KB Output is correct
3 Correct 101 ms 106836 KB Output is correct
4 Correct 97 ms 106868 KB Output is correct
5 Correct 98 ms 105652 KB Output is correct
6 Correct 57 ms 101836 KB Output is correct
7 Correct 2176 ms 115248 KB Output is correct
8 Correct 2185 ms 115260 KB Output is correct
9 Correct 95 ms 106944 KB Output is correct
10 Correct 2108 ms 114392 KB Output is correct
11 Correct 1998 ms 114352 KB Output is correct
12 Correct 1259 ms 114868 KB Output is correct
13 Correct 1495 ms 113516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 106800 KB Output is correct
2 Correct 108 ms 106760 KB Output is correct
3 Correct 101 ms 106836 KB Output is correct
4 Correct 97 ms 106868 KB Output is correct
5 Correct 98 ms 105652 KB Output is correct
6 Correct 57 ms 101836 KB Output is correct
7 Correct 7242 ms 127608 KB Output is correct
8 Correct 7199 ms 127484 KB Output is correct
9 Correct 106 ms 106908 KB Output is correct
10 Correct 7809 ms 127728 KB Output is correct
11 Correct 6558 ms 127608 KB Output is correct
12 Correct 6625 ms 127608 KB Output is correct
13 Correct 6205 ms 126888 KB Output is correct
14 Correct 7546 ms 124340 KB Output is correct
15 Correct 7645 ms 124288 KB Output is correct
16 Correct 46 ms 101740 KB Output is correct
17 Correct 6616 ms 124296 KB Output is correct
18 Correct 5183 ms 124416 KB Output is correct
19 Correct 7426 ms 124296 KB Output is correct
20 Correct 7667 ms 124288 KB Output is correct
21 Correct 47 ms 101720 KB Output is correct
22 Correct 48 ms 101704 KB Output is correct
23 Correct 7688 ms 124288 KB Output is correct
24 Correct 6510 ms 124296 KB Output is correct
25 Correct 5040 ms 124420 KB Output is correct
26 Correct 2176 ms 115248 KB Output is correct
27 Correct 2185 ms 115260 KB Output is correct
28 Correct 95 ms 106944 KB Output is correct
29 Correct 2108 ms 114392 KB Output is correct
30 Correct 1998 ms 114352 KB Output is correct
31 Correct 1259 ms 114868 KB Output is correct
32 Correct 1495 ms 113516 KB Output is correct
33 Correct 8696 ms 128764 KB Output is correct
34 Correct 9532 ms 128808 KB Output is correct
35 Correct 7866 ms 128112 KB Output is correct
36 Correct 5300 ms 128256 KB Output is correct
37 Correct 5104 ms 128500 KB Output is correct
38 Correct 5423 ms 127084 KB Output is correct