Submission #211003

# Submission time Handle Problem Language Result Execution time Memory
211003 2020-03-19T05:24:34 Z oolimry Sky Walking (IOI19_walk) C++14
100 / 100
1259 ms 119388 KB
#include "walk.h"
#include <bits/stdc++.h>

using namespace std;
typedef pair<long long, long long> ii;
long long inf = (1LL << 62LL);

///X, Y refers to points & buildings. L, R, H refers to skywalks
struct walk { long long l, r, h; };
struct vertex { long long x, y, id; };

long long min_distance(vector<int> X, vector<int> Y, vector<int> L, vector<int> R, vector<int> H, int START, int END) {
	long long n = X.size(), m = L.size();
	walk skywalks[m];
	for(long long i = 0;i < m;i++) skywalks[i] = {L[i], R[i], H[i]};
	
	vector<long long> useful[n];
	useful[START].push_back(0);
	useful[END].push_back(0);
	
	for(walk w : skywalks){
		useful[w.l].push_back(w.h);
		useful[w.r].push_back(w.h);
	}
	
	///For skywalks crossing START and END, Split them
	set<long long> aboveLine; ///A sweeping line going from bottom to top;
	
	vector<long long> walkHeight; ///walk indeces sorted in increasing H
	for(long long i = 0;i < m;i++) walkHeight.push_back(i);
	sort(walkHeight.begin(), walkHeight.end(), [&](long long a, long long b) { return H[a] < H[b]; });
	
	vector<ii> buildingHeight; ///building indeces soreted in increasing Y
	for(long long i = 0;i < n;i++){
		buildingHeight.push_back(ii(Y[i], i));
		aboveLine.insert(i);
	}
	sort(buildingHeight.begin(), buildingHeight.end());
	long long ptr = 0;
	
	for(long long i = 0;i < m;i++){
		long long w = walkHeight[i];
		while(ptr < n && buildingHeight[ptr].first < H[w]){
			aboveLine.erase(buildingHeight[ptr].second);
			ptr++;
		}
		
		if(L[w] <= START && START <= R[w]){
			auto it = aboveLine.lower_bound(START);
			if(*it == START){
				useful[START].push_back(H[w]);
			}
			else{
				useful[*it].push_back(H[w]);
				it--;
				useful[*it].push_back(H[w]);
			}
		}
		
		if(L[w] <= END && END <= R[w]){
			auto it = aboveLine.lower_bound(END);
			if(*it == END){
				useful[END].push_back(H[w]);
			}
			else{
				useful[*it].push_back(H[w]);
				it--;
				useful[*it].push_back(H[w]);
			}
		}
		
	}
	
	vector<ii> event[n]; ///process walks from left to right
	for(walk w : skywalks){
		event[w.l].push_back(ii(w.h, 1));
		if(w.r != n-1) event[w.r+1].push_back(ii(w.h, -1));
	}
	
	multiset<long long> walkHeights;
	for(long long i = 0;i < n;i++){ 
		for(ii Ev : event[i]){
			if(Ev.second == 1) walkHeights.insert(Ev.first);
			else walkHeights.erase(walkHeights.find(Ev.first));
		}
		
		vector<long long> moreUseful; ///extra points to add to useful[i]
		
		for(long long h : useful[i]){
			multiset<long long>::iterator it = walkHeights.upper_bound(h);
			//if(it != walkHeights.end()) moreUseful.push_back(*it);
			if(it == walkHeights.begin()) continue;
			it--;
			if(it != walkHeights.begin()) moreUseful.push_back(*(--it));
		}
		
		for(long long u : moreUseful) useful[i].push_back(u);
		sort(useful[i].begin(),useful[i].end());
		useful[i].erase(unique(useful[i].begin(),useful[i].end()), useful[i].end());
		while(useful[i].size() > 0 && useful[i].back() > Y[i]) useful[i].pop_back();
	}
	
	
	vector<vertex> vertices;
	long long S, E; ///Start and End based on renumbering
	long long idx = 0;
	
	for(long long i = 0;i < n;i++){
		if(i == START) S = idx;
		if(i == END) E = idx;
		for(long long y : useful[i]){
			vertices.push_back({X[i], y, idx}); 
			idx++;
		}
	}
	
	long long N = vertices.size(); ///big N is for no. of nodes on the graph
	vector<ii> adj[N];
	long long vis[N];
	fill(vis, vis + N, inf);
	
	for(long long i = 1;i < N;i++){
		if(vertices[i].x == vertices[i-1].x){
			long long w = vertices[i].y - vertices[i-1].y;
			adj[i].push_back(ii(w,i-1));
			adj[i-1].push_back(ii(w,i));
		}
	}
	
	auto compY = [&](vertex a, vertex b){ return ii(a.y,a.x) < ii(b.y,b.x); };
	sort(vertices.begin(),vertices.end(),compY);
	
	for(walk w : skywalks){
		int pos = lower_bound(vertices.begin(),vertices.end(), (vertex){X[w.l], w.h, -1} ,compY) - vertices.begin();
		while(vertices[pos].x < X[w.r]){
			long long u = vertices[pos].id;
			pos++;
			long long v = vertices[pos].id;
			long long w = vertices[pos].x - vertices[pos-1].x;
			adj[u].push_back(ii(w,v));
			adj[v].push_back(ii(w,u));
		}
	}
	
	///Dijkstra
	priority_queue<ii, vector<ii>, greater<ii> > dij;
	dij.push(ii(0,S)); vis[S] = 0;
	while(!dij.empty()){
		ii u = dij.top(); dij.pop();
		for(ii v : adj[u.second]){
			if(vis[v.second] > u.first + v.first){
				vis[v.second] = u.first + v.first;
				dij.push(ii(vis[v.second], v.second));
			}
		}
	}
	
	if(vis[E] >= inf) vis[E] = -1;
	return vis[E];
}

Compilation message

walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:147:28: warning: 'S' may be used uninitialized in this function [-Wmaybe-uninitialized]
  dij.push(ii(0,S)); vis[S] = 0;
                     ~~~~~~~^~~
walk.cpp:105:15: warning: 'E' may be used uninitialized in this function [-Wmaybe-uninitialized]
  long long S, E; ///Start and End based on renumbering
               ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 256 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 256 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 571 ms 63668 KB Output is correct
4 Correct 679 ms 69200 KB Output is correct
5 Correct 407 ms 52800 KB Output is correct
6 Correct 413 ms 51168 KB Output is correct
7 Correct 406 ms 53216 KB Output is correct
8 Correct 591 ms 65868 KB Output is correct
9 Correct 536 ms 64720 KB Output is correct
10 Correct 653 ms 69836 KB Output is correct
11 Correct 474 ms 56272 KB Output is correct
12 Correct 422 ms 46084 KB Output is correct
13 Correct 641 ms 72780 KB Output is correct
14 Correct 423 ms 47324 KB Output is correct
15 Correct 442 ms 49372 KB Output is correct
16 Correct 407 ms 48348 KB Output is correct
17 Correct 392 ms 46432 KB Output is correct
18 Correct 560 ms 59052 KB Output is correct
19 Correct 16 ms 2748 KB Output is correct
20 Correct 144 ms 22624 KB Output is correct
21 Correct 447 ms 47576 KB Output is correct
22 Correct 359 ms 45528 KB Output is correct
23 Correct 501 ms 59460 KB Output is correct
24 Correct 362 ms 49868 KB Output is correct
25 Correct 399 ms 50172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 15980 KB Output is correct
2 Correct 644 ms 71016 KB Output is correct
3 Correct 684 ms 74188 KB Output is correct
4 Correct 825 ms 84400 KB Output is correct
5 Correct 950 ms 87908 KB Output is correct
6 Correct 870 ms 82816 KB Output is correct
7 Correct 385 ms 48384 KB Output is correct
8 Correct 370 ms 50500 KB Output is correct
9 Correct 891 ms 80900 KB Output is correct
10 Correct 451 ms 52792 KB Output is correct
11 Correct 34 ms 6772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 15980 KB Output is correct
2 Correct 644 ms 71016 KB Output is correct
3 Correct 684 ms 74188 KB Output is correct
4 Correct 825 ms 84400 KB Output is correct
5 Correct 950 ms 87908 KB Output is correct
6 Correct 870 ms 82816 KB Output is correct
7 Correct 385 ms 48384 KB Output is correct
8 Correct 370 ms 50500 KB Output is correct
9 Correct 891 ms 80900 KB Output is correct
10 Correct 451 ms 52792 KB Output is correct
11 Correct 34 ms 6772 KB Output is correct
12 Correct 695 ms 73520 KB Output is correct
13 Correct 791 ms 78796 KB Output is correct
14 Correct 970 ms 83276 KB Output is correct
15 Correct 641 ms 72908 KB Output is correct
16 Correct 661 ms 71116 KB Output is correct
17 Correct 731 ms 79672 KB Output is correct
18 Correct 831 ms 72904 KB Output is correct
19 Correct 717 ms 71532 KB Output is correct
20 Correct 451 ms 43360 KB Output is correct
21 Correct 107 ms 13416 KB Output is correct
22 Correct 523 ms 64464 KB Output is correct
23 Correct 487 ms 63048 KB Output is correct
24 Correct 511 ms 50512 KB Output is correct
25 Correct 456 ms 58188 KB Output is correct
26 Correct 458 ms 42916 KB Output is correct
27 Correct 1010 ms 84048 KB Output is correct
28 Correct 686 ms 78412 KB Output is correct
29 Correct 899 ms 77752 KB Output is correct
30 Correct 478 ms 43740 KB Output is correct
31 Correct 849 ms 75952 KB Output is correct
32 Correct 373 ms 49492 KB Output is correct
33 Correct 381 ms 48336 KB Output is correct
34 Correct 458 ms 53764 KB Output is correct
35 Correct 458 ms 54236 KB Output is correct
36 Correct 414 ms 50520 KB Output is correct
37 Correct 358 ms 47476 KB Output is correct
38 Correct 370 ms 45408 KB Output is correct
39 Correct 500 ms 59460 KB Output is correct
40 Correct 435 ms 49884 KB Output is correct
41 Correct 389 ms 50140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 256 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 256 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 571 ms 63668 KB Output is correct
21 Correct 679 ms 69200 KB Output is correct
22 Correct 407 ms 52800 KB Output is correct
23 Correct 413 ms 51168 KB Output is correct
24 Correct 406 ms 53216 KB Output is correct
25 Correct 591 ms 65868 KB Output is correct
26 Correct 536 ms 64720 KB Output is correct
27 Correct 653 ms 69836 KB Output is correct
28 Correct 474 ms 56272 KB Output is correct
29 Correct 422 ms 46084 KB Output is correct
30 Correct 641 ms 72780 KB Output is correct
31 Correct 423 ms 47324 KB Output is correct
32 Correct 442 ms 49372 KB Output is correct
33 Correct 407 ms 48348 KB Output is correct
34 Correct 392 ms 46432 KB Output is correct
35 Correct 560 ms 59052 KB Output is correct
36 Correct 16 ms 2748 KB Output is correct
37 Correct 144 ms 22624 KB Output is correct
38 Correct 447 ms 47576 KB Output is correct
39 Correct 359 ms 45528 KB Output is correct
40 Correct 501 ms 59460 KB Output is correct
41 Correct 362 ms 49868 KB Output is correct
42 Correct 399 ms 50172 KB Output is correct
43 Correct 101 ms 15980 KB Output is correct
44 Correct 644 ms 71016 KB Output is correct
45 Correct 684 ms 74188 KB Output is correct
46 Correct 825 ms 84400 KB Output is correct
47 Correct 950 ms 87908 KB Output is correct
48 Correct 870 ms 82816 KB Output is correct
49 Correct 385 ms 48384 KB Output is correct
50 Correct 370 ms 50500 KB Output is correct
51 Correct 891 ms 80900 KB Output is correct
52 Correct 451 ms 52792 KB Output is correct
53 Correct 34 ms 6772 KB Output is correct
54 Correct 695 ms 73520 KB Output is correct
55 Correct 791 ms 78796 KB Output is correct
56 Correct 970 ms 83276 KB Output is correct
57 Correct 641 ms 72908 KB Output is correct
58 Correct 661 ms 71116 KB Output is correct
59 Correct 731 ms 79672 KB Output is correct
60 Correct 831 ms 72904 KB Output is correct
61 Correct 717 ms 71532 KB Output is correct
62 Correct 451 ms 43360 KB Output is correct
63 Correct 107 ms 13416 KB Output is correct
64 Correct 523 ms 64464 KB Output is correct
65 Correct 487 ms 63048 KB Output is correct
66 Correct 511 ms 50512 KB Output is correct
67 Correct 456 ms 58188 KB Output is correct
68 Correct 458 ms 42916 KB Output is correct
69 Correct 1010 ms 84048 KB Output is correct
70 Correct 686 ms 78412 KB Output is correct
71 Correct 899 ms 77752 KB Output is correct
72 Correct 478 ms 43740 KB Output is correct
73 Correct 849 ms 75952 KB Output is correct
74 Correct 373 ms 49492 KB Output is correct
75 Correct 381 ms 48336 KB Output is correct
76 Correct 458 ms 53764 KB Output is correct
77 Correct 458 ms 54236 KB Output is correct
78 Correct 414 ms 50520 KB Output is correct
79 Correct 358 ms 47476 KB Output is correct
80 Correct 370 ms 45408 KB Output is correct
81 Correct 500 ms 59460 KB Output is correct
82 Correct 435 ms 49884 KB Output is correct
83 Correct 389 ms 50140 KB Output is correct
84 Correct 90 ms 12904 KB Output is correct
85 Correct 726 ms 77812 KB Output is correct
86 Correct 1146 ms 100556 KB Output is correct
87 Correct 101 ms 20840 KB Output is correct
88 Correct 137 ms 18536 KB Output is correct
89 Correct 103 ms 20712 KB Output is correct
90 Correct 29 ms 4220 KB Output is correct
91 Correct 6 ms 512 KB Output is correct
92 Correct 28 ms 3828 KB Output is correct
93 Correct 341 ms 34912 KB Output is correct
94 Correct 115 ms 15464 KB Output is correct
95 Correct 582 ms 71456 KB Output is correct
96 Correct 538 ms 67788 KB Output is correct
97 Correct 453 ms 56028 KB Output is correct
98 Correct 460 ms 61900 KB Output is correct
99 Correct 1259 ms 119388 KB Output is correct
100 Correct 838 ms 84044 KB Output is correct
101 Correct 990 ms 92744 KB Output is correct
102 Correct 446 ms 47316 KB Output is correct
103 Correct 384 ms 49104 KB Output is correct
104 Correct 377 ms 49232 KB Output is correct
105 Correct 462 ms 53968 KB Output is correct
106 Correct 408 ms 51580 KB Output is correct
107 Correct 492 ms 55184 KB Output is correct
108 Correct 47 ms 6892 KB Output is correct
109 Correct 701 ms 67404 KB Output is correct
110 Correct 702 ms 82764 KB Output is correct
111 Correct 656 ms 83592 KB Output is correct
112 Correct 483 ms 55940 KB Output is correct
113 Correct 443 ms 53884 KB Output is correct