이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10;
const ll INF = 1e18+10;
template <class T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
int n, m;
vector<pair<int, int>> adj[N];
ll dist[N];
void add_edge(int a, int b, int w) {
	adj[a].emplace_back(b, w);
	adj[b].emplace_back(a, w);
}
void dijkstra(int S) {
	fill(dist, dist + N, INF);
	min_pq<pair<ll, int>> pq;
	pq.push({dist[S] = 0, S});
	while (!pq.empty()) {
		auto [d_c, c] = pq.top(); pq.pop();
		if (dist[c] != d_c) continue;
		for (auto [nxt, w] : adj[c]) {
			if (dist[nxt] > dist[c] + w) {
				dist[nxt] = dist[c] + w;
				pq.push({dist[nxt], nxt});
			}
		}
	}
}
long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g) {
	n = sz(x), m = sz(l);
	assert(s == 0 && g == n-1);
	auto build = [&]() {
		vector<vector<int>> ev(n);
		for (int i = 0; i < m; i++) {
			ev[l[i]].push_back(i);
			ev[r[i]].push_back(i);
		}
		vector<bool> on(m);
		set<pair<int, int>> s;
		for (int i = 0; i < n; i++) {
			for (int x : ev[i]) if (!on[x]) {
				auto me = s.insert({y[x], x}).first;
				{
					auto it = next(me);
					if (it != s.end() && it->first <= h[i]) {
						add_edge(x, it->second, it->first - y[x]);
					}
				}
				{
					if (me != s.begin()) {
						auto it = prev(me);
						add_edge(x, it->second, y[x] - it->first);
					}
				}
			}
			for (int x : ev[i]) if (on[x]) {
				auto me = s.find({y[x], x});
				{
					auto it = next(me);
					if (it != s.end() && it->first <= h[i]) {
						add_edge(x, it->second, it->first - y[x]);
					}
				}
				{
					if (me != s.begin()) {
						auto it = prev(me);
						add_edge(x, it->second, y[x] - it->first);
					}
				}
				s.erase({y[x], x});
			}
			for (int x : ev[i]) on[x] = !on[x];
		}
	};
	build();
	int S = m;
	for (int i = 0; i < m; i++) if (l[i] == 0) {
		add_edge(S, i, y[i]);
	}
	dijkstra(S);
	ll ans = INF;
	for (int i = 0; i < m; i++) if (r[i] == n-1) {
		ans = min(ans, dist[i] + y[i]);
	}
	// cerr << x[n-1] - x[0] << ' ' << ans << endl;
	if (ans == INF) return -1;
	return ans + x[n-1] - x[0];
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |