Submission #251492

# Submission time Handle Problem Language Result Execution time Memory
251492 2020-07-21T13:26:12 Z nvmdava Sky Walking (IOI19_walk) C++17
100 / 100
2063 ms 156668 KB
#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ll long long
#define ss second
const int N = 1000'005;

vector<int> br[N];
vector<pair<int, int> > adj[N];
int mx[N];
int n;

void build(vector<int>& h, int id, int l, int r){
	if(l == r){
		mx[id] = h[l];
		return;
	}
	int m = (l + r) >> 1;
	build(h, id << 1, l, m);
	build(h, id << 1 | 1, m + 1, r);
	mx[id] = max(mx[id << 1], mx[id << 1 | 1]);
}

int find_right(int id, int l, int r, int L, int R, int x){
	if(L > r || R < l) return -1;
	if(L <= l && r <= R){
		if(mx[id] < x) return -1;
		if(l == r) return r;
		int m = (l + r) >> 1;
		if(mx[id << 1 | 1] >= x) return find_right(id << 1 | 1, m + 1, r, L, R, x);
		return find_right(id << 1, l, m, L, R, x);
	}
	int m = (l + r) >> 1;
	int s = find_right(id << 1 | 1, m + 1, r, L, R, x);
	if(s != -1) return s;
	return find_right(id << 1, l, m, L, R, x);
}

int find_left(int id, int l, int r, int L, int R, int x){
	if(L > r || R < l) return -1;
	if(L <= l && r <= R){
		if(mx[id] < x) return -1;
		if(l == r) return r;
		int m = (l + r) >> 1;
		if(mx[id << 1] >= x) return find_left(id << 1, l, m, L, R, x);
		return find_left(id << 1 | 1, m + 1, r, L, R, x);
	}
	int m = (l + r) >> 1;
	int s = find_left(id << 1, l, m, L, R, x);
	if(s != -1) return s;
	return find_left(id << 1 | 1, m + 1, r, L, R, x);
}

vector<pair<int, int> > pos;
vector<pair<int, int> > sweep;

multiset<int> in;
map<pair<int, int>, int> id;
map<int, pair<int, int> > last;

void add(int v, int u, int d){
	adj[v].push_back({u, d});
	adj[u].push_back({v, d});
}

ll dis[N];
priority_queue<pair<ll, int> > pq;

long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {

	n = h.size();
	build(h, 1, 0, n - 1);

	for(int i = 0; i < l.size(); ++i){
		sweep.push_back({l[i], y[i]});
		sweep.push_back({l[i] + 1, y[i]});
		sweep.push_back({r[i] + 1, -y[i]});
		sweep.push_back({r[i] + 1, -y[i]});
		pos.push_back({l[i], -y[i]});
		pos.push_back({r[i], -y[i]});
		if(l[i] < s && s < r[i]){
			pos.push_back({find_left(1, 0, n - 1, s, r[i], y[i]), -y[i]});
			pos.push_back({find_right(1, 0, n - 1, l[i], s, y[i]), -y[i]});
		}
		if(l[i] < g && g < r[i]){
			pos.push_back({find_left(1, 0, n - 1, g, r[i], y[i]), -y[i]});
			pos.push_back({find_right(1, 0, n - 1, l[i], g, y[i]), -y[i]});
		}
	}

	sort(pos.begin(), pos.end());

	sort(sweep.begin(), sweep.end());

	int i = 0;
	in.insert(0);

	int tmp = 0;
	for(auto& w : pos){
		while(i < sweep.size() && sweep[i].ff <= w.ff){
			if(sweep[i].ss > 0) in.insert(-sweep[i].ss);
			else in.erase(in.find(sweep[i].ss));
			++i;
		}

		int x1 = w.ff, y1 = -w.ss, id1;
		int x2 = w.ff, y2 = -(*in.upper_bound(w.ss)), id2;

		if((id1 = id[{x1, y1}]) == 0)
			id1 = id[{x1, y1}] = ++tmp;
		if((id2 = id[{x2, y2}]) == 0)
			id2 = id[{x2, y2}] = ++tmp;
		add(id1, id2, y1 - y2);
		if(in.count(-y1) >= 2)
			add(id1, last[y1].ff, x[x1] - x[last[y1].ss]);
		if(in.count(-y2) >= 2)
			add(id2, last[y2].ff, x[x2] - x[last[y2].ss]);

		last[y2] = {id2, x2};
		last[y1] = {id1, x1};
	}

	int b = id[{g, 0}];
	for(int i = 0; i < N; ++i)
		dis[i] = 2'000'000'000'000'000'000;
	dis[b] = 0;
	pq.push({0, b});

	while(!pq.empty()){
		b = pq.top().ss;
		if(pq.top().ff != -dis[b]){
			pq.pop();
			continue;
		}
		pq.pop();

		for(auto& w : adj[b]){
			if(dis[w.ff] > dis[b] + w.ss){
				dis[w.ff] = dis[b] + w.ss;
				pq.push({-dis[w.ff], w.ff});
			}
		}
	}

	b = id[{s, 0}];

	return dis[b] == 0 || dis[b] == 2'000'000'000'000'000'000 ? -1 : dis[b];
}

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:75:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < l.size(); ++i){
                 ~~^~~~~~~~~~
walk.cpp:101:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(i < sweep.size() && sweep[i].ff <= w.ff){
         ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 35 ms 55160 KB Output is correct
2 Correct 30 ms 55168 KB Output is correct
3 Correct 35 ms 55160 KB Output is correct
4 Correct 32 ms 55168 KB Output is correct
5 Correct 35 ms 55168 KB Output is correct
6 Correct 36 ms 55160 KB Output is correct
7 Correct 39 ms 55160 KB Output is correct
8 Correct 35 ms 55164 KB Output is correct
9 Correct 32 ms 55160 KB Output is correct
10 Correct 35 ms 55160 KB Output is correct
11 Correct 32 ms 55168 KB Output is correct
12 Correct 30 ms 55168 KB Output is correct
13 Correct 37 ms 55160 KB Output is correct
14 Correct 33 ms 55168 KB Output is correct
15 Correct 37 ms 55160 KB Output is correct
16 Correct 30 ms 55168 KB Output is correct
17 Correct 33 ms 55160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 55092 KB Output is correct
2 Correct 33 ms 55160 KB Output is correct
3 Correct 753 ms 106268 KB Output is correct
4 Correct 676 ms 110404 KB Output is correct
5 Correct 459 ms 98208 KB Output is correct
6 Correct 504 ms 99412 KB Output is correct
7 Correct 462 ms 98264 KB Output is correct
8 Correct 778 ms 107604 KB Output is correct
9 Correct 691 ms 108536 KB Output is correct
10 Correct 683 ms 110416 KB Output is correct
11 Correct 703 ms 104136 KB Output is correct
12 Correct 606 ms 104416 KB Output is correct
13 Correct 719 ms 111516 KB Output is correct
14 Correct 717 ms 108624 KB Output is correct
15 Correct 622 ms 104260 KB Output is correct
16 Correct 388 ms 98668 KB Output is correct
17 Correct 371 ms 96208 KB Output is correct
18 Correct 587 ms 114272 KB Output is correct
19 Correct 54 ms 58056 KB Output is correct
20 Correct 325 ms 85080 KB Output is correct
21 Correct 330 ms 96608 KB Output is correct
22 Correct 363 ms 98116 KB Output is correct
23 Correct 645 ms 108284 KB Output is correct
24 Correct 367 ms 98272 KB Output is correct
25 Correct 374 ms 96260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 70376 KB Output is correct
2 Correct 986 ms 110492 KB Output is correct
3 Correct 1195 ms 111892 KB Output is correct
4 Correct 1187 ms 114248 KB Output is correct
5 Correct 1544 ms 117916 KB Output is correct
6 Correct 1384 ms 116940 KB Output is correct
7 Correct 426 ms 87384 KB Output is correct
8 Correct 519 ms 104544 KB Output is correct
9 Correct 1178 ms 118500 KB Output is correct
10 Correct 502 ms 102400 KB Output is correct
11 Correct 47 ms 57208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 70376 KB Output is correct
2 Correct 986 ms 110492 KB Output is correct
3 Correct 1195 ms 111892 KB Output is correct
4 Correct 1187 ms 114248 KB Output is correct
5 Correct 1544 ms 117916 KB Output is correct
6 Correct 1384 ms 116940 KB Output is correct
7 Correct 426 ms 87384 KB Output is correct
8 Correct 519 ms 104544 KB Output is correct
9 Correct 1178 ms 118500 KB Output is correct
10 Correct 502 ms 102400 KB Output is correct
11 Correct 47 ms 57208 KB Output is correct
12 Correct 1101 ms 111584 KB Output is correct
13 Correct 1275 ms 114144 KB Output is correct
14 Correct 1307 ms 118204 KB Output is correct
15 Correct 753 ms 109148 KB Output is correct
16 Correct 697 ms 105060 KB Output is correct
17 Correct 877 ms 109920 KB Output is correct
18 Correct 905 ms 109024 KB Output is correct
19 Correct 798 ms 104420 KB Output is correct
20 Correct 478 ms 87144 KB Output is correct
21 Correct 82 ms 60280 KB Output is correct
22 Correct 590 ms 103156 KB Output is correct
23 Correct 615 ms 103392 KB Output is correct
24 Correct 556 ms 100192 KB Output is correct
25 Correct 524 ms 101856 KB Output is correct
26 Correct 507 ms 100196 KB Output is correct
27 Correct 1391 ms 116320 KB Output is correct
28 Correct 922 ms 113504 KB Output is correct
29 Correct 1211 ms 116268 KB Output is correct
30 Correct 432 ms 87016 KB Output is correct
31 Correct 1311 ms 117856 KB Output is correct
32 Correct 430 ms 97120 KB Output is correct
33 Correct 465 ms 98212 KB Output is correct
34 Correct 506 ms 102500 KB Output is correct
35 Correct 490 ms 100960 KB Output is correct
36 Correct 416 ms 98144 KB Output is correct
37 Correct 323 ms 96096 KB Output is correct
38 Correct 346 ms 97632 KB Output is correct
39 Correct 628 ms 107880 KB Output is correct
40 Correct 356 ms 97632 KB Output is correct
41 Correct 341 ms 95820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 55160 KB Output is correct
2 Correct 30 ms 55168 KB Output is correct
3 Correct 35 ms 55160 KB Output is correct
4 Correct 32 ms 55168 KB Output is correct
5 Correct 35 ms 55168 KB Output is correct
6 Correct 36 ms 55160 KB Output is correct
7 Correct 39 ms 55160 KB Output is correct
8 Correct 35 ms 55164 KB Output is correct
9 Correct 32 ms 55160 KB Output is correct
10 Correct 35 ms 55160 KB Output is correct
11 Correct 32 ms 55168 KB Output is correct
12 Correct 30 ms 55168 KB Output is correct
13 Correct 37 ms 55160 KB Output is correct
14 Correct 33 ms 55168 KB Output is correct
15 Correct 37 ms 55160 KB Output is correct
16 Correct 30 ms 55168 KB Output is correct
17 Correct 33 ms 55160 KB Output is correct
18 Correct 32 ms 55092 KB Output is correct
19 Correct 33 ms 55160 KB Output is correct
20 Correct 753 ms 106268 KB Output is correct
21 Correct 676 ms 110404 KB Output is correct
22 Correct 459 ms 98208 KB Output is correct
23 Correct 504 ms 99412 KB Output is correct
24 Correct 462 ms 98264 KB Output is correct
25 Correct 778 ms 107604 KB Output is correct
26 Correct 691 ms 108536 KB Output is correct
27 Correct 683 ms 110416 KB Output is correct
28 Correct 703 ms 104136 KB Output is correct
29 Correct 606 ms 104416 KB Output is correct
30 Correct 719 ms 111516 KB Output is correct
31 Correct 717 ms 108624 KB Output is correct
32 Correct 622 ms 104260 KB Output is correct
33 Correct 388 ms 98668 KB Output is correct
34 Correct 371 ms 96208 KB Output is correct
35 Correct 587 ms 114272 KB Output is correct
36 Correct 54 ms 58056 KB Output is correct
37 Correct 325 ms 85080 KB Output is correct
38 Correct 330 ms 96608 KB Output is correct
39 Correct 363 ms 98116 KB Output is correct
40 Correct 645 ms 108284 KB Output is correct
41 Correct 367 ms 98272 KB Output is correct
42 Correct 374 ms 96260 KB Output is correct
43 Correct 163 ms 70376 KB Output is correct
44 Correct 986 ms 110492 KB Output is correct
45 Correct 1195 ms 111892 KB Output is correct
46 Correct 1187 ms 114248 KB Output is correct
47 Correct 1544 ms 117916 KB Output is correct
48 Correct 1384 ms 116940 KB Output is correct
49 Correct 426 ms 87384 KB Output is correct
50 Correct 519 ms 104544 KB Output is correct
51 Correct 1178 ms 118500 KB Output is correct
52 Correct 502 ms 102400 KB Output is correct
53 Correct 47 ms 57208 KB Output is correct
54 Correct 1101 ms 111584 KB Output is correct
55 Correct 1275 ms 114144 KB Output is correct
56 Correct 1307 ms 118204 KB Output is correct
57 Correct 753 ms 109148 KB Output is correct
58 Correct 697 ms 105060 KB Output is correct
59 Correct 877 ms 109920 KB Output is correct
60 Correct 905 ms 109024 KB Output is correct
61 Correct 798 ms 104420 KB Output is correct
62 Correct 478 ms 87144 KB Output is correct
63 Correct 82 ms 60280 KB Output is correct
64 Correct 590 ms 103156 KB Output is correct
65 Correct 615 ms 103392 KB Output is correct
66 Correct 556 ms 100192 KB Output is correct
67 Correct 524 ms 101856 KB Output is correct
68 Correct 507 ms 100196 KB Output is correct
69 Correct 1391 ms 116320 KB Output is correct
70 Correct 922 ms 113504 KB Output is correct
71 Correct 1211 ms 116268 KB Output is correct
72 Correct 432 ms 87016 KB Output is correct
73 Correct 1311 ms 117856 KB Output is correct
74 Correct 430 ms 97120 KB Output is correct
75 Correct 465 ms 98212 KB Output is correct
76 Correct 506 ms 102500 KB Output is correct
77 Correct 490 ms 100960 KB Output is correct
78 Correct 416 ms 98144 KB Output is correct
79 Correct 323 ms 96096 KB Output is correct
80 Correct 346 ms 97632 KB Output is correct
81 Correct 628 ms 107880 KB Output is correct
82 Correct 356 ms 97632 KB Output is correct
83 Correct 341 ms 95820 KB Output is correct
84 Correct 135 ms 67316 KB Output is correct
85 Correct 1314 ms 113176 KB Output is correct
86 Correct 1751 ms 137628 KB Output is correct
87 Correct 108 ms 64716 KB Output is correct
88 Correct 104 ms 64072 KB Output is correct
89 Correct 99 ms 64712 KB Output is correct
90 Correct 69 ms 59056 KB Output is correct
91 Correct 33 ms 55296 KB Output is correct
92 Correct 55 ms 57592 KB Output is correct
93 Correct 328 ms 79332 KB Output is correct
94 Correct 75 ms 60536 KB Output is correct
95 Correct 601 ms 107348 KB Output is correct
96 Correct 590 ms 106040 KB Output is correct
97 Correct 583 ms 105552 KB Output is correct
98 Correct 510 ms 103508 KB Output is correct
99 Correct 2063 ms 156668 KB Output is correct
100 Correct 980 ms 116184 KB Output is correct
101 Correct 1396 ms 132668 KB Output is correct
102 Correct 436 ms 88672 KB Output is correct
103 Correct 433 ms 98400 KB Output is correct
104 Correct 442 ms 99796 KB Output is correct
105 Correct 519 ms 102620 KB Output is correct
106 Correct 539 ms 104020 KB Output is correct
107 Correct 601 ms 106324 KB Output is correct
108 Correct 84 ms 60232 KB Output is correct
109 Correct 1176 ms 105480 KB Output is correct
110 Correct 781 ms 116564 KB Output is correct
111 Correct 837 ms 116820 KB Output is correct
112 Correct 464 ms 101132 KB Output is correct
113 Correct 455 ms 100052 KB Output is correct