답안 #923185

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
923185 2024-02-06T20:04:23 Z denniskim Bitaro's travel (JOI23_travel) C++17
15 / 100
228 ms 77676 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;
	
	for(ll i = 1 ; i < n ; i++)
		gap2[i] = 2 * a[i] - a[i + 1];
	
	gap2[n] = -INF;
	
	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(1LL, j - (1LL << (i - 1)));
			ll R = min(n, 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;
		
		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 = 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(maxx[now][i] <= XX)
						now -= (1LL << i);
				}
				
				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(minn[now][i] > XX)
						now += (1LL << i);
				}
				
				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 8796 KB Output is correct
5 Correct 2 ms 8664 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 2 ms 6616 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6648 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 8672 KB Output is correct
16 Correct 2 ms 8792 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 8796 KB Output is correct
5 Correct 2 ms 8664 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 2 ms 6616 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6648 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 8672 KB Output is correct
16 Correct 2 ms 8792 KB Output is correct
17 Correct 98 ms 75856 KB Output is correct
18 Correct 96 ms 75904 KB Output is correct
19 Correct 96 ms 75888 KB Output is correct
20 Correct 99 ms 75904 KB Output is correct
21 Correct 100 ms 75728 KB Output is correct
22 Correct 100 ms 75900 KB Output is correct
23 Correct 97 ms 75920 KB Output is correct
24 Correct 99 ms 75900 KB Output is correct
25 Correct 98 ms 75896 KB Output is correct
26 Correct 97 ms 75900 KB Output is correct
27 Correct 95 ms 75884 KB Output is correct
28 Correct 97 ms 76052 KB Output is correct
29 Correct 95 ms 75808 KB Output is correct
30 Correct 92 ms 75604 KB Output is correct
31 Correct 97 ms 75844 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 6492 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 6492 KB Output is correct
7 Incorrect 228 ms 77676 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 8796 KB Output is correct
5 Correct 2 ms 8664 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 2 ms 6616 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6648 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 8672 KB Output is correct
16 Correct 2 ms 8792 KB Output is correct
17 Correct 98 ms 75856 KB Output is correct
18 Correct 96 ms 75904 KB Output is correct
19 Correct 96 ms 75888 KB Output is correct
20 Correct 99 ms 75904 KB Output is correct
21 Correct 100 ms 75728 KB Output is correct
22 Correct 100 ms 75900 KB Output is correct
23 Correct 97 ms 75920 KB Output is correct
24 Correct 99 ms 75900 KB Output is correct
25 Correct 98 ms 75896 KB Output is correct
26 Correct 97 ms 75900 KB Output is correct
27 Correct 95 ms 75884 KB Output is correct
28 Correct 97 ms 76052 KB Output is correct
29 Correct 95 ms 75808 KB Output is correct
30 Correct 92 ms 75604 KB Output is correct
31 Correct 97 ms 75844 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 6492 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 6492 KB Output is correct
38 Incorrect 228 ms 77676 KB Output isn't correct
39 Halted 0 ms 0 KB -