This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define endl '\n'
const int MAX_N = 110;
int n, q, a[MAX_N];
void input()
{
std::cin >> n;
for (int i = 1; i <= n; i ++)
std::cin >> a[i];
}
std::vector < std::pair < int, int > > fun;
void precompute_pairs()
{
std::stack < int > st;
for (int i = 1; i <= n; i ++)
{
while(!st.empty() && a[st.top()] >= a[i])
{
fun.push_back({st.top(), i});
st.pop();
}
st.push(i);
}
while(!st.empty())
st.pop();
for (int i = 1; i <= n; i ++)
{
while(!st.empty() && a[st.top()] <= a[i])
{
fun.push_back({st.top(), i});
st.pop();
}
st.push(i);
}
}
int fp[MAX_N];
void single_query(int l, int r)
{
fp[r + 1] = 0;
for (int i = r; i >= l; i --)
{
fp[i] = std::max(fp[i + 1], a[i]);
}
int res = 0;
for (std::pair < int, int > cur : fun)
{
int id = 2 * cur.second - cur.first;
if (id > r || cur.first < l)
continue;
//std::cout << "query " << cur.first << " " << cur.second << " " << id << endl;
res = std::max(res, a[cur.first] + a[cur.second] + fp[id]);
}
/*for (int i = l; i <= r; i ++)
for (int f = i + 1; f <= r; f ++)
for (int j = 2 * f - i; j <= r; j ++)
{
if (a[i] + a[j] + a[f] == 208)
std::cout << "winner " << i << " " << f << " " << j << endl;
}*/
std::cout << res << endl;
}
void answer_queries()
{
std::cin >> q;
for (int i = 1; i <= q; i ++)
{
int l, r;
std::cin >> l >> r;
single_query(l, r);
}
}
void solve()
{
input();
precompute_pairs();
answer_queries();
}
void speed()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
}
int main()
{
speed();
solve();
return 0;
}
/*
15
12 96 100 61 54 66 37 34 58 21 21 1 13 50 81
1
4 15
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |