Submission #918939

# Submission time Handle Problem Language Result Execution time Memory
918939 2024-01-30T21:39:48 Z OAleksa Bitaro's travel (JOI23_travel) C++14
100 / 100
476 ms 78780 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
const int N = 2e5 + 69;
const int inf = 1e15;
const int K = 18;
vector<vector<int>> levo(N, vector<int>(K, inf)), desno(N, vector<int>(K, -inf));
int x[N], n, q;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tt = 1;
  //cin >> tt;
  while (tt--) {
  	x[0] = -inf;
  	cin >> n;
  	for (int i = 1;i <= n;i++)
  		cin >> x[i];
  	x[n + 1] = inf;
  	for (int i = 1;i <= n;i++)
  		desno[i][0] = 2 * x[i] - x[i + 1];
  	for (int i = 1;i <= n;i++)
  		levo[i][0] = 2 * x[i] - x[i - 1];
  	for (int j = 1;j < K;j++) {
  		for (int i = 1;i + (1 << j) <= n + 1;i++) 
  			desno[i][j] = min(desno[i][j - 1], desno[i + (1 << (j - 1))][j - 1]);
  	}
  	for (int j = 1;j < K;j++) {
  		for (int i = n;i - (1 << j) >= 0;i--)
				levo[i][j] = max(levo[i][j - 1], levo[i - (1 << (j - 1))][j - 1]);
  	}
  	auto Close = [&](int s)	{
  		auto u = upper_bound(x + 1, x + n + 1, s) - x;
  		if (abs(x[u] - s) < abs(x[u - 1] - s))
  			return u;
  		return u - 1;
  	};
  	cin >> q;
  	while (q--) {
  		int s;
  		cin >> s;
  		int ans = 0;
  		int p = Close(s);
  		ans += abs(x[p] - s);
  		int l = p, r = p;
  		int t = p;
  		while (l != 1 && r != n) {
  			int np;
  			if (abs(x[l - 1] - x[t]) <= abs(x[r + 1] - x[t])) {
  				//idem levo
  				np = l;
  				for (int i = K - 1;i >= 0;i--) {
  					if (levo[np][i] <= x[r + 1]) {
  						np -= (1 << i);
  					}
  				}
  				l = np;
  				ans += abs(x[np] - x[t]);
  				t = np;
  			}
  			else {
  				//idem desno
  				np = r;
  				for (int i = K - 1;i >= 0;i--) {
  					if (desno[np][i] > x[l - 1])
  						np += (1 << i);
  				}
  				r = np;
  				ans += abs(x[np] - x[t]);
  				t = np;
  			}
  		}
  		ans += x[n] - x[1];
  		cout << ans << '\n';
  	}
	}
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 45 ms 72272 KB Output is correct
2 Correct 46 ms 72532 KB Output is correct
3 Correct 47 ms 72528 KB Output is correct
4 Correct 46 ms 72528 KB Output is correct
5 Correct 47 ms 72276 KB Output is correct
6 Correct 46 ms 72528 KB Output is correct
7 Correct 47 ms 72528 KB Output is correct
8 Correct 49 ms 72628 KB Output is correct
9 Correct 47 ms 72272 KB Output is correct
10 Correct 47 ms 72300 KB Output is correct
11 Correct 46 ms 72272 KB Output is correct
12 Correct 48 ms 72280 KB Output is correct
13 Correct 48 ms 72280 KB Output is correct
14 Correct 46 ms 72472 KB Output is correct
15 Correct 47 ms 72312 KB Output is correct
16 Correct 46 ms 72528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 72272 KB Output is correct
2 Correct 46 ms 72532 KB Output is correct
3 Correct 47 ms 72528 KB Output is correct
4 Correct 46 ms 72528 KB Output is correct
5 Correct 47 ms 72276 KB Output is correct
6 Correct 46 ms 72528 KB Output is correct
7 Correct 47 ms 72528 KB Output is correct
8 Correct 49 ms 72628 KB Output is correct
9 Correct 47 ms 72272 KB Output is correct
10 Correct 47 ms 72300 KB Output is correct
11 Correct 46 ms 72272 KB Output is correct
12 Correct 48 ms 72280 KB Output is correct
13 Correct 48 ms 72280 KB Output is correct
14 Correct 46 ms 72472 KB Output is correct
15 Correct 47 ms 72312 KB Output is correct
16 Correct 46 ms 72528 KB Output is correct
17 Correct 144 ms 75460 KB Output is correct
18 Correct 141 ms 75088 KB Output is correct
19 Correct 146 ms 75344 KB Output is correct
20 Correct 143 ms 75180 KB Output is correct
21 Correct 146 ms 75300 KB Output is correct
22 Correct 160 ms 75340 KB Output is correct
23 Correct 155 ms 75336 KB Output is correct
24 Correct 158 ms 75336 KB Output is correct
25 Correct 145 ms 75088 KB Output is correct
26 Correct 152 ms 75344 KB Output is correct
27 Correct 144 ms 75344 KB Output is correct
28 Correct 149 ms 75336 KB Output is correct
29 Correct 144 ms 75336 KB Output is correct
30 Correct 146 ms 75284 KB Output is correct
31 Correct 153 ms 75340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 72496 KB Output is correct
2 Correct 46 ms 72272 KB Output is correct
3 Correct 46 ms 72272 KB Output is correct
4 Correct 47 ms 72268 KB Output is correct
5 Correct 48 ms 72276 KB Output is correct
6 Correct 47 ms 72504 KB Output is correct
7 Correct 303 ms 77136 KB Output is correct
8 Correct 299 ms 76984 KB Output is correct
9 Correct 308 ms 76980 KB Output is correct
10 Correct 304 ms 76784 KB Output is correct
11 Correct 306 ms 76992 KB Output is correct
12 Correct 307 ms 76880 KB Output is correct
13 Correct 81 ms 75628 KB Output is correct
14 Correct 70 ms 73404 KB Output is correct
15 Correct 272 ms 77624 KB Output is correct
16 Correct 275 ms 77488 KB Output is correct
17 Correct 267 ms 77476 KB Output is correct
18 Correct 79 ms 75632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 72272 KB Output is correct
2 Correct 46 ms 72532 KB Output is correct
3 Correct 47 ms 72528 KB Output is correct
4 Correct 46 ms 72528 KB Output is correct
5 Correct 47 ms 72276 KB Output is correct
6 Correct 46 ms 72528 KB Output is correct
7 Correct 47 ms 72528 KB Output is correct
8 Correct 49 ms 72628 KB Output is correct
9 Correct 47 ms 72272 KB Output is correct
10 Correct 47 ms 72300 KB Output is correct
11 Correct 46 ms 72272 KB Output is correct
12 Correct 48 ms 72280 KB Output is correct
13 Correct 48 ms 72280 KB Output is correct
14 Correct 46 ms 72472 KB Output is correct
15 Correct 47 ms 72312 KB Output is correct
16 Correct 46 ms 72528 KB Output is correct
17 Correct 144 ms 75460 KB Output is correct
18 Correct 141 ms 75088 KB Output is correct
19 Correct 146 ms 75344 KB Output is correct
20 Correct 143 ms 75180 KB Output is correct
21 Correct 146 ms 75300 KB Output is correct
22 Correct 160 ms 75340 KB Output is correct
23 Correct 155 ms 75336 KB Output is correct
24 Correct 158 ms 75336 KB Output is correct
25 Correct 145 ms 75088 KB Output is correct
26 Correct 152 ms 75344 KB Output is correct
27 Correct 144 ms 75344 KB Output is correct
28 Correct 149 ms 75336 KB Output is correct
29 Correct 144 ms 75336 KB Output is correct
30 Correct 146 ms 75284 KB Output is correct
31 Correct 153 ms 75340 KB Output is correct
32 Correct 46 ms 72496 KB Output is correct
33 Correct 46 ms 72272 KB Output is correct
34 Correct 46 ms 72272 KB Output is correct
35 Correct 47 ms 72268 KB Output is correct
36 Correct 48 ms 72276 KB Output is correct
37 Correct 47 ms 72504 KB Output is correct
38 Correct 303 ms 77136 KB Output is correct
39 Correct 299 ms 76984 KB Output is correct
40 Correct 308 ms 76980 KB Output is correct
41 Correct 304 ms 76784 KB Output is correct
42 Correct 306 ms 76992 KB Output is correct
43 Correct 307 ms 76880 KB Output is correct
44 Correct 81 ms 75628 KB Output is correct
45 Correct 70 ms 73404 KB Output is correct
46 Correct 272 ms 77624 KB Output is correct
47 Correct 275 ms 77488 KB Output is correct
48 Correct 267 ms 77476 KB Output is correct
49 Correct 79 ms 75632 KB Output is correct
50 Correct 476 ms 78780 KB Output is correct
51 Correct 432 ms 78504 KB Output is correct
52 Correct 422 ms 78452 KB Output is correct
53 Correct 371 ms 78416 KB Output is correct
54 Correct 374 ms 78372 KB Output is correct
55 Correct 385 ms 78524 KB Output is correct
56 Correct 197 ms 78244 KB Output is correct
57 Correct 201 ms 78136 KB Output is correct
58 Correct 200 ms 78072 KB Output is correct