#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();
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, R[i]));
}
cout << ans << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
380 KB |
Output is correct |
11 |
Execution timed out |
4026 ms |
8184 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4016 ms |
7144 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
380 KB |
Output is correct |
11 |
Execution timed out |
4026 ms |
8184 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |