답안 #850035

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
850035 2023-09-15T16:27:39 Z IBory Bitaro's travel (JOI23_travel) C++17
100 / 100
916 ms 18520 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
typedef long long ll;
using namespace std;

const int SZ = 1 << 18;
ll X[SZ], LR[SZ], RL[SZ], pre[SZ];
// LR[n]: L 방향으로 가는데, 만약 출발 위치가 LR[n] 이하이면 반드시 오른쪽으로 꺾어야 한다

pii Merge(pii& a, pii& b) {
	return { min(a.first, b.first), max(a.second, b.second) };
}

struct Seg {
	pii T[SZ << 1];
	void Build() {
		for (int i = SZ; i > 0; --i) {
			T[i] = Merge(T[i * 2], T[i * 2 + 1]);
		}
	}
	pii Query(int L, int R) {
		pii ret = { 2e9, -2e9 };
		for (L += SZ - 1, R += SZ - 1; L <= R; L >>= 1, R >>= 1) {
			if (L & 1) ret = Merge(ret, T[L++]);
			if (!(R & 1)) ret = Merge(ret, T[R--]);
		}
		return ret;
	}
} T;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int N, Q;
	cin >> N;
	X[0] = -1E18;
	X[N + 1] = 1E18;
	for (int i = 1; i <= N; ++i) cin >> X[i];
	for (int i = 1; i <= N; ++i) {
		if (1 < i) {
			ll d = X[i] - X[i - 1];
			int L = i - 1, R = N + 1;
			while (L + 1 < R) {
				int mid = (L + R) >> 1;
				(X[mid] - X[i] < d ? L : R) = mid;
			}
			LR[i] = L - 1;
		}
		else LR[i] = 1e9;
		if (i < N) {
			ll d = X[i + 1] - X[i];
			int L = 0, R = i + 1;
			while (L + 1 < R) {
				int mid = (L + R) >> 1;
				(X[i] - X[mid] <= d ? R : L) = mid;
			}
			RL[i] = R + 1;
		}
		else RL[i] = -1e9;
		T.T[SZ + i - 1] = { RL[i], LR[i] };
	}
	T.Build();

	for (int i = 1; i <= N; ++i) {
		int L = i, R = i, cur = i, left = -1;
		if (i == N || (i != 1 && X[i] - X[i - 1] <= X[i + 1] - X[i])) left = 1;
		else left = 0;
		while (L != 1 && R != N) {
			if (left) {
				int l = 1, r = cur;
				while (l + 1 < r) {
					int mid = (l + r) >> 1;
					(T.Query(mid, cur - 1).second >= R ? l : r) = mid;
				}
				L = l;
				pre[i] += X[cur] - X[L];
				cur = L;
			}
			else {
				int l = cur, r = N;
				while (l + 1 < r) {
					int mid = (l + r) >> 1;
					(T.Query(cur + 1, mid).first <= L ? r : l) = mid;
				}
				R = r;
				pre[i] += X[R] - X[cur];
				cur = R;
			}
			left ^= 1;
		}
		pre[i] += X[N] - X[1];
	}

	cin >> Q;
	while (Q--) {
		ll P;
		cin >> P;
		auto it = lower_bound(X + 1, X + N + 1, P) - X;
		ll ans = 0, DL = (1 < it ? P - X[it - 1] : 2E9), DR = (it <= N ? X[it] - P : 2E9);
		if (DL <= DR) ans = DL + pre[it - 1];
		else ans = DR + pre[it];
		cout << ans << '\n';
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 4 ms 8796 KB Output is correct
3 Correct 4 ms 8864 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 3 ms 8796 KB Output is correct
7 Correct 3 ms 8668 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 1 ms 8656 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 1 ms 8796 KB Output is correct
14 Correct 3 ms 8672 KB Output is correct
15 Correct 3 ms 8884 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 4 ms 8796 KB Output is correct
3 Correct 4 ms 8864 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 3 ms 8796 KB Output is correct
7 Correct 3 ms 8668 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 1 ms 8656 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 1 ms 8796 KB Output is correct
14 Correct 3 ms 8672 KB Output is correct
15 Correct 3 ms 8884 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 470 ms 14196 KB Output is correct
18 Correct 482 ms 14196 KB Output is correct
19 Correct 500 ms 14420 KB Output is correct
20 Correct 487 ms 14196 KB Output is correct
21 Correct 523 ms 14164 KB Output is correct
22 Correct 530 ms 14192 KB Output is correct
23 Correct 511 ms 14208 KB Output is correct
24 Correct 347 ms 14196 KB Output is correct
25 Correct 338 ms 14168 KB Output is correct
26 Correct 333 ms 14164 KB Output is correct
27 Correct 337 ms 14164 KB Output is correct
28 Correct 338 ms 14196 KB Output is correct
29 Correct 890 ms 14144 KB Output is correct
30 Correct 871 ms 14144 KB Output is correct
31 Correct 873 ms 14148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 323 ms 13552 KB Output is correct
8 Correct 316 ms 13628 KB Output is correct
9 Correct 316 ms 16212 KB Output is correct
10 Correct 322 ms 16216 KB Output is correct
11 Correct 324 ms 16100 KB Output is correct
12 Correct 321 ms 16216 KB Output is correct
13 Correct 35 ms 12624 KB Output is correct
14 Correct 23 ms 9816 KB Output is correct
15 Correct 371 ms 17064 KB Output is correct
16 Correct 357 ms 17108 KB Output is correct
17 Correct 370 ms 17116 KB Output is correct
18 Correct 36 ms 12628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 4 ms 8796 KB Output is correct
3 Correct 4 ms 8864 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 3 ms 8796 KB Output is correct
6 Correct 3 ms 8796 KB Output is correct
7 Correct 3 ms 8668 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 1 ms 8656 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 1 ms 8796 KB Output is correct
14 Correct 3 ms 8672 KB Output is correct
15 Correct 3 ms 8884 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 470 ms 14196 KB Output is correct
18 Correct 482 ms 14196 KB Output is correct
19 Correct 500 ms 14420 KB Output is correct
20 Correct 487 ms 14196 KB Output is correct
21 Correct 523 ms 14164 KB Output is correct
22 Correct 530 ms 14192 KB Output is correct
23 Correct 511 ms 14208 KB Output is correct
24 Correct 347 ms 14196 KB Output is correct
25 Correct 338 ms 14168 KB Output is correct
26 Correct 333 ms 14164 KB Output is correct
27 Correct 337 ms 14164 KB Output is correct
28 Correct 338 ms 14196 KB Output is correct
29 Correct 890 ms 14144 KB Output is correct
30 Correct 871 ms 14144 KB Output is correct
31 Correct 873 ms 14148 KB Output is correct
32 Correct 2 ms 8796 KB Output is correct
33 Correct 2 ms 8796 KB Output is correct
34 Correct 2 ms 8796 KB Output is correct
35 Correct 2 ms 8792 KB Output is correct
36 Correct 2 ms 8792 KB Output is correct
37 Correct 2 ms 8796 KB Output is correct
38 Correct 323 ms 13552 KB Output is correct
39 Correct 316 ms 13628 KB Output is correct
40 Correct 316 ms 16212 KB Output is correct
41 Correct 322 ms 16216 KB Output is correct
42 Correct 324 ms 16100 KB Output is correct
43 Correct 321 ms 16216 KB Output is correct
44 Correct 35 ms 12624 KB Output is correct
45 Correct 23 ms 9816 KB Output is correct
46 Correct 371 ms 17064 KB Output is correct
47 Correct 357 ms 17108 KB Output is correct
48 Correct 370 ms 17116 KB Output is correct
49 Correct 36 ms 12628 KB Output is correct
50 Correct 549 ms 18264 KB Output is correct
51 Correct 570 ms 18512 KB Output is correct
52 Correct 584 ms 18256 KB Output is correct
53 Correct 398 ms 18260 KB Output is correct
54 Correct 410 ms 18520 KB Output is correct
55 Correct 395 ms 18256 KB Output is correct
56 Correct 913 ms 18260 KB Output is correct
57 Correct 916 ms 18372 KB Output is correct
58 Correct 916 ms 18388 KB Output is correct