Submission #1030660

# Submission time Handle Problem Language Result Execution time Memory
1030660 2024-07-22T08:14:48 Z Unforgettablepl Bitaro's travel (JOI23_travel) C++17
100 / 100
401 ms 16244 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

struct segtree{
	vector<int> tree;
	segtree():tree(524288,INT64_MIN){}
	void update(int k,int x){
		k+=262144;
		tree[k]=x;
		while(k/=2){
			tree[k]=max(tree[2*k],tree[2*k+1]);
		}
	}
	int get(int a,int b){
		a+=262144;b+=262144;
		int ans = INT64_MIN;
		while(a<=b){
			if(a&1)ans=max(ans,tree[a++]);
			if(b%2 == 0)ans=max(ans,tree[b--]);
			a/=2;b/=2;
		}
		return ans;
	}
};

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin >> n;
	vector<int> arr(n+2);
	for(int i=1;i<=n;i++)cin>>arr[i];
	arr[0] = -1e12;
	arr[n+1] = 1e12;
	segtree seg;
	for(int i=1;i<=n+1;i++){
		seg.update(i,2ll*arr[i]-arr[i-1]);
	}
	segtree seg2;
	for(int i=0;i<=n;i++){
		seg2.update(i,-(2ll*arr[i]-arr[i+1]));
	}
	int q;
	cin >> q;
	for(int i=1;i<=q;i++){
		int curr_l,curr_r,curr_pos;
		cin >> curr_pos;
		int ans = 0;
		auto above = upper_bound(arr.begin(),arr.end(),curr_pos);
		if((*above)-curr_pos<curr_pos-(*(above-1))){
			ans+=(*above)-curr_pos;
			curr_l=curr_r=above-arr.begin();
			curr_pos=arr[curr_l];
		} else {
			above--;
			ans+=curr_pos-(*above);
			curr_l=curr_r=above-arr.begin();	
			curr_pos=arr[curr_l];
		}
		while(1<curr_l or curr_r<n){
			if(curr_pos-arr[curr_l-1]<=arr[curr_r+1]-curr_pos){
				// We go left
				for(int jump=131072;jump;jump/=2){
					if(curr_l-jump<=0)continue;
					if(seg.get(curr_l-jump+1,curr_l)<=arr[curr_r+1])curr_l-=jump;
				}
				ans+=curr_pos-arr[curr_l];
				curr_pos = arr[curr_l];
			} else {
				// We go right
				for(int jump=131072;jump;jump/=2){
					if(curr_r+jump>n)continue;
					if((-seg2.get(curr_r,curr_r+jump-1))>arr[curr_l-1])curr_r+=jump;
				}
				ans+=arr[curr_r]-curr_pos;
				curr_pos = arr[curr_r];
			}
		}
		cout << ans << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8540 KB Output is correct
2 Correct 3 ms 8512 KB Output is correct
3 Correct 3 ms 8680 KB Output is correct
4 Correct 3 ms 8580 KB Output is correct
5 Correct 4 ms 8540 KB Output is correct
6 Correct 3 ms 8540 KB Output is correct
7 Correct 3 ms 8568 KB Output is correct
8 Correct 3 ms 8540 KB Output is correct
9 Correct 3 ms 8536 KB Output is correct
10 Correct 3 ms 8540 KB Output is correct
11 Correct 3 ms 8540 KB Output is correct
12 Correct 3 ms 8540 KB Output is correct
13 Correct 3 ms 8540 KB Output is correct
14 Correct 3 ms 8540 KB Output is correct
15 Correct 3 ms 8692 KB Output is correct
16 Correct 3 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8540 KB Output is correct
2 Correct 3 ms 8512 KB Output is correct
3 Correct 3 ms 8680 KB Output is correct
4 Correct 3 ms 8580 KB Output is correct
5 Correct 4 ms 8540 KB Output is correct
6 Correct 3 ms 8540 KB Output is correct
7 Correct 3 ms 8568 KB Output is correct
8 Correct 3 ms 8540 KB Output is correct
9 Correct 3 ms 8536 KB Output is correct
10 Correct 3 ms 8540 KB Output is correct
11 Correct 3 ms 8540 KB Output is correct
12 Correct 3 ms 8540 KB Output is correct
13 Correct 3 ms 8540 KB Output is correct
14 Correct 3 ms 8540 KB Output is correct
15 Correct 3 ms 8692 KB Output is correct
16 Correct 3 ms 8540 KB Output is correct
17 Correct 35 ms 12164 KB Output is correct
18 Correct 35 ms 12120 KB Output is correct
19 Correct 34 ms 12116 KB Output is correct
20 Correct 34 ms 12124 KB Output is correct
21 Correct 35 ms 12112 KB Output is correct
22 Correct 34 ms 12116 KB Output is correct
23 Correct 34 ms 12116 KB Output is correct
24 Correct 34 ms 12140 KB Output is correct
25 Correct 33 ms 12124 KB Output is correct
26 Correct 34 ms 12120 KB Output is correct
27 Correct 34 ms 12116 KB Output is correct
28 Correct 34 ms 12124 KB Output is correct
29 Correct 33 ms 11856 KB Output is correct
30 Correct 35 ms 11888 KB Output is correct
31 Correct 34 ms 11860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8540 KB Output is correct
2 Correct 3 ms 8540 KB Output is correct
3 Correct 3 ms 8536 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 3 ms 8540 KB Output is correct
7 Correct 200 ms 14144 KB Output is correct
8 Correct 198 ms 14088 KB Output is correct
9 Correct 206 ms 14164 KB Output is correct
10 Correct 216 ms 14160 KB Output is correct
11 Correct 207 ms 14160 KB Output is correct
12 Correct 219 ms 14164 KB Output is correct
13 Correct 30 ms 12368 KB Output is correct
14 Correct 20 ms 9816 KB Output is correct
15 Correct 169 ms 15180 KB Output is correct
16 Correct 168 ms 15184 KB Output is correct
17 Correct 166 ms 15188 KB Output is correct
18 Correct 28 ms 12372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8540 KB Output is correct
2 Correct 3 ms 8512 KB Output is correct
3 Correct 3 ms 8680 KB Output is correct
4 Correct 3 ms 8580 KB Output is correct
5 Correct 4 ms 8540 KB Output is correct
6 Correct 3 ms 8540 KB Output is correct
7 Correct 3 ms 8568 KB Output is correct
8 Correct 3 ms 8540 KB Output is correct
9 Correct 3 ms 8536 KB Output is correct
10 Correct 3 ms 8540 KB Output is correct
11 Correct 3 ms 8540 KB Output is correct
12 Correct 3 ms 8540 KB Output is correct
13 Correct 3 ms 8540 KB Output is correct
14 Correct 3 ms 8540 KB Output is correct
15 Correct 3 ms 8692 KB Output is correct
16 Correct 3 ms 8540 KB Output is correct
17 Correct 35 ms 12164 KB Output is correct
18 Correct 35 ms 12120 KB Output is correct
19 Correct 34 ms 12116 KB Output is correct
20 Correct 34 ms 12124 KB Output is correct
21 Correct 35 ms 12112 KB Output is correct
22 Correct 34 ms 12116 KB Output is correct
23 Correct 34 ms 12116 KB Output is correct
24 Correct 34 ms 12140 KB Output is correct
25 Correct 33 ms 12124 KB Output is correct
26 Correct 34 ms 12120 KB Output is correct
27 Correct 34 ms 12116 KB Output is correct
28 Correct 34 ms 12124 KB Output is correct
29 Correct 33 ms 11856 KB Output is correct
30 Correct 35 ms 11888 KB Output is correct
31 Correct 34 ms 11860 KB Output is correct
32 Correct 3 ms 8540 KB Output is correct
33 Correct 3 ms 8540 KB Output is correct
34 Correct 3 ms 8536 KB Output is correct
35 Correct 3 ms 8540 KB Output is correct
36 Correct 2 ms 8540 KB Output is correct
37 Correct 3 ms 8540 KB Output is correct
38 Correct 200 ms 14144 KB Output is correct
39 Correct 198 ms 14088 KB Output is correct
40 Correct 206 ms 14164 KB Output is correct
41 Correct 216 ms 14160 KB Output is correct
42 Correct 207 ms 14160 KB Output is correct
43 Correct 219 ms 14164 KB Output is correct
44 Correct 30 ms 12368 KB Output is correct
45 Correct 20 ms 9816 KB Output is correct
46 Correct 169 ms 15180 KB Output is correct
47 Correct 168 ms 15184 KB Output is correct
48 Correct 166 ms 15188 KB Output is correct
49 Correct 28 ms 12372 KB Output is correct
50 Correct 401 ms 16204 KB Output is correct
51 Correct 400 ms 16088 KB Output is correct
52 Correct 316 ms 16208 KB Output is correct
53 Correct 264 ms 16212 KB Output is correct
54 Correct 264 ms 16244 KB Output is correct
55 Correct 283 ms 16208 KB Output is correct
56 Correct 148 ms 16112 KB Output is correct
57 Correct 152 ms 16028 KB Output is correct
58 Correct 158 ms 16072 KB Output is correct