Submission #923203

# Submission time Handle Problem Language Result Execution time Memory
923203 2024-02-06T20:48:42 Z denniskim Bitaro's travel (JOI23_travel) C++17
15 / 100
149 ms 78880 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 - 1;
				
				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 + 1;
				
				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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 1 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 2 ms 8792 KB Output is correct
8 Correct 1 ms 6488 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 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 8792 KB Output is correct
16 Correct 2 ms 8792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 1 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 2 ms 8792 KB Output is correct
8 Correct 1 ms 6488 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 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 8792 KB Output is correct
16 Correct 2 ms 8792 KB Output is correct
17 Correct 94 ms 73864 KB Output is correct
18 Correct 88 ms 73808 KB Output is correct
19 Correct 88 ms 73812 KB Output is correct
20 Correct 96 ms 73964 KB Output is correct
21 Correct 86 ms 73964 KB Output is correct
22 Correct 86 ms 73816 KB Output is correct
23 Correct 84 ms 73960 KB Output is correct
24 Correct 85 ms 73964 KB Output is correct
25 Correct 86 ms 73816 KB Output is correct
26 Correct 83 ms 73880 KB Output is correct
27 Correct 87 ms 73820 KB Output is correct
28 Correct 85 ms 74132 KB Output is correct
29 Correct 87 ms 73816 KB Output is correct
30 Correct 85 ms 74064 KB Output is correct
31 Correct 85 ms 73964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 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 Correct 148 ms 75260 KB Output is correct
8 Correct 147 ms 77920 KB Output is correct
9 Correct 147 ms 77904 KB Output is correct
10 Correct 149 ms 77676 KB Output is correct
11 Correct 148 ms 77920 KB Output is correct
12 Correct 147 ms 77828 KB Output is correct
13 Correct 37 ms 10264 KB Output is correct
14 Correct 25 ms 7772 KB Output is correct
15 Incorrect 144 ms 78880 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 1 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 2 ms 8792 KB Output is correct
8 Correct 1 ms 6488 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 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 8792 KB Output is correct
16 Correct 2 ms 8792 KB Output is correct
17 Correct 94 ms 73864 KB Output is correct
18 Correct 88 ms 73808 KB Output is correct
19 Correct 88 ms 73812 KB Output is correct
20 Correct 96 ms 73964 KB Output is correct
21 Correct 86 ms 73964 KB Output is correct
22 Correct 86 ms 73816 KB Output is correct
23 Correct 84 ms 73960 KB Output is correct
24 Correct 85 ms 73964 KB Output is correct
25 Correct 86 ms 73816 KB Output is correct
26 Correct 83 ms 73880 KB Output is correct
27 Correct 87 ms 73820 KB Output is correct
28 Correct 85 ms 74132 KB Output is correct
29 Correct 87 ms 73816 KB Output is correct
30 Correct 85 ms 74064 KB Output is correct
31 Correct 85 ms 73964 KB Output is correct
32 Correct 1 ms 6488 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 Correct 148 ms 75260 KB Output is correct
39 Correct 147 ms 77920 KB Output is correct
40 Correct 147 ms 77904 KB Output is correct
41 Correct 149 ms 77676 KB Output is correct
42 Correct 148 ms 77920 KB Output is correct
43 Correct 147 ms 77828 KB Output is correct
44 Correct 37 ms 10264 KB Output is correct
45 Correct 25 ms 7772 KB Output is correct
46 Incorrect 144 ms 78880 KB Output isn't correct
47 Halted 0 ms 0 KB -