답안 #826703

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
826703 2023-08-15T21:29:49 Z Asymmetry Bitaro's travel (JOI23_travel) C++17
100 / 100
282 ms 74576 KB
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int n;
	cin >> n;
	vector t(n + 2, 0ll);
	FOR (i, 1, n)
		cin >> t[i];
	const LL INF = LL(1e18);
	t[0] = -INF;
	t[n + 1] = INF;
	debug(t);
	const int Z = __lg(int(1e9)) + 2;
	debug(Z);
	vector type(n + 1, Z - 1);
	REP (i, n + 1) {
		REP (j, Z)
			if ((1ll << j) > (t[i + 1] - t[i])) {
				type[i] = j;
				break;
			}
	}
	debug(type);
	vector prv(n + 1, vector(Z, 0));
	auto nxt = prv;
	REP (i, Z)
		prv[1][i] = 1;
	FOR (i, 2, n) {
		prv[i] = prv[i - 1];
		REP (j, Z)
			if ((1 << j) <= t[i] - t[i - 1])
				prv[i][j] = i;
	}

	REP (i, Z)
		nxt[n][i] = n;
	for (int i = n - 1; i >= 1; --i) {
		nxt[i] = nxt[i + 1];
		REP (j, Z)
			if ((1 << j) <= t[i + 1] - t[i])
				nxt[i][j] = i;
	}

	debug(prv);
	debug(nxt);

	int q;
	cin >> q;
	REP (xd, q) {
		LL x;
		cin >> x;
		LL ans = 0;
		int pos = int(upper_bound(t.begin(), t.end(), x) - t.begin());
		if (t[pos] - x >= x - t[pos - 1])
			--pos;
		ans = abs(x - t[pos]);
		int lf = pos, rg = pos;
		debug(pos, t[pos] - t[pos - 1], t[pos + 1] - t[pos]);
		int side = (t[pos] - t[pos - 1]) <= (t[pos + 1] - t[pos]);
		int pot = 0;
		while (lf != 1 || rg != n) {
			debug(lf, rg, side);
			while ((2 << pot) <= t[rg] - t[lf])
				++pot;
			debug(pot);
			if (side) {
				// left
				ans += t[lf] - t[prv[lf][pot]];
				lf = prv[lf][pot];
				if (lf > 1 && t[lf] - t[lf - 1] <= t[rg + 1] - t[lf]) {
					ans += t[lf] - t[lf - 1];
					--lf;
				}
				else if (rg < n) {
					ans += t[rg] - t[lf];
					side ^= 1;
				}
			}
			else {
				// right
				ans += t[nxt[rg][pot]] - t[rg];
				rg = nxt[rg][pot];
				if (rg < n && t[rg + 1] - t[rg] < t[rg] - t[lf - 1]) {
					ans += t[rg + 1] - t[rg];
					++rg;
				}
				else if (lf > 1) {
					ans += t[rg] - t[lf];
					side ^= 1;
				}
			}
		}
		cout << ans << '\n';
	}
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 976 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 968 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 320 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 1 ms 980 KB Output is correct
16 Correct 1 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 976 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 968 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 320 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 1 ms 980 KB Output is correct
16 Correct 1 ms 980 KB Output is correct
17 Correct 69 ms 70284 KB Output is correct
18 Correct 70 ms 70232 KB Output is correct
19 Correct 69 ms 70348 KB Output is correct
20 Correct 70 ms 70352 KB Output is correct
21 Correct 75 ms 70472 KB Output is correct
22 Correct 69 ms 70340 KB Output is correct
23 Correct 76 ms 70356 KB Output is correct
24 Correct 72 ms 70280 KB Output is correct
25 Correct 72 ms 70252 KB Output is correct
26 Correct 71 ms 70276 KB Output is correct
27 Correct 74 ms 70360 KB Output is correct
28 Correct 78 ms 70348 KB Output is correct
29 Correct 73 ms 70200 KB Output is correct
30 Correct 70 ms 70280 KB Output is correct
31 Correct 70 ms 70228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 200 ms 72280 KB Output is correct
8 Correct 198 ms 72328 KB Output is correct
9 Correct 208 ms 72300 KB Output is correct
10 Correct 196 ms 72304 KB Output is correct
11 Correct 203 ms 72280 KB Output is correct
12 Correct 205 ms 72324 KB Output is correct
13 Correct 34 ms 4204 KB Output is correct
14 Correct 25 ms 1452 KB Output is correct
15 Correct 173 ms 73416 KB Output is correct
16 Correct 178 ms 73428 KB Output is correct
17 Correct 170 ms 73420 KB Output is correct
18 Correct 34 ms 4044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 976 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 968 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 320 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 1 ms 980 KB Output is correct
16 Correct 1 ms 980 KB Output is correct
17 Correct 69 ms 70284 KB Output is correct
18 Correct 70 ms 70232 KB Output is correct
19 Correct 69 ms 70348 KB Output is correct
20 Correct 70 ms 70352 KB Output is correct
21 Correct 75 ms 70472 KB Output is correct
22 Correct 69 ms 70340 KB Output is correct
23 Correct 76 ms 70356 KB Output is correct
24 Correct 72 ms 70280 KB Output is correct
25 Correct 72 ms 70252 KB Output is correct
26 Correct 71 ms 70276 KB Output is correct
27 Correct 74 ms 70360 KB Output is correct
28 Correct 78 ms 70348 KB Output is correct
29 Correct 73 ms 70200 KB Output is correct
30 Correct 70 ms 70280 KB Output is correct
31 Correct 70 ms 70228 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 320 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 320 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 200 ms 72280 KB Output is correct
39 Correct 198 ms 72328 KB Output is correct
40 Correct 208 ms 72300 KB Output is correct
41 Correct 196 ms 72304 KB Output is correct
42 Correct 203 ms 72280 KB Output is correct
43 Correct 205 ms 72324 KB Output is correct
44 Correct 34 ms 4204 KB Output is correct
45 Correct 25 ms 1452 KB Output is correct
46 Correct 173 ms 73416 KB Output is correct
47 Correct 178 ms 73428 KB Output is correct
48 Correct 170 ms 73420 KB Output is correct
49 Correct 34 ms 4044 KB Output is correct
50 Correct 274 ms 74460 KB Output is correct
51 Correct 241 ms 74576 KB Output is correct
52 Correct 227 ms 74444 KB Output is correct
53 Correct 275 ms 74508 KB Output is correct
54 Correct 282 ms 74424 KB Output is correct
55 Correct 282 ms 74468 KB Output is correct
56 Correct 139 ms 74308 KB Output is correct
57 Correct 142 ms 74344 KB Output is correct
58 Correct 141 ms 74320 KB Output is correct