#include <bits/stdc++.h>
using namespace std;
const int Nmax = 2e5 + 5;
const int inf = 1e9;
vector<pair<int,int>> v;
int a[Nmax], st[Nmax];
int n, L[Nmax], R[Nmax];
void fnd()
{
int i, nr = 0;
st[0] = 0;
a[0] = inf;
for(i=1; i<=n; ++i)
{
while(a[st[nr]] <= a[i])
{
v.push_back({st[nr], i});
--nr;
}
v.push_back({st[nr], i});
st[++nr] = i;
}
}
int best(int x, int y)
{
int i; int ans = 0;
for(i=x; i<=y; ++i) ans = max(ans, a[i]);
return ans;
}
int main()
{
// freopen("triple.in", "r", stdin);
cin.tie(0); cin.sync_with_stdio(false);
cin >> n;
int i, q;
for(i=1; i<=n; ++i) cin >> a[i];
cin >> q;
for(i=1; i<=q; ++i) cin >> L[i] >> R[i];
fnd();
vector<int> best(n+1);
for(i=n; i; --i) best[i] = max(best[i+1], a[i]);
for(i=1; i<=q; ++i)
{
int ans = 0;
for(auto el : v)
{
int x, y, z;
tie(x, y) = el;
z = 2 * y - x;
if(x < L[i] || z > R[i]) continue;
ans = max(ans, a[x] + a[y] + best[z]);
}
cout << ans << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
3 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
3 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
7144 KB |
Output is correct |
2 |
Correct |
38 ms |
7144 KB |
Output is correct |
3 |
Correct |
35 ms |
5996 KB |
Output is correct |
4 |
Correct |
41 ms |
7144 KB |
Output is correct |
5 |
Correct |
40 ms |
7144 KB |
Output is correct |
6 |
Correct |
33 ms |
6504 KB |
Output is correct |
7 |
Correct |
32 ms |
6376 KB |
Output is correct |
8 |
Correct |
32 ms |
6376 KB |
Output is correct |
9 |
Correct |
36 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
3 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |