Submission #1064786

# Submission time Handle Problem Language Result Execution time Memory
1064786 2024-08-18T17:44:27 Z GusterGoose27 Bitaro's travel (JOI23_travel) C++17
100 / 100
139 ms 37028 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 2e5+5;
const int B = 18;
const int inf = 1e9+5;
int n;
vector<int> arr;
int ddiff[2][MAXN][B];
ll ans[MAXN];

ll sim(int l, int r, bool side, int b_prev = B) {
	if (r >= n) {
		assert(!side);
		return arr[l]-arr[0];
	}
	if (l < 0) {
		assert(side);
		return arr[n-1]-arr[r];
	}
	if (side) {
		for (int i = b_prev-1; i >= 0; i--) {
			int nxt = min(n-1, r+(1<<i));
			if (ddiff[0][r][i] < -arr[l]) return sim(l, nxt, 1, i)+(arr[nxt]-arr[r]);
		}
		return sim(l, r+1, 0)+arr[r]-arr[l];
	}
	else {
		for (int i = b_prev-1; i >= 0; i--) {
			int nxt = max(0, l-(1<<i));
			if (ddiff[1][l][i] <= arr[r]) return sim(nxt, r, 0, i)+(arr[l]-arr[nxt]);
		}
		return sim(l-1, r, 1)+arr[r]-arr[l];
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n;
	arr.resize(n);
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}
	for (int i = 0; i < n-1; i++) {
		ddiff[0][i][0] = arr[i+1]-2*arr[i];
		ddiff[1][i+1][0] = 2*arr[i+1]-arr[i];
	}
	ddiff[0][n-1][0] = inf;
	ddiff[1][0][0] = inf;
	for (int b = 1; b < B; b++) {
		for (int i = 0; i < n; i++) {
			if (i+(1<<(b-1)) >= n) ddiff[0][i][b] = ddiff[0][i][b-1];
			else ddiff[0][i][b] = max(ddiff[0][i][b-1], ddiff[0][i+(1<<(b-1))][b-1]);
			if (i-(1<<(b-1)) < 0) ddiff[1][i][b] = ddiff[1][i][b-1];
			else ddiff[1][i][b] = max(ddiff[1][i][b-1], ddiff[1][i-(1<<(b-1))][b-1]);
		}
	}
	sim(4,5,0);
	for (int i = 0; i < n; i++) {
		ans[i] = sim(i, i+1, 0);
	}
	int q; cin >> q;
	for (int i = 0; i < q; i++) {
		int x; cin >> x;
		if (x >= arr[n-1]) {
			cout << (x-arr[n-1]+ans[n-1]) << '\n';
		}
		else {
			int nxt = upper_bound(arr.begin(), arr.end(), x)-arr.begin();
			if (nxt == 0) cout << (arr[0]-x + ans[0]) << '\n';
			else cout << (x-arr[nxt-1] <= arr[nxt]-x ? x-arr[nxt-1]+ans[nxt-1] : arr[nxt]-x+ans[nxt]) << '\n';
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 464 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 792 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 464 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 792 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 77 ms 32896 KB Output is correct
18 Correct 74 ms 32904 KB Output is correct
19 Correct 79 ms 32724 KB Output is correct
20 Correct 89 ms 32984 KB Output is correct
21 Correct 77 ms 32852 KB Output is correct
22 Correct 74 ms 32752 KB Output is correct
23 Correct 78 ms 32852 KB Output is correct
24 Correct 68 ms 32744 KB Output is correct
25 Correct 66 ms 32736 KB Output is correct
26 Correct 57 ms 32756 KB Output is correct
27 Correct 79 ms 32884 KB Output is correct
28 Correct 56 ms 32756 KB Output is correct
29 Correct 102 ms 32700 KB Output is correct
30 Correct 112 ms 32652 KB Output is correct
31 Correct 102 ms 32852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 93 ms 33360 KB Output is correct
8 Correct 113 ms 34896 KB Output is correct
9 Correct 98 ms 34896 KB Output is correct
10 Correct 114 ms 34744 KB Output is correct
11 Correct 93 ms 34940 KB Output is correct
12 Correct 96 ms 34904 KB Output is correct
13 Correct 27 ms 8276 KB Output is correct
14 Correct 18 ms 3676 KB Output is correct
15 Correct 94 ms 35924 KB Output is correct
16 Correct 90 ms 35924 KB Output is correct
17 Correct 118 ms 35924 KB Output is correct
18 Correct 26 ms 6232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 464 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 792 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 77 ms 32896 KB Output is correct
18 Correct 74 ms 32904 KB Output is correct
19 Correct 79 ms 32724 KB Output is correct
20 Correct 89 ms 32984 KB Output is correct
21 Correct 77 ms 32852 KB Output is correct
22 Correct 74 ms 32752 KB Output is correct
23 Correct 78 ms 32852 KB Output is correct
24 Correct 68 ms 32744 KB Output is correct
25 Correct 66 ms 32736 KB Output is correct
26 Correct 57 ms 32756 KB Output is correct
27 Correct 79 ms 32884 KB Output is correct
28 Correct 56 ms 32756 KB Output is correct
29 Correct 102 ms 32700 KB Output is correct
30 Correct 112 ms 32652 KB Output is correct
31 Correct 102 ms 32852 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 0 ms 2396 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 93 ms 33360 KB Output is correct
39 Correct 113 ms 34896 KB Output is correct
40 Correct 98 ms 34896 KB Output is correct
41 Correct 114 ms 34744 KB Output is correct
42 Correct 93 ms 34940 KB Output is correct
43 Correct 96 ms 34904 KB Output is correct
44 Correct 27 ms 8276 KB Output is correct
45 Correct 18 ms 3676 KB Output is correct
46 Correct 94 ms 35924 KB Output is correct
47 Correct 90 ms 35924 KB Output is correct
48 Correct 118 ms 35924 KB Output is correct
49 Correct 26 ms 6232 KB Output is correct
50 Correct 129 ms 36948 KB Output is correct
51 Correct 122 ms 36992 KB Output is correct
52 Correct 128 ms 36972 KB Output is correct
53 Correct 107 ms 37028 KB Output is correct
54 Correct 109 ms 36948 KB Output is correct
55 Correct 111 ms 36820 KB Output is correct
56 Correct 134 ms 36692 KB Output is correct
57 Correct 139 ms 36828 KB Output is correct
58 Correct 136 ms 36760 KB Output is correct