답안 #923207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
923207 2024-02-06T20:52:09 Z denniskim Bitaro's travel (JOI23_travel) C++17
100 / 100
254 ms 80016 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())

ll n;
ll a[200010];
ll q;
ll X;
ll gap1[200010], gap2[200010];
ll minn[2500010][21], maxx[2500010][21];

int main(void)
{
	fastio
	
	cin >> n;
	
	for(ll i = 1 ; i <= n ; i++)
		cin >> a[i];
	
	for(ll i = 2 ; i <= n ; i++)
		gap1[i] = 2 * a[i] - a[i - 1];
	
	gap1[1] = INF / n;
	
	for(ll i = 1 ; i < n ; i++)
		gap2[i] = 2 * a[i] - a[i + 1];
	
	gap2[n] = -INF / n;
	
	for(ll i = 1 ; i <= n ; i++)
		minn[i][0] = gap2[i], maxx[i][0] = gap1[i];
	
	for(ll i = 1 ; i <= 20 ; i++)
	{
		for(ll j = 1 ; j <= n ; j++)
		{
			ll L = max(2LL, j - (1LL << (i - 1)));
			ll R = min(n - 1, j + (1LL << (i - 1)));
			
			minn[j][i] = min(minn[j][i - 1], minn[R][i - 1]);
			maxx[j][i] = max(maxx[j][i - 1], maxx[L][i - 1]);
		}
	}
	
	cin >> q;
	
	while(q--)
	{
		cin >> X;
		
		if(n == 1)
		{
			cout << abs(X - a[1]) en;
			continue;
		}
		
		ll st = 0;
		ll ans = 0;
		
		if(X <= a[1])
			st = 1, ans += a[1] - X;
		else if(X >= a[n])
			st = n, ans += X - a[n];
		
		else
		{
			ll idx = lower_bound(a + 1, a + 1 + n, X) - a;
			ll idx2 = idx - 1;
			
			if(X - a[idx2] <= a[idx] - X)
				st = idx2, ans += X - a[idx2];
			else
				st = idx, ans += a[idx] - X;
		}
		
		ll L = st, R = st;
		ll D;
		
		if(st == 1)
			D = 1;
		else if(st == n)
			D = -1;
		
		else
		{
			if(a[st] - a[st - 1] <= a[st + 1] - a[st])
				D = -1;
			else
				D = 1;
		}
		
		while(1)
		{
			if(L <= 1)
			{
				if(D == 1)
					ans += a[n] - a[R];
				else
					ans += a[n] - a[1];
				
				break;
			}
			
			if(R >= n)
			{
				if(D == -1)
					ans += a[L] - a[1];
				else
					ans += a[n] - a[1];
				
				break;
			}
			
			if(D == -1)
			{
				ll XX = a[R + 1];
				ll now = L;
				
				for(ll i = 20 ; i >= 0 ; i--)
				{
					if(now <= 1)
						break;
					
					if(maxx[now][i] <= XX)
						now -= (1LL << i);
				}
				
				if(now <= 1)
				{
					ans += a[L] - a[1];
					ans += a[n] - a[1];
					break;
				}
				
				ans += a[L] - a[now];
				ans += a[R + 1] - a[now];
				
				L = now, R = R + 1, D = 1;
			}
			
			else
			{
				ll XX = a[L - 1];
				ll now = R;
				
				for(ll i = 20 ; i >= 0 ; i--)
				{
					if(now >= n)
						break;
					
					if(minn[now][i] > XX)
						now += (1LL << i);
				}
				
				if(now >= n)
				{
					ans += a[n] - a[R];
					ans += a[n] - a[1];
					break;
				}
				
				ans += a[now] - a[R];
				ans += a[now] - a[L - 1];
				
				L = L - 1, R = now, D = -1;
			}
		}
		
		cout << ans en;
	}
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 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 8796 KB Output is correct
6 Correct 1 ms 8796 KB Output is correct
7 Correct 1 ms 8796 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 8796 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 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 8796 KB Output is correct
6 Correct 1 ms 8796 KB Output is correct
7 Correct 1 ms 8796 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 8796 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 89 ms 73812 KB Output is correct
18 Correct 84 ms 73952 KB Output is correct
19 Correct 83 ms 73716 KB Output is correct
20 Correct 85 ms 73960 KB Output is correct
21 Correct 83 ms 73964 KB Output is correct
22 Correct 84 ms 73848 KB Output is correct
23 Correct 86 ms 73964 KB Output is correct
24 Correct 84 ms 73920 KB Output is correct
25 Correct 86 ms 73960 KB Output is correct
26 Correct 85 ms 73820 KB Output is correct
27 Correct 83 ms 73816 KB Output is correct
28 Correct 83 ms 73968 KB Output is correct
29 Correct 82 ms 73812 KB Output is correct
30 Correct 85 ms 74068 KB Output is correct
31 Correct 82 ms 73808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 147 ms 75512 KB Output is correct
8 Correct 145 ms 75344 KB Output is correct
9 Correct 147 ms 75348 KB Output is correct
10 Correct 149 ms 75520 KB Output is correct
11 Correct 145 ms 75364 KB Output is correct
12 Correct 145 ms 75348 KB Output is correct
13 Correct 36 ms 8496 KB Output is correct
14 Correct 22 ms 7000 KB Output is correct
15 Correct 140 ms 75764 KB Output is correct
16 Correct 143 ms 78932 KB Output is correct
17 Correct 144 ms 79184 KB Output is correct
18 Correct 34 ms 10324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 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 8796 KB Output is correct
6 Correct 1 ms 8796 KB Output is correct
7 Correct 1 ms 8796 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 8796 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 89 ms 73812 KB Output is correct
18 Correct 84 ms 73952 KB Output is correct
19 Correct 83 ms 73716 KB Output is correct
20 Correct 85 ms 73960 KB Output is correct
21 Correct 83 ms 73964 KB Output is correct
22 Correct 84 ms 73848 KB Output is correct
23 Correct 86 ms 73964 KB Output is correct
24 Correct 84 ms 73920 KB Output is correct
25 Correct 86 ms 73960 KB Output is correct
26 Correct 85 ms 73820 KB Output is correct
27 Correct 83 ms 73816 KB Output is correct
28 Correct 83 ms 73968 KB Output is correct
29 Correct 82 ms 73812 KB Output is correct
30 Correct 85 ms 74068 KB Output is correct
31 Correct 82 ms 73808 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6488 KB Output is correct
35 Correct 1 ms 6492 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6488 KB Output is correct
38 Correct 147 ms 75512 KB Output is correct
39 Correct 145 ms 75344 KB Output is correct
40 Correct 147 ms 75348 KB Output is correct
41 Correct 149 ms 75520 KB Output is correct
42 Correct 145 ms 75364 KB Output is correct
43 Correct 145 ms 75348 KB Output is correct
44 Correct 36 ms 8496 KB Output is correct
45 Correct 22 ms 7000 KB Output is correct
46 Correct 140 ms 75764 KB Output is correct
47 Correct 143 ms 78932 KB Output is correct
48 Correct 144 ms 79184 KB Output is correct
49 Correct 34 ms 10324 KB Output is correct
50 Correct 231 ms 80016 KB Output is correct
51 Correct 239 ms 79972 KB Output is correct
52 Correct 254 ms 79952 KB Output is correct
53 Correct 178 ms 79956 KB Output is correct
54 Correct 180 ms 79808 KB Output is correct
55 Correct 194 ms 79956 KB Output is correct
56 Correct 128 ms 79956 KB Output is correct
57 Correct 126 ms 79696 KB Output is correct
58 Correct 127 ms 79808 KB Output is correct