답안 #954259

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
954259 2024-03-27T14:34:16 Z LCJLY Bitaro's travel (JOI23_travel) C++14
100 / 100
347 ms 66548 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
 
struct SparseTable {
    vector<vector<int> > ST;
    int N, K;
    SparseTable(int _N, int a[]): N(_N) {
        K = MSB(N);
        ST.resize(K);
        ST[0] = vector<int>(a, a+N);
        for (int k = 1; k < K; ++k) 
            for (int i = 0; i+(1<<k) <= N; ++i) 
                ST[k].push_back( min(ST[k-1][i], ST[k-1][i+(1<<(k-1))]) ); 
    }
 
    inline int MSB(unsigned int x) { return 32-__builtin_clz(x); }
    
    int query(int x, int y) {
        int k = MSB(y-x+1) - 1;
        return min(ST[k][x], ST[k][y-(1<<k)+1]);            
    }
};
 
struct SparseTable2 {
    vector<vector<int> > ST;
    int N, K;
    SparseTable2(int _N, int a[]): N(_N) {
        K = MSB(N);
        ST.resize(K);
        ST[0] = vector<int>(a, a+N);
        for (int k = 1; k < K; ++k) 
            for (int i = 0; i+(1<<k) <= N; ++i) 
                ST[k].push_back( max(ST[k-1][i], ST[k-1][i+(1<<(k-1))]) ); 
    }
 
    inline int MSB(unsigned int x) { return 32-__builtin_clz(x); }
    
    int query(int x, int y) {
        int k = MSB(y-x+1) - 1;
        return max(ST[k][x], ST[k][y-(1<<k)+1]);            
    }
};

int r[200005];
int l[200005];
 
 
void solve(){
	int n;
	cin >> n;
	int arr[n+5];
	for(int x=1;x<=n;x++){
		cin >> arr[x];
	}
	arr[0]=-1e17;
	arr[n+1]=1e17;
	
	
	l[0]=1e15;
	r[n+1]=-1e15;
	for(int x=1;x<=n;x++){
		l[x]=2*arr[x]-arr[x-1];
		r[x]=2*arr[x]-arr[x+1];
	}
	
	SparseTable2 st(n+2,l);
	SparseTable st2(n+2,r);
	
	int q;
	cin >> q;
	
	for(int x=0;x<q;x++){
		int index;
		cin >> index;
		
		int cur=0;
		int hold=lower_bound(arr,arr+n+2,index)-arr;
		if(index-arr[hold-1]<=arr[hold]-index) cur=hold-1;
		else cur=hold;
		
		int counter=abs(index-arr[cur]);
		
		bool amos=true;
		
		if(arr[cur]-arr[cur-1]<=arr[cur+1]-arr[cur]) amos=false;
		
		int left=cur;
		int right=cur;
		
		while(left>1&&right<n){
			if(amos){
				//venture right
				int target=arr[left-1];
				//modify
				
				//for(int y=cur+1;y<=n;y++){
					//right=y;
					//if(r[y]<=target) break;
				//}
				
				int lft=cur;
				int rgt=n;
				int mid;
				int best=rgt;
				
				while(lft<=rgt){
					//show2(lft,lft,rgt,rgt);
					mid=(lft+rgt)/2;
					if(st2.query(cur,mid)<=target){
						best=mid;
						rgt=mid-1;
					}
					else lft=mid+1;
				}
				
				right=best;
				
				
				//modify
				counter+=abs(arr[left-1]-arr[right]);
				counter+=abs(arr[right]-arr[cur]);
				cur=left-1;
				left--;
				amos=false;
			}
			else{
				//venture left
				int target=arr[right+1];
				
				//modify
				//for(int y=cur-1;y>=1;y--){
					//left=y;
					//if(l[y]>target) break;
				//}
				
				int lft=1;
				int rgt=cur;
				int mid;
				int best=lft;
				
				while(lft<=rgt){
					//show2(lft,lft,rgt,rgt);
					mid=(lft+rgt)/2;
					if(st.query(mid,cur)>target){
						best=mid;
						lft=mid+1;
					}
					else rgt=mid-1;
				}
				
				left=best;
				
				
				
				//modify
				counter+=abs(arr[left]-arr[right+1]);
				counter+=abs(arr[left]-arr[cur]);
				cur=right+1;
				right++;
				amos=true;
			}
		}
		if(left!=1){
			counter+=abs(arr[cur]-arr[1]);
		}	
		if(right!=n){
			counter+=abs(arr[cur]-arr[n]);
		}
		cout << counter << "\n";
	}
	
}	
 
/*
10
1 2 3 4 5 6 7 8 9 10
1
2
*/
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	//cin >> t;
	while(t--){
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2392 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 3 ms 2708 KB Output is correct
5 Correct 1 ms 2792 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 2 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2392 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 2 ms 2676 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2392 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 3 ms 2708 KB Output is correct
5 Correct 1 ms 2792 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 2 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2392 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 2 ms 2676 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 60 ms 59508 KB Output is correct
18 Correct 60 ms 59500 KB Output is correct
19 Correct 59 ms 59476 KB Output is correct
20 Correct 59 ms 59484 KB Output is correct
21 Correct 63 ms 59496 KB Output is correct
22 Correct 60 ms 59512 KB Output is correct
23 Correct 63 ms 59648 KB Output is correct
24 Correct 64 ms 59472 KB Output is correct
25 Correct 59 ms 59528 KB Output is correct
26 Correct 61 ms 59512 KB Output is correct
27 Correct 60 ms 59452 KB Output is correct
28 Correct 58 ms 60512 KB Output is correct
29 Correct 59 ms 59492 KB Output is correct
30 Correct 58 ms 59496 KB Output is correct
31 Correct 60 ms 59464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2648 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2520 KB Output is correct
7 Correct 144 ms 61064 KB Output is correct
8 Correct 122 ms 62284 KB Output is correct
9 Correct 124 ms 62372 KB Output is correct
10 Correct 124 ms 62368 KB Output is correct
11 Correct 126 ms 62380 KB Output is correct
12 Correct 134 ms 63328 KB Output is correct
13 Correct 39 ms 6372 KB Output is correct
14 Correct 25 ms 3408 KB Output is correct
15 Correct 131 ms 63052 KB Output is correct
16 Correct 132 ms 64516 KB Output is correct
17 Correct 144 ms 64328 KB Output is correct
18 Correct 36 ms 6328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2392 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 3 ms 2708 KB Output is correct
5 Correct 1 ms 2792 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 2 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2392 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 2 ms 2676 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 60 ms 59508 KB Output is correct
18 Correct 60 ms 59500 KB Output is correct
19 Correct 59 ms 59476 KB Output is correct
20 Correct 59 ms 59484 KB Output is correct
21 Correct 63 ms 59496 KB Output is correct
22 Correct 60 ms 59512 KB Output is correct
23 Correct 63 ms 59648 KB Output is correct
24 Correct 64 ms 59472 KB Output is correct
25 Correct 59 ms 59528 KB Output is correct
26 Correct 61 ms 59512 KB Output is correct
27 Correct 60 ms 59452 KB Output is correct
28 Correct 58 ms 60512 KB Output is correct
29 Correct 59 ms 59492 KB Output is correct
30 Correct 58 ms 59496 KB Output is correct
31 Correct 60 ms 59464 KB Output is correct
32 Correct 1 ms 2392 KB Output is correct
33 Correct 1 ms 2648 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 0 ms 2396 KB Output is correct
36 Correct 1 ms 2396 KB Output is correct
37 Correct 2 ms 2520 KB Output is correct
38 Correct 144 ms 61064 KB Output is correct
39 Correct 122 ms 62284 KB Output is correct
40 Correct 124 ms 62372 KB Output is correct
41 Correct 124 ms 62368 KB Output is correct
42 Correct 126 ms 62380 KB Output is correct
43 Correct 134 ms 63328 KB Output is correct
44 Correct 39 ms 6372 KB Output is correct
45 Correct 25 ms 3408 KB Output is correct
46 Correct 131 ms 63052 KB Output is correct
47 Correct 132 ms 64516 KB Output is correct
48 Correct 144 ms 64328 KB Output is correct
49 Correct 36 ms 6328 KB Output is correct
50 Correct 347 ms 65512 KB Output is correct
51 Correct 304 ms 65548 KB Output is correct
52 Correct 272 ms 65432 KB Output is correct
53 Correct 191 ms 65352 KB Output is correct
54 Correct 201 ms 66548 KB Output is correct
55 Correct 201 ms 65596 KB Output is correct
56 Correct 114 ms 65464 KB Output is correct
57 Correct 118 ms 65352 KB Output is correct
58 Correct 116 ms 65352 KB Output is correct