#include <bits/stdc++.h>
#define int long long
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
using pii = pair<int, int>;
using vi = vector<int>;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
template <typename val, typename B>
string to_string(pair<val, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
struct Segtree {
#define op max
const int e = -1e18;
vector<int> tree;
int N;
Segtree(vector<int> v) {
N = v.size();
tree.resize(2*N);
for (int i = 2*N-1; i >= 1; --i) {
tree[i] = (i >= N ? v[i-N] : op(tree[2*i], tree[2*i+1]));
}
}
int query_include(int L, int R) {
L += N, R += N+1;
int res = e;
while (L < R) {
if (L & 1) res = op(res, tree[L++]);
if (R & 1) res = op(res, tree[--R]);
L /= 2, R /= 2;
}
return res;
}
#undef op
};
void s3(int N, int Q, vector<int> val, int lq, int rq) {
vector<int> stk;
vector<pii> cand;
rep(B, 0, N) {
while (!stk.empty()) {
int A = stk.back();
cand.emplace_back(A, B);
if (val[A] >= val[B]) break;
stk.pop_back();
}
stk.push_back(B);
}
Segtree st(val);
int ans = -1e18;
for (auto [A, B] : cand) if (lq <= A) {
int suffC = B + (B-A);
chmax(ans, val[A] + val[B] + st.query_include(suffC, rq));
}
cout << ans << '\n';
}
void s2(int N, int Q, vector<int> val, vector<pii> req) {
vector<vi> dp(N, vi(N));
for (int A = N-1; A >= 0; --A) {
int B = A+1, maxMid = 0;
rep(C, A+2, N) {
while(B < N && B-A <= C-B) {
chmax(maxMid, val[B++]);
}
dp[A][C] = val[A] + maxMid + val[C];
if (A < C) {
chmax(dp[A][C], max(dp[A+1][C], dp[A][C-1]));
}
}
}
for (auto [A, C] : req) {
cout << dp[A][C] << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N; cin >> N;
vector<int> val(N);
for (int &x : val) cin >>x;
int Q; cin >> Q;
vector<pii> req(Q);
for (auto &[l, r] : req) {
cin >> l >> r;
--l, --r;
}
if (N <= 5000) {
s2(N, Q, val, req);
} else {
for (auto [l,r] : req) {
s3(N, Q, val, l, r);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
414 ms |
221724 KB |
Output is correct |
12 |
Correct |
401 ms |
221616 KB |
Output is correct |
13 |
Correct |
269 ms |
221588 KB |
Output is correct |
14 |
Correct |
278 ms |
221608 KB |
Output is correct |
15 |
Correct |
279 ms |
221676 KB |
Output is correct |
16 |
Correct |
294 ms |
220972 KB |
Output is correct |
17 |
Correct |
283 ms |
220932 KB |
Output is correct |
18 |
Correct |
349 ms |
221036 KB |
Output is correct |
19 |
Correct |
278 ms |
221512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
16072 KB |
Output is correct |
2 |
Correct |
39 ms |
13044 KB |
Output is correct |
3 |
Correct |
43 ms |
14524 KB |
Output is correct |
4 |
Correct |
49 ms |
16072 KB |
Output is correct |
5 |
Correct |
51 ms |
16112 KB |
Output is correct |
6 |
Correct |
53 ms |
15416 KB |
Output is correct |
7 |
Correct |
45 ms |
15292 KB |
Output is correct |
8 |
Correct |
56 ms |
15292 KB |
Output is correct |
9 |
Correct |
50 ms |
15616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
414 ms |
221724 KB |
Output is correct |
12 |
Correct |
401 ms |
221616 KB |
Output is correct |
13 |
Correct |
269 ms |
221588 KB |
Output is correct |
14 |
Correct |
278 ms |
221608 KB |
Output is correct |
15 |
Correct |
279 ms |
221676 KB |
Output is correct |
16 |
Correct |
294 ms |
220972 KB |
Output is correct |
17 |
Correct |
283 ms |
220932 KB |
Output is correct |
18 |
Correct |
349 ms |
221036 KB |
Output is correct |
19 |
Correct |
278 ms |
221512 KB |
Output is correct |
20 |
Correct |
64 ms |
16072 KB |
Output is correct |
21 |
Correct |
39 ms |
13044 KB |
Output is correct |
22 |
Correct |
43 ms |
14524 KB |
Output is correct |
23 |
Correct |
49 ms |
16072 KB |
Output is correct |
24 |
Correct |
51 ms |
16112 KB |
Output is correct |
25 |
Correct |
53 ms |
15416 KB |
Output is correct |
26 |
Correct |
45 ms |
15292 KB |
Output is correct |
27 |
Correct |
56 ms |
15292 KB |
Output is correct |
28 |
Correct |
50 ms |
15616 KB |
Output is correct |
29 |
Execution timed out |
4053 ms |
59204 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |