Submission #592378

# Submission time Handle Problem Language Result Execution time Memory
592378 2022-07-09T06:33:47 Z 8e7 Sky Walking (IOI19_walk) C++17
100 / 100
1683 ms 243524 KB
//Challenge: Accepted
#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r)cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 100005
#define maxv 3000005
#define pii pair<ll, ll>
#define ff first
#define ss second
const ll inf = 1LL<<60;
vector<pii> adj[maxv];
ll dis[maxv];
int prv[maxn], px[maxn];
vector<int> lft[maxn], rht[maxn];
bool on[maxn];

struct segtree{
	int seg[4 * maxn];
	void init(int cur, int l, int r, vector<int> &h) {
		if (r <= l) return;
		if (r - l == 1) {
			seg[cur] = h[l];
			return;
		}
		int m = (l + r) / 2;
		init(cur*2, l, m, h), init(cur*2+1, m, r, h);
		seg[cur] = max(seg[cur*2], seg[cur*2+1]);
	}
	int search(int cur, int l, int r, int ql, int qr, int x, bool type) {
		if (r <= l || ql >= r || qr <= l || seg[cur] < x) return -1;
		if (r - l == 1) return l;
		int m = (l + r) / 2;
		if (type == 0) {
			int vl = search(cur*2, l, m, ql, qr, x, type);
			if (vl != -1) return vl;
			else return search(cur*2+1, m, r, ql, qr, x, type);
		} else {
			int vr = search(cur*2+1, m, r, ql, qr, x, type);
			if (vr != -1) return vr;
			else return search(cur*2, l, m, ql, qr, x, type);
		}
	}	
} tr;
long long min_distance(vector<int> X, vector<int> H, vector<int> L, vector<int> R, vector<int> Y, int st, int ed) {
	int n = X.size(), m = L.size();
	tr.init(1, 0, n, H);	

	set<pii> se;	
	for (int i = 0;i < m;i++) {
		int l = L[i], r = R[i], y = Y[i];
		lft[l].push_back(i), rht[r].push_back(i);
		if (l <= st && r >= st) {
			int vl = tr.search(1, 0, n, l, st+1, y, 1), vr = tr.search(1, 0, n, st+1, r+1, y, 0);
			if (vl != -1) lft[vl].push_back(i);	
			if (vr != -1) lft[vr].push_back(i);
		}
		if (l <= ed && r >= ed) {
			int vl = tr.search(1, 0, n, l, ed+1, y, 1), vr = tr.search(1, 0, n, ed+1, r+1, y, 0);
			if (vl != -1) lft[vl].push_back(i);	
			if (vr != -1) lft[vr].push_back(i);
		}
	}
	auto addedge = [&] (int u, int v, int w){
		adj[u].push_back({v, w});
		adj[v].push_back({u, w});
		//debug(u, v, w);
	};
	debug();
	int term = maxv - 1;
	{
		int cur = 1;
		for (int i = 0;i < n;i++) {
			vector<int> ev = lft[i];
			ev.insert(ev.end(), rht[i].begin(), rht[i].end());
			sort(ev.begin(), ev.end());
			ev.resize(int(unique(ev.begin(), ev.end()) - ev.begin()));
			//pary(ev.begin(), ev.end());
			for (int id:ev) {
				int y = Y[id], node = cur++;
				//debug("node", node, X[i], Y[id]);
				if (i == st && y <= H[i]) addedge(node, 0, y);
				if (i == ed && y <= H[i]) addedge(node, term, y);
				
				{
					auto it = se.upper_bound(make_pair(y, id));
					if (it != se.end() && it->ff <= H[i]) {
						addedge(cur, prv[it->ss], X[i] - px[it->ss]);
						prv[it->ss] = cur;
						px[it->ss] = X[i];
						//debug("node", cur, X[i], it->ff);
						addedge(node, cur++, abs(y - it->ff));	
					}
				}
				{
					auto it = se.lower_bound(make_pair(y, id));
					if (it != se.begin()) {
						it = prev(it);
						addedge(cur, prv[it->ss], X[i] - px[it->ss]);
						prv[it->ss] = cur;
						px[it->ss] = X[i];
						//debug("node", cur, X[i], it->ff);
						addedge(node, cur++, abs(y - it->ff));	
					}
				}

				if (on[id]) {
					addedge(node, prv[id], X[i] - px[id]);
				} else {
					se.insert(make_pair(y, id));
					on[id] = 1;
				}
				prv[id] = node;
				px[id] = X[i];
				 
			}
			for (int id:rht[i]) {
				se.erase(se.find(make_pair(Y[id], id)));
			}
		}
	}
	for (int i = 0;i < maxv;i++) dis[i] = inf;	
	dis[0] = 0;	
	priority_queue<pii, vector<pii>, greater<pii> > pq;
	pq.push({0, 0});
	while (pq.size()) {
		auto [d, cur] = pq.top();
		pq.pop();
		if (d != dis[cur]) continue;
		for (auto [v, w]:adj[cur]) {
			if (d + w < dis[v]) {
				dis[v] = d + w;
				pq.push({dis[v], v});
			}
		}
	}
	if (dis[term] == inf) return -1;
	return dis[term];
}

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:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
walk.cpp:80:2: note: in expansion of macro 'debug'
   80 |  debug();
      |  ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 49 ms 98848 KB Output is correct
2 Correct 46 ms 98836 KB Output is correct
3 Correct 45 ms 98832 KB Output is correct
4 Correct 49 ms 98844 KB Output is correct
5 Correct 48 ms 98916 KB Output is correct
6 Correct 44 ms 98892 KB Output is correct
7 Correct 53 ms 98912 KB Output is correct
8 Correct 47 ms 98924 KB Output is correct
9 Correct 50 ms 98928 KB Output is correct
10 Correct 55 ms 98984 KB Output is correct
11 Correct 45 ms 98908 KB Output is correct
12 Correct 44 ms 98968 KB Output is correct
13 Correct 44 ms 98884 KB Output is correct
14 Correct 49 ms 98892 KB Output is correct
15 Correct 57 ms 98828 KB Output is correct
16 Correct 58 ms 98904 KB Output is correct
17 Correct 49 ms 98932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 98844 KB Output is correct
2 Correct 44 ms 98872 KB Output is correct
3 Correct 391 ms 151300 KB Output is correct
4 Correct 323 ms 152752 KB Output is correct
5 Correct 217 ms 140096 KB Output is correct
6 Correct 320 ms 138224 KB Output is correct
7 Correct 217 ms 140308 KB Output is correct
8 Correct 404 ms 151836 KB Output is correct
9 Correct 260 ms 150732 KB Output is correct
10 Correct 364 ms 153704 KB Output is correct
11 Correct 317 ms 147964 KB Output is correct
12 Correct 196 ms 134604 KB Output is correct
13 Correct 367 ms 155200 KB Output is correct
14 Correct 275 ms 131332 KB Output is correct
15 Correct 332 ms 134464 KB Output is correct
16 Correct 231 ms 137400 KB Output is correct
17 Correct 225 ms 135448 KB Output is correct
18 Correct 768 ms 171388 KB Output is correct
19 Correct 51 ms 100680 KB Output is correct
20 Correct 144 ms 116348 KB Output is correct
21 Correct 222 ms 138368 KB Output is correct
22 Correct 182 ms 133812 KB Output is correct
23 Correct 367 ms 148008 KB Output is correct
24 Correct 250 ms 135936 KB Output is correct
25 Correct 191 ms 136160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 134 ms 122436 KB Output is correct
2 Correct 556 ms 152536 KB Output is correct
3 Correct 432 ms 153632 KB Output is correct
4 Correct 555 ms 160212 KB Output is correct
5 Correct 778 ms 163564 KB Output is correct
6 Correct 779 ms 164968 KB Output is correct
7 Correct 229 ms 132940 KB Output is correct
8 Correct 186 ms 134628 KB Output is correct
9 Correct 735 ms 168124 KB Output is correct
10 Correct 445 ms 157404 KB Output is correct
11 Correct 58 ms 100916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 134 ms 122436 KB Output is correct
2 Correct 556 ms 152536 KB Output is correct
3 Correct 432 ms 153632 KB Output is correct
4 Correct 555 ms 160212 KB Output is correct
5 Correct 778 ms 163564 KB Output is correct
6 Correct 779 ms 164968 KB Output is correct
7 Correct 229 ms 132940 KB Output is correct
8 Correct 186 ms 134628 KB Output is correct
9 Correct 735 ms 168124 KB Output is correct
10 Correct 445 ms 157404 KB Output is correct
11 Correct 58 ms 100916 KB Output is correct
12 Correct 453 ms 153580 KB Output is correct
13 Correct 340 ms 159812 KB Output is correct
14 Correct 728 ms 163536 KB Output is correct
15 Correct 407 ms 144860 KB Output is correct
16 Correct 450 ms 159380 KB Output is correct
17 Correct 467 ms 160480 KB Output is correct
18 Correct 384 ms 144964 KB Output is correct
19 Correct 464 ms 159320 KB Output is correct
20 Correct 255 ms 132800 KB Output is correct
21 Correct 75 ms 104068 KB Output is correct
22 Correct 265 ms 148388 KB Output is correct
23 Correct 225 ms 147824 KB Output is correct
24 Correct 216 ms 139212 KB Output is correct
25 Correct 266 ms 144960 KB Output is correct
26 Correct 186 ms 130112 KB Output is correct
27 Correct 750 ms 164072 KB Output is correct
28 Correct 282 ms 159172 KB Output is correct
29 Correct 685 ms 165004 KB Output is correct
30 Correct 239 ms 132748 KB Output is correct
31 Correct 696 ms 168004 KB Output is correct
32 Correct 323 ms 149336 KB Output is correct
33 Correct 278 ms 143888 KB Output is correct
34 Correct 347 ms 150376 KB Output is correct
35 Correct 286 ms 144760 KB Output is correct
36 Correct 217 ms 139732 KB Output is correct
37 Correct 182 ms 138268 KB Output is correct
38 Correct 180 ms 133636 KB Output is correct
39 Correct 371 ms 147836 KB Output is correct
40 Correct 200 ms 135884 KB Output is correct
41 Correct 196 ms 136204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 98848 KB Output is correct
2 Correct 46 ms 98836 KB Output is correct
3 Correct 45 ms 98832 KB Output is correct
4 Correct 49 ms 98844 KB Output is correct
5 Correct 48 ms 98916 KB Output is correct
6 Correct 44 ms 98892 KB Output is correct
7 Correct 53 ms 98912 KB Output is correct
8 Correct 47 ms 98924 KB Output is correct
9 Correct 50 ms 98928 KB Output is correct
10 Correct 55 ms 98984 KB Output is correct
11 Correct 45 ms 98908 KB Output is correct
12 Correct 44 ms 98968 KB Output is correct
13 Correct 44 ms 98884 KB Output is correct
14 Correct 49 ms 98892 KB Output is correct
15 Correct 57 ms 98828 KB Output is correct
16 Correct 58 ms 98904 KB Output is correct
17 Correct 49 ms 98932 KB Output is correct
18 Correct 45 ms 98844 KB Output is correct
19 Correct 44 ms 98872 KB Output is correct
20 Correct 391 ms 151300 KB Output is correct
21 Correct 323 ms 152752 KB Output is correct
22 Correct 217 ms 140096 KB Output is correct
23 Correct 320 ms 138224 KB Output is correct
24 Correct 217 ms 140308 KB Output is correct
25 Correct 404 ms 151836 KB Output is correct
26 Correct 260 ms 150732 KB Output is correct
27 Correct 364 ms 153704 KB Output is correct
28 Correct 317 ms 147964 KB Output is correct
29 Correct 196 ms 134604 KB Output is correct
30 Correct 367 ms 155200 KB Output is correct
31 Correct 275 ms 131332 KB Output is correct
32 Correct 332 ms 134464 KB Output is correct
33 Correct 231 ms 137400 KB Output is correct
34 Correct 225 ms 135448 KB Output is correct
35 Correct 768 ms 171388 KB Output is correct
36 Correct 51 ms 100680 KB Output is correct
37 Correct 144 ms 116348 KB Output is correct
38 Correct 222 ms 138368 KB Output is correct
39 Correct 182 ms 133812 KB Output is correct
40 Correct 367 ms 148008 KB Output is correct
41 Correct 250 ms 135936 KB Output is correct
42 Correct 191 ms 136160 KB Output is correct
43 Correct 134 ms 122436 KB Output is correct
44 Correct 556 ms 152536 KB Output is correct
45 Correct 432 ms 153632 KB Output is correct
46 Correct 555 ms 160212 KB Output is correct
47 Correct 778 ms 163564 KB Output is correct
48 Correct 779 ms 164968 KB Output is correct
49 Correct 229 ms 132940 KB Output is correct
50 Correct 186 ms 134628 KB Output is correct
51 Correct 735 ms 168124 KB Output is correct
52 Correct 445 ms 157404 KB Output is correct
53 Correct 58 ms 100916 KB Output is correct
54 Correct 453 ms 153580 KB Output is correct
55 Correct 340 ms 159812 KB Output is correct
56 Correct 728 ms 163536 KB Output is correct
57 Correct 407 ms 144860 KB Output is correct
58 Correct 450 ms 159380 KB Output is correct
59 Correct 467 ms 160480 KB Output is correct
60 Correct 384 ms 144964 KB Output is correct
61 Correct 464 ms 159320 KB Output is correct
62 Correct 255 ms 132800 KB Output is correct
63 Correct 75 ms 104068 KB Output is correct
64 Correct 265 ms 148388 KB Output is correct
65 Correct 225 ms 147824 KB Output is correct
66 Correct 216 ms 139212 KB Output is correct
67 Correct 266 ms 144960 KB Output is correct
68 Correct 186 ms 130112 KB Output is correct
69 Correct 750 ms 164072 KB Output is correct
70 Correct 282 ms 159172 KB Output is correct
71 Correct 685 ms 165004 KB Output is correct
72 Correct 239 ms 132748 KB Output is correct
73 Correct 696 ms 168004 KB Output is correct
74 Correct 323 ms 149336 KB Output is correct
75 Correct 278 ms 143888 KB Output is correct
76 Correct 347 ms 150376 KB Output is correct
77 Correct 286 ms 144760 KB Output is correct
78 Correct 217 ms 139732 KB Output is correct
79 Correct 182 ms 138268 KB Output is correct
80 Correct 180 ms 133636 KB Output is correct
81 Correct 371 ms 147836 KB Output is correct
82 Correct 200 ms 135884 KB Output is correct
83 Correct 196 ms 136204 KB Output is correct
84 Correct 138 ms 117300 KB Output is correct
85 Correct 512 ms 159424 KB Output is correct
86 Correct 1191 ms 207236 KB Output is correct
87 Correct 87 ms 107136 KB Output is correct
88 Correct 88 ms 109132 KB Output is correct
89 Correct 83 ms 107164 KB Output is correct
90 Correct 62 ms 104148 KB Output is correct
91 Correct 46 ms 99012 KB Output is correct
92 Correct 70 ms 101520 KB Output is correct
93 Correct 207 ms 125004 KB Output is correct
94 Correct 72 ms 104736 KB Output is correct
95 Correct 267 ms 150728 KB Output is correct
96 Correct 239 ms 148252 KB Output is correct
97 Correct 223 ms 139600 KB Output is correct
98 Correct 226 ms 144860 KB Output is correct
99 Correct 1683 ms 243524 KB Output is correct
100 Correct 456 ms 160236 KB Output is correct
101 Correct 997 ms 196960 KB Output is correct
102 Correct 244 ms 133404 KB Output is correct
103 Correct 300 ms 146788 KB Output is correct
104 Correct 271 ms 143712 KB Output is correct
105 Correct 367 ms 149340 KB Output is correct
106 Correct 317 ms 145084 KB Output is correct
107 Correct 319 ms 139452 KB Output is correct
108 Correct 70 ms 103808 KB Output is correct
109 Correct 509 ms 148848 KB Output is correct
110 Correct 319 ms 160332 KB Output is correct
111 Correct 303 ms 160380 KB Output is correct
112 Correct 277 ms 145904 KB Output is correct
113 Correct 260 ms 141708 KB Output is correct