답안 #1013040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1013040 2024-07-03T06:23:13 Z aliarapov 3단 점프 (JOI19_jumps) C++17
0 / 100
51 ms 34908 KB
#include <bits/stdc++.h>
 
#define int long long
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
 
using namespace std;
 
const int INF = 1e9;
const int mod = 998244353;
const int N = 200005;
int sp[N][20];
 
int get(int l, int r){
	int lg = __lg(r - l + 1);
	return max(sp[l][lg], sp[r - (1ll << lg) + 1][lg]);
}
 
void solve(){
	int n;
	cin >> n;
	vector<int> a(n);
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	for(int i = 0; i < n; i++){
		sp[i][0] = a[i];
	}
	for(int i = 1; i < 20; i++){
		for(int j = 0; j < n; j++){
			sp[j][i] = max(sp[j][i - 1], sp[min(n - 1, j + (1ll << (i - 1)))][i - 1]);
		}
	}
	int q;
	cin >> q;
	while(q--){
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		int mx = get(l, r), pos = l;
		for(int i = r; i >= l; i--){
			if(a[i] == mx){
				pos = i;
				break;
			}
		}
		int ans = 0;
		for(int i = l; i < pos; i++){
			if(i != l) ans = max(ans, a[pos] + a[i] + get(max(l, i - (pos - i)), i - 1));
			
			if(pos + (pos - i) <= r) ans = max(ans, a[pos] + a[i] + get(pos + (pos - i), r));
		}
		for(int i = pos + 1; i < r; i++){
			if(i + (i - pos) <= r) ans = max(ans, a[pos] + a[i] + get(i + (i - pos), r));
			else break;
		}
		
		for(int i = l; i <= r; i++){
			if(a[i] == mx){
				pos = i;
				break;
			}
		}
		for(int i = l; i < pos; i++){
			if(i != l) ans = max(ans, a[pos] + a[i] + get(max(l, i - (pos - i)), i - 1));
			
			if(pos + (pos - i) <= r) ans = max(ans, a[pos] + a[i] + get(pos + (pos - i), r));
		}
		for(int i = pos + 1; i < r; i++){
			if(i + (i - pos) <= r) ans = max(ans, a[pos] + a[i] + get(i + (i - pos), r));
			else break;
		}
		
		cout << ans << '\n';
	}
}
 
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int tt = 1;
    //cin >> tt;
    while(tt--){
		solve();
	}
}

Compilation message

jumps.cpp:79:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   79 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 34900 KB Output is correct
2 Correct 42 ms 34908 KB Output is correct
3 Correct 40 ms 34896 KB Output is correct
4 Correct 51 ms 34896 KB Output is correct
5 Incorrect 41 ms 34896 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -