답안 #619113

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
619113 2022-08-02T09:57:07 Z tqbfjotld Road Construction (JOI21_road_construction) C++14
100 / 100
5473 ms 33164 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

vector<int> yvs;
vector<int> xvs;

int fenw[250005];

void upd(int pos, int v){
	pos++;
	while (pos<250005){
		fenw[pos]+=v;
		pos += pos&-pos;
	}
}

int qu(int pos){
	pos++;
	int ans = 0;
	while (pos>0){
		ans += fenw[pos];
		pos -= pos&-pos;
	}
	return ans;
}

int origX[250005];
int origY[250005];
int nX[250005];
int nY[250005];
vector<pair<pair<int,int>,int>> pts;

int lb(int v){
	return lower_bound(yvs.begin(),yvs.end(),v)-yvs.begin();
}
int ub(int v){
	return upper_bound(yvs.begin(),yvs.end(),v)-yvs.begin();
}

int check(int dist){
	for (int x = 0; x<250005; x++) fenw[x] = 0;
	int c = 0;
	int ans = 0;
	for (auto x : pts){
		while (c<pts.size() && pts[c].first.first<=x.first.first+dist){
			upd(lb(pts[c].first.second),1);
			c++;
		}
		upd(lb(x.first.second),-1);
		ans += qu(ub(x.first.second+dist)-1)-qu(lb(x.first.second-dist)-1);
	}
	return ans;
}

vector<pair<int,int> > getall(int dist){
	int c = 0;
	vector<pair<int,int> > ans;
	set<pair<int,int> > stuff;
	for (auto x : pts){
		while (c<pts.size() && pts[c].first.first<=x.first.first+dist){
			stuff.insert({pts[c].first.second,pts[c].second});
			//printf("ins %lld\n",c);
			c++;
		}
		stuff.erase({x.first.second,x.second});
		//printf("er %lld\n",x.second);
		for (auto it = stuff.lower_bound({x.first.second-dist,-1}); (it!=stuff.end()) && (*it).first<=x.first.second+dist; it++){
			ans.push_back({x.second,(*it).second});
			assert(x.second!=(*it).second);
		}
	}
	return ans;
}

int md(pair<int,int> x){
	return abs(origX[x.first]-origX[x.second])+abs(origY[x.first]-origY[x.second]);
}

bool cmp(pair<int,int> a,pair<int,int>b){
	return md(a)<md(b);
}


main(){
	int n,K;
	scanf("%lld%lld",&n,&K);
	for (int x = 0; x<n; x++){
		scanf("%lld%lld",&origX[x],&origY[x]);
		nX[x] = origX[x]+origY[x];
		nY[x] = origY[x]-origX[x];
		pts.push_back({{nX[x],nY[x]},x});
		yvs.push_back(nY[x]);
	}
	sort(pts.begin(),pts.end());
	sort(yvs.begin(),yvs.end());
	
	int l = 0;
	int r = 4000000000LL;
	while (l+1<r){
		int mid = (l+r)/2;
		if (check(mid)>=K){
			r = mid;
		}
		else l = mid;
	}
	
	vector<pair<int,int>> impt = getall(l);
	sort(impt.begin(),impt.end(),cmp);
	for (auto x : impt){
		printf("%lld\n",abs(origX[x.first]-origX[x.second])+abs(origY[x.first]-origY[x.second]));
	}
	for (int x = impt.size(); x<K; x++){
		printf("%lld\n",r);
	}
	
}

Compilation message

road_construction.cpp: In function 'long long int check(long long int)':
road_construction.cpp:46:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |   while (c<pts.size() && pts[c].first.first<=x.first.first+dist){
      |          ~^~~~~~~~~~~
road_construction.cpp: In function 'std::vector<std::pair<long long int, long long int> > getall(long long int)':
road_construction.cpp:61:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |   while (c<pts.size() && pts[c].first.first<=x.first.first+dist){
      |          ~^~~~~~~~~~~
road_construction.cpp: At global scope:
road_construction.cpp:85:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   85 | main(){
      | ^~~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:87:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |  scanf("%lld%lld",&n,&K);
      |  ~~~~~^~~~~~~~~~~~~~~~~~
road_construction.cpp:89:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |   scanf("%lld%lld",&origX[x],&origY[x]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 9008 KB Output is correct
2 Correct 78 ms 8924 KB Output is correct
3 Correct 76 ms 9136 KB Output is correct
4 Correct 74 ms 9160 KB Output is correct
5 Correct 75 ms 7892 KB Output is correct
6 Correct 7 ms 2384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1863 ms 28700 KB Output is correct
2 Correct 1821 ms 28908 KB Output is correct
3 Correct 81 ms 8888 KB Output is correct
4 Correct 1884 ms 29660 KB Output is correct
5 Correct 1757 ms 28896 KB Output is correct
6 Correct 1805 ms 28796 KB Output is correct
7 Correct 1799 ms 27988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4433 ms 21584 KB Output is correct
2 Correct 4622 ms 21616 KB Output is correct
3 Correct 2 ms 2260 KB Output is correct
4 Correct 1866 ms 21556 KB Output is correct
5 Correct 3650 ms 21596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4433 ms 21584 KB Output is correct
2 Correct 4622 ms 21616 KB Output is correct
3 Correct 2 ms 2260 KB Output is correct
4 Correct 1866 ms 21556 KB Output is correct
5 Correct 3650 ms 21596 KB Output is correct
6 Correct 4418 ms 21628 KB Output is correct
7 Correct 4781 ms 24160 KB Output is correct
8 Correct 3 ms 2260 KB Output is correct
9 Correct 4 ms 2260 KB Output is correct
10 Correct 4746 ms 24164 KB Output is correct
11 Correct 1717 ms 22892 KB Output is correct
12 Correct 3587 ms 24168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 9008 KB Output is correct
2 Correct 78 ms 8924 KB Output is correct
3 Correct 76 ms 9136 KB Output is correct
4 Correct 74 ms 9160 KB Output is correct
5 Correct 75 ms 7892 KB Output is correct
6 Correct 7 ms 2384 KB Output is correct
7 Correct 1887 ms 18616 KB Output is correct
8 Correct 1857 ms 19012 KB Output is correct
9 Correct 74 ms 9000 KB Output is correct
10 Correct 1733 ms 18220 KB Output is correct
11 Correct 1619 ms 18124 KB Output is correct
12 Correct 1106 ms 13500 KB Output is correct
13 Correct 1255 ms 17580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 9008 KB Output is correct
2 Correct 78 ms 8924 KB Output is correct
3 Correct 76 ms 9136 KB Output is correct
4 Correct 74 ms 9160 KB Output is correct
5 Correct 75 ms 7892 KB Output is correct
6 Correct 7 ms 2384 KB Output is correct
7 Correct 1863 ms 28700 KB Output is correct
8 Correct 1821 ms 28908 KB Output is correct
9 Correct 81 ms 8888 KB Output is correct
10 Correct 1884 ms 29660 KB Output is correct
11 Correct 1757 ms 28896 KB Output is correct
12 Correct 1805 ms 28796 KB Output is correct
13 Correct 1799 ms 27988 KB Output is correct
14 Correct 4433 ms 21584 KB Output is correct
15 Correct 4622 ms 21616 KB Output is correct
16 Correct 2 ms 2260 KB Output is correct
17 Correct 1866 ms 21556 KB Output is correct
18 Correct 3650 ms 21596 KB Output is correct
19 Correct 4418 ms 21628 KB Output is correct
20 Correct 4781 ms 24160 KB Output is correct
21 Correct 3 ms 2260 KB Output is correct
22 Correct 4 ms 2260 KB Output is correct
23 Correct 4746 ms 24164 KB Output is correct
24 Correct 1717 ms 22892 KB Output is correct
25 Correct 3587 ms 24168 KB Output is correct
26 Correct 1887 ms 18616 KB Output is correct
27 Correct 1857 ms 19012 KB Output is correct
28 Correct 74 ms 9000 KB Output is correct
29 Correct 1733 ms 18220 KB Output is correct
30 Correct 1619 ms 18124 KB Output is correct
31 Correct 1106 ms 13500 KB Output is correct
32 Correct 1255 ms 17580 KB Output is correct
33 Correct 5095 ms 33144 KB Output is correct
34 Correct 5473 ms 33164 KB Output is correct
35 Correct 4831 ms 32376 KB Output is correct
36 Correct 2941 ms 25856 KB Output is correct
37 Correct 2887 ms 26112 KB Output is correct
38 Correct 3723 ms 31612 KB Output is correct