Submission #833122

# Submission time Handle Problem Language Result Execution time Memory
833122 2023-08-22T01:36:20 Z 79brue Sky Walking (IOI19_walk) C++17
100 / 100
903 ms 187760 KB
#include "walk.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Edge{
	int l, r, y, idx;
	Edge(){}
	Edge(int l, int r, int y, int idx): l(l), r(r), y(y), idx(idx){}
	bool operator<(const Edge &r)const{
		if(y != r.y) return y < r.y;
		return idx < r.idx;
	}
};

int n, k, Start, Goal;
ll arr[100002];
ll height[100002];
ll el[100002], er[100002], ey[100002];

void findIntersections();
ll solve();

ll min_distance(vector<int> _x, vector<int> _h, vector<int> _l, vector<int> _r,
			    vector<int> _y, int _s, int _t) {
	n = (int)_x.size();
	for(int i=1; i<=n; i++) arr[i] = _x[i-1], height[i] = _h[i-1];
	k = (int)_l.size();

	vector<Edge> vec;
	for(int i=1; i<=k; i++) vec.push_back(Edge(_l[i-1] + 1, _r[i-1] + 1, _y[i-1], i));
	sort(vec.begin(), vec.end());
	
	for(int i=1; i<=k; i++) el[i] = vec[i-1].l, er[i] = vec[i-1].r, ey[i] = vec[i-1].y;
	
	Start = _s+1, Goal = _t+1;

	findIntersections();
	return solve();
}

/// 정점과 간선 정보
int floorV[100002];
vector<pair<int, ll> > link[2000002];
vector<int> addLine[100002], delLine[100002];

vector<pair<int, int> > edgeIdx[100002]; /// 간선별로 정점을 추가할 x좌표들, 그리고 그 정점 번호
vector<pair<int, int> > towerIdx[100002]; /// x좌표별로 정점을 추가할 높이들, 그리고 그 정점 번호
int recent[100002];

vector<int> insertionPoint[100002];

void findIntersections(){
	for(int i=1; i<=k; i++){
		addLine[el[i]].push_back(i);
		delLine[er[i]].push_back(i);
	}

	/// Subtask 4의 정점들만 추가
	int vCnt = 0;
	set<int> st; /// 간선 번호
	for(int i=1; i<=n; i++){
		for(int x: addLine[i]){
			// printf("addLine %d - %d\n", i, x);
			edgeIdx[x].push_back(make_pair(arr[i], ++vCnt));
			towerIdx[i].push_back(make_pair(ey[x], vCnt));
			// printf("%d: (%d, %d)\n", vCnt, arr[i], ey[x]);
			recent[x] = i;

			auto it = st.insert(x).first;
			if(it != st.begin() && recent[*prev(it)] != i){
				int y = *prev(it);
				edgeIdx[y].push_back(make_pair(arr[i], ++vCnt));
				towerIdx[i].push_back(make_pair(ey[y], vCnt));
				// printf("%d: (%d, %d)\n", vCnt, arr[i], ey[y]);
				recent[y] = i;
			}
			if(next(it) != st.end() && recent[*next(it)] != i){
				int y = *next(it);
				edgeIdx[y].push_back(make_pair(arr[i], ++vCnt));
				towerIdx[i].push_back(make_pair(ey[y], vCnt));
				// printf("%d: (%d, %d)\n", vCnt, arr[i], ey[y]);
				recent[y] = i;
			}
		}
		for(int x: delLine[i]){
			// printf("delLine %d - %d\n", i, x);
			edgeIdx[x].push_back(make_pair(arr[i], ++vCnt));
			towerIdx[i].push_back(make_pair(ey[x], vCnt));
			// printf("%d: (%d, %d)\n", vCnt, arr[i], ey[x]);
			recent[x] = i;

			auto it = st.find(x);
			if(it != st.begin()){
				int y = *prev(it);
				edgeIdx[y].push_back(make_pair(arr[i], ++vCnt));
				towerIdx[i].push_back(make_pair(ey[y], vCnt));
				// printf("%d: (%d, %d)\n", vCnt, arr[i], ey[y]);
				recent[y] = i;
			}
			if(next(it) != st.end()){
				int y = *next(it);
				edgeIdx[y].push_back(make_pair(arr[i], ++vCnt));
				towerIdx[i].push_back(make_pair(ey[y], vCnt));
				// printf("%d: (%d, %d)\n", vCnt, arr[i], ey[y]);
				recent[y] = i;
			}
			st.erase(x);
		}
		towerIdx[i].push_back(make_pair(0, ++vCnt));
		floorV[i] = vCnt;
		// printf("%d: (%d, %d)\n", vCnt, arr[i], 0);
	}

	/// Full task의 정점들도 추가
	st.clear();
	for(int i=1; i<=n; i++){
		auto it = prev(upper_bound(ey+1, ey+k+1, height[i]));
		if(it == ey) continue;
		insertionPoint[it-ey].push_back(i);
		// printf("%d: bound %d (height[i] %d)\n", i, it-ey, height[i]);
	}
	for(int i=k; i>=1; i--){
		for(int x: insertionPoint[i]){
			// printf("Inserted %d in %d\n", x, i);
			st.insert(x);
		}

		auto it = st.lower_bound(Start);
		if(it != st.end() && el[i] <= *it && *it <= er[i]){
			int x = *it;
			edgeIdx[i].push_back(make_pair(arr[x], ++vCnt));
			towerIdx[x].push_back(make_pair(ey[i], vCnt));
			// printf("%d: (%d, %d)\n", vCnt, arr[x], ey[i]);
		}
		if(it != st.begin() && el[i] <= *prev(it) && *prev(it) <= er[i]){
			int x = *prev(it);
			edgeIdx[i].push_back(make_pair(arr[x], ++vCnt));
			towerIdx[x].push_back(make_pair(ey[i], vCnt));
			// printf("%d: (%d, %d)\n", vCnt, arr[x], ey[i]);
		}

		it = st.lower_bound(Goal);
		if(it != st.end() && el[i] <= *it && *it <= er[i]){
			int x = *it;
			edgeIdx[i].push_back(make_pair(arr[x], ++vCnt));
			towerIdx[x].push_back(make_pair(ey[i], vCnt));
			// printf("%d: (%d, %d)\n", vCnt, arr[x], ey[i]);
		}
		if(it != st.begin() && el[i] <= *prev(it) && *prev(it) <= er[i]){
			int x = *prev(it);
			edgeIdx[i].push_back(make_pair(arr[x], ++vCnt));
			towerIdx[x].push_back(make_pair(ey[i], vCnt));
			// printf("%d: (%d, %d)\n", vCnt, arr[x], ey[i]);
		}
	}

	/// 정점들을 간선으로 연결
	for(int i=1; i<=k; i++){
		sort(edgeIdx[i].begin(), edgeIdx[i].end());
		for(int j=0; j<(int)edgeIdx[i].size()-1; j++){
			int x = edgeIdx[i][j].second, y = edgeIdx[i][j+1].second;
			int v = edgeIdx[i][j+1].first - edgeIdx[i][j].first;
			// printf("Connect %d %d : %d\n", x, y, v);
			link[x].push_back(make_pair(y, v));
			link[y].push_back(make_pair(x, v));
		}
	}

	for(int i=1; i<=n; i++){
		sort(towerIdx[i].begin(), towerIdx[i].end());
		for(int j=0; j<(int)towerIdx[i].size()-1; j++){
			// printf("i %d j %d arr[i] %d towerIdx[i][j+1].first %d\n", i, j, height[i], towerIdx[i][j+1].first);
			if(height[i] < towerIdx[i][j+1].first){
				//printf("Breaked because height[%d] = %d, towerIdx[%d][%d] = %d\n", i,height[i],i,j+1,towerIdx[i][j+1].first);
				break;
			}
			int x = towerIdx[i][j].second, y = towerIdx[i][j+1].second;
			int v = towerIdx[i][j+1].first - towerIdx[i][j].first;
			// printf("Connect %d %d : %d\n", x, y, v);
			link[x].push_back(make_pair(y, v));
			link[y].push_back(make_pair(x, v));
		}
	}
	assert(vCnt <= 2000000);
}

struct dat{
	int x; ll d;
	dat(){}
	dat(int x, ll d): x(x), d(d){}
	bool operator<(const dat &r)const{
		return d>r.d;
	}
};
bool visited[2000002];

ll solve(){
	// printf("Solve %d to %d\n", floorV[Start], floorV[Goal]);
	priority_queue<dat> pq;
	pq.push(dat(floorV[Start], 0));
	while(!pq.empty()){
		dat tmp = pq.top(); pq.pop();
		if(visited[tmp.x]) continue;
		// printf("%d: %lld\n", tmp.x, tmp.d);
		if(tmp.x == floorV[Goal]) return tmp.d;
		visited[tmp.x] = 1;

		for(pair<int, ll> p: link[tmp.x]){
			pq.push(dat(p.first, tmp.d + p.second));
		}
	}
	// // printf("No solution\n");
	return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 27 ms 58976 KB Output is correct
2 Correct 23 ms 58980 KB Output is correct
3 Correct 23 ms 59020 KB Output is correct
4 Correct 23 ms 59012 KB Output is correct
5 Correct 24 ms 59016 KB Output is correct
6 Correct 27 ms 59004 KB Output is correct
7 Correct 24 ms 59048 KB Output is correct
8 Correct 24 ms 59052 KB Output is correct
9 Correct 25 ms 58972 KB Output is correct
10 Correct 25 ms 59020 KB Output is correct
11 Correct 23 ms 59048 KB Output is correct
12 Correct 24 ms 59096 KB Output is correct
13 Correct 25 ms 59072 KB Output is correct
14 Correct 24 ms 59024 KB Output is correct
15 Correct 24 ms 59060 KB Output is correct
16 Correct 25 ms 59012 KB Output is correct
17 Correct 35 ms 58996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 58992 KB Output is correct
2 Correct 25 ms 59024 KB Output is correct
3 Correct 281 ms 129104 KB Output is correct
4 Correct 474 ms 148808 KB Output is correct
5 Correct 261 ms 128388 KB Output is correct
6 Correct 300 ms 129680 KB Output is correct
7 Correct 270 ms 128516 KB Output is correct
8 Correct 317 ms 130812 KB Output is correct
9 Correct 356 ms 139804 KB Output is correct
10 Correct 540 ms 146368 KB Output is correct
11 Correct 360 ms 129404 KB Output is correct
12 Correct 335 ms 133780 KB Output is correct
13 Correct 502 ms 150616 KB Output is correct
14 Correct 320 ms 144636 KB Output is correct
15 Correct 377 ms 140048 KB Output is correct
16 Correct 342 ms 136136 KB Output is correct
17 Correct 319 ms 136592 KB Output is correct
18 Correct 460 ms 141164 KB Output is correct
19 Correct 37 ms 63188 KB Output is correct
20 Correct 159 ms 101508 KB Output is correct
21 Correct 324 ms 132052 KB Output is correct
22 Correct 281 ms 137508 KB Output is correct
23 Correct 491 ms 154328 KB Output is correct
24 Correct 320 ms 135448 KB Output is correct
25 Correct 321 ms 131120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 90424 KB Output is correct
2 Correct 474 ms 134788 KB Output is correct
3 Correct 580 ms 135584 KB Output is correct
4 Correct 731 ms 150000 KB Output is correct
5 Correct 903 ms 151644 KB Output is correct
6 Correct 730 ms 156760 KB Output is correct
7 Correct 399 ms 110976 KB Output is correct
8 Correct 312 ms 128764 KB Output is correct
9 Correct 859 ms 160704 KB Output is correct
10 Correct 412 ms 131436 KB Output is correct
11 Correct 48 ms 64840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 90424 KB Output is correct
2 Correct 474 ms 134788 KB Output is correct
3 Correct 580 ms 135584 KB Output is correct
4 Correct 731 ms 150000 KB Output is correct
5 Correct 903 ms 151644 KB Output is correct
6 Correct 730 ms 156760 KB Output is correct
7 Correct 399 ms 110976 KB Output is correct
8 Correct 312 ms 128764 KB Output is correct
9 Correct 859 ms 160704 KB Output is correct
10 Correct 412 ms 131436 KB Output is correct
11 Correct 48 ms 64840 KB Output is correct
12 Correct 571 ms 136112 KB Output is correct
13 Correct 449 ms 150188 KB Output is correct
14 Correct 797 ms 152884 KB Output is correct
15 Correct 563 ms 133312 KB Output is correct
16 Correct 614 ms 140416 KB Output is correct
17 Correct 720 ms 147860 KB Output is correct
18 Correct 560 ms 133544 KB Output is correct
19 Correct 644 ms 140300 KB Output is correct
20 Correct 415 ms 112352 KB Output is correct
21 Correct 75 ms 71872 KB Output is correct
22 Correct 331 ms 132848 KB Output is correct
23 Correct 292 ms 132008 KB Output is correct
24 Correct 261 ms 122964 KB Output is correct
25 Correct 269 ms 128328 KB Output is correct
26 Correct 231 ms 118792 KB Output is correct
27 Correct 846 ms 155008 KB Output is correct
28 Correct 405 ms 148644 KB Output is correct
29 Correct 747 ms 155516 KB Output is correct
30 Correct 439 ms 111552 KB Output is correct
31 Correct 719 ms 161160 KB Output is correct
32 Correct 332 ms 128580 KB Output is correct
33 Correct 322 ms 131236 KB Output is correct
34 Correct 426 ms 141568 KB Output is correct
35 Correct 393 ms 136444 KB Output is correct
36 Correct 382 ms 129668 KB Output is correct
37 Correct 338 ms 129368 KB Output is correct
38 Correct 308 ms 134448 KB Output is correct
39 Correct 545 ms 151260 KB Output is correct
40 Correct 336 ms 132520 KB Output is correct
41 Correct 326 ms 128284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 58976 KB Output is correct
2 Correct 23 ms 58980 KB Output is correct
3 Correct 23 ms 59020 KB Output is correct
4 Correct 23 ms 59012 KB Output is correct
5 Correct 24 ms 59016 KB Output is correct
6 Correct 27 ms 59004 KB Output is correct
7 Correct 24 ms 59048 KB Output is correct
8 Correct 24 ms 59052 KB Output is correct
9 Correct 25 ms 58972 KB Output is correct
10 Correct 25 ms 59020 KB Output is correct
11 Correct 23 ms 59048 KB Output is correct
12 Correct 24 ms 59096 KB Output is correct
13 Correct 25 ms 59072 KB Output is correct
14 Correct 24 ms 59024 KB Output is correct
15 Correct 24 ms 59060 KB Output is correct
16 Correct 25 ms 59012 KB Output is correct
17 Correct 35 ms 58996 KB Output is correct
18 Correct 26 ms 58992 KB Output is correct
19 Correct 25 ms 59024 KB Output is correct
20 Correct 281 ms 129104 KB Output is correct
21 Correct 474 ms 148808 KB Output is correct
22 Correct 261 ms 128388 KB Output is correct
23 Correct 300 ms 129680 KB Output is correct
24 Correct 270 ms 128516 KB Output is correct
25 Correct 317 ms 130812 KB Output is correct
26 Correct 356 ms 139804 KB Output is correct
27 Correct 540 ms 146368 KB Output is correct
28 Correct 360 ms 129404 KB Output is correct
29 Correct 335 ms 133780 KB Output is correct
30 Correct 502 ms 150616 KB Output is correct
31 Correct 320 ms 144636 KB Output is correct
32 Correct 377 ms 140048 KB Output is correct
33 Correct 342 ms 136136 KB Output is correct
34 Correct 319 ms 136592 KB Output is correct
35 Correct 460 ms 141164 KB Output is correct
36 Correct 37 ms 63188 KB Output is correct
37 Correct 159 ms 101508 KB Output is correct
38 Correct 324 ms 132052 KB Output is correct
39 Correct 281 ms 137508 KB Output is correct
40 Correct 491 ms 154328 KB Output is correct
41 Correct 320 ms 135448 KB Output is correct
42 Correct 321 ms 131120 KB Output is correct
43 Correct 159 ms 90424 KB Output is correct
44 Correct 474 ms 134788 KB Output is correct
45 Correct 580 ms 135584 KB Output is correct
46 Correct 731 ms 150000 KB Output is correct
47 Correct 903 ms 151644 KB Output is correct
48 Correct 730 ms 156760 KB Output is correct
49 Correct 399 ms 110976 KB Output is correct
50 Correct 312 ms 128764 KB Output is correct
51 Correct 859 ms 160704 KB Output is correct
52 Correct 412 ms 131436 KB Output is correct
53 Correct 48 ms 64840 KB Output is correct
54 Correct 571 ms 136112 KB Output is correct
55 Correct 449 ms 150188 KB Output is correct
56 Correct 797 ms 152884 KB Output is correct
57 Correct 563 ms 133312 KB Output is correct
58 Correct 614 ms 140416 KB Output is correct
59 Correct 720 ms 147860 KB Output is correct
60 Correct 560 ms 133544 KB Output is correct
61 Correct 644 ms 140300 KB Output is correct
62 Correct 415 ms 112352 KB Output is correct
63 Correct 75 ms 71872 KB Output is correct
64 Correct 331 ms 132848 KB Output is correct
65 Correct 292 ms 132008 KB Output is correct
66 Correct 261 ms 122964 KB Output is correct
67 Correct 269 ms 128328 KB Output is correct
68 Correct 231 ms 118792 KB Output is correct
69 Correct 846 ms 155008 KB Output is correct
70 Correct 405 ms 148644 KB Output is correct
71 Correct 747 ms 155516 KB Output is correct
72 Correct 439 ms 111552 KB Output is correct
73 Correct 719 ms 161160 KB Output is correct
74 Correct 332 ms 128580 KB Output is correct
75 Correct 322 ms 131236 KB Output is correct
76 Correct 426 ms 141568 KB Output is correct
77 Correct 393 ms 136444 KB Output is correct
78 Correct 382 ms 129668 KB Output is correct
79 Correct 338 ms 129368 KB Output is correct
80 Correct 308 ms 134448 KB Output is correct
81 Correct 545 ms 151260 KB Output is correct
82 Correct 336 ms 132520 KB Output is correct
83 Correct 326 ms 128284 KB Output is correct
84 Correct 132 ms 85476 KB Output is correct
85 Correct 502 ms 140548 KB Output is correct
86 Correct 841 ms 173228 KB Output is correct
87 Correct 88 ms 78356 KB Output is correct
88 Correct 107 ms 79656 KB Output is correct
89 Correct 91 ms 78320 KB Output is correct
90 Correct 64 ms 68016 KB Output is correct
91 Correct 36 ms 59292 KB Output is correct
92 Correct 54 ms 63324 KB Output is correct
93 Correct 220 ms 101516 KB Output is correct
94 Correct 83 ms 73928 KB Output is correct
95 Correct 334 ms 140008 KB Output is correct
96 Correct 302 ms 137148 KB Output is correct
97 Correct 306 ms 136384 KB Output is correct
98 Correct 277 ms 132048 KB Output is correct
99 Correct 740 ms 187760 KB Output is correct
100 Correct 655 ms 155236 KB Output is correct
101 Correct 546 ms 165480 KB Output is correct
102 Correct 270 ms 114436 KB Output is correct
103 Correct 372 ms 135296 KB Output is correct
104 Correct 340 ms 134360 KB Output is correct
105 Correct 477 ms 142760 KB Output is correct
106 Correct 379 ms 136912 KB Output is correct
107 Correct 424 ms 146196 KB Output is correct
108 Correct 77 ms 66916 KB Output is correct
109 Correct 654 ms 139388 KB Output is correct
110 Correct 428 ms 152480 KB Output is correct
111 Correct 357 ms 151228 KB Output is correct
112 Correct 395 ms 139400 KB Output is correct
113 Correct 369 ms 135476 KB Output is correct