#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 Node {
int maxRouge, maxBleu, maxSomme;
void up() { chmax(maxSomme, maxRouge+maxBleu); }
};
Node op(Node a, Node b) {
return {
max(a.maxRouge, b.maxRouge),
max(a.maxBleu, b.maxBleu),
max(max(a.maxSomme, b.maxSomme), a.maxRouge + b.maxBleu)
};
}
Node e = {0, 0, 0};
const int ROUGE = 0, BLEU = 1;
struct Segtree {
vector<Node> tree;
int N;
Segtree(int _N) : N(_N) {
tree.assign(2*N, e);
}
int query_include(int L, int R) {
L += N, R += N+1;
Node resl = e, resr = e;
while (L < R) {
if (L & 1) resl = op(resl, tree[L++]);
if (R & 1) resr = op(tree[--R], resr);
L /= 2, R /= 2;
}
return op(resl, resr).maxSomme;
}
void prop(int i, int coul, int x) {
i += N;
assert(0 <= i && i < 2*N);
chmax(coul == ROUGE ? tree[i].maxRouge : tree[i].maxBleu, x);
tree[i].up();
while (i >= 1) {
i /= 2;
tree[i] = op(tree[2*i], tree[2*i+1]);
}
}
};
struct Req {
int l, r, i;
};
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<vector<Req>> req(N);
vector<int> ansReq(Q);
rep(i, 0, Q) {
int l, r;
cin >> l >> r;
--l, --r;
req[l].push_back({l, r, i});
}
vector<int> stk;
vector<vi> cand(N);
rep(B, 0, N) {
while (!stk.empty()) {
int A = stk.back();
cand[A].push_back(B);
if (val[A] >= val[B]) break;
stk.pop_back();
}
stk.push_back(B);
}
Segtree st(N+1);
vector<pii> ptRouge, ptBleu;
for (int i = N-1; i >= 0; --i) {
//ptBleu.emplace_back(i, val[i]);
st.prop(i, BLEU, val[i]);
{
int A = i;
for (int B : cand[i]) {
int suffC = min(B + (B-A), N);
//ptRouge.emplace_back(suffC, val[A]+val[B]);
st.prop(suffC, ROUGE, val[A]+val[B]);
}
}
for (Req rq : req[i]) {
ansReq[rq.i] = st.query_include(rq.l, rq.r);
}
}
rep(i, 0, Q) {
cout << ansReq[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
150 ms |
29260 KB |
Output is correct |
12 |
Correct |
147 ms |
33828 KB |
Output is correct |
13 |
Correct |
147 ms |
34140 KB |
Output is correct |
14 |
Correct |
147 ms |
34064 KB |
Output is correct |
15 |
Correct |
154 ms |
34104 KB |
Output is correct |
16 |
Correct |
155 ms |
33380 KB |
Output is correct |
17 |
Correct |
145 ms |
33372 KB |
Output is correct |
18 |
Correct |
146 ms |
33260 KB |
Output is correct |
19 |
Correct |
159 ms |
33836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
28216 KB |
Output is correct |
2 |
Correct |
59 ms |
28624 KB |
Output is correct |
3 |
Correct |
60 ms |
30228 KB |
Output is correct |
4 |
Correct |
88 ms |
29780 KB |
Output is correct |
5 |
Correct |
87 ms |
29896 KB |
Output is correct |
6 |
Correct |
87 ms |
29244 KB |
Output is correct |
7 |
Correct |
87 ms |
29112 KB |
Output is correct |
8 |
Correct |
84 ms |
29132 KB |
Output is correct |
9 |
Correct |
86 ms |
29368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
150 ms |
29260 KB |
Output is correct |
12 |
Correct |
147 ms |
33828 KB |
Output is correct |
13 |
Correct |
147 ms |
34140 KB |
Output is correct |
14 |
Correct |
147 ms |
34064 KB |
Output is correct |
15 |
Correct |
154 ms |
34104 KB |
Output is correct |
16 |
Correct |
155 ms |
33380 KB |
Output is correct |
17 |
Correct |
145 ms |
33372 KB |
Output is correct |
18 |
Correct |
146 ms |
33260 KB |
Output is correct |
19 |
Correct |
159 ms |
33836 KB |
Output is correct |
20 |
Correct |
101 ms |
28216 KB |
Output is correct |
21 |
Correct |
59 ms |
28624 KB |
Output is correct |
22 |
Correct |
60 ms |
30228 KB |
Output is correct |
23 |
Correct |
88 ms |
29780 KB |
Output is correct |
24 |
Correct |
87 ms |
29896 KB |
Output is correct |
25 |
Correct |
87 ms |
29244 KB |
Output is correct |
26 |
Correct |
87 ms |
29112 KB |
Output is correct |
27 |
Correct |
84 ms |
29132 KB |
Output is correct |
28 |
Correct |
86 ms |
29368 KB |
Output is correct |
29 |
Correct |
591 ms |
105996 KB |
Output is correct |
30 |
Correct |
493 ms |
102900 KB |
Output is correct |
31 |
Correct |
530 ms |
106744 KB |
Output is correct |
32 |
Correct |
570 ms |
106000 KB |
Output is correct |
33 |
Correct |
582 ms |
106024 KB |
Output is correct |
34 |
Correct |
594 ms |
103744 KB |
Output is correct |
35 |
Correct |
562 ms |
103360 KB |
Output is correct |
36 |
Correct |
588 ms |
103356 KB |
Output is correct |
37 |
Correct |
549 ms |
104824 KB |
Output is correct |
38 |
Correct |
375 ms |
105472 KB |
Output is correct |
39 |
Correct |
384 ms |
105392 KB |
Output is correct |
40 |
Correct |
372 ms |
102056 KB |
Output is correct |
41 |
Correct |
362 ms |
101608 KB |
Output is correct |
42 |
Correct |
367 ms |
101652 KB |
Output is correct |
43 |
Correct |
370 ms |
103292 KB |
Output is correct |
44 |
Correct |
421 ms |
105500 KB |
Output is correct |
45 |
Correct |
421 ms |
105448 KB |
Output is correct |
46 |
Correct |
407 ms |
102348 KB |
Output is correct |
47 |
Correct |
425 ms |
101868 KB |
Output is correct |
48 |
Correct |
413 ms |
101916 KB |
Output is correct |
49 |
Correct |
412 ms |
103924 KB |
Output is correct |
50 |
Correct |
484 ms |
105684 KB |
Output is correct |
51 |
Correct |
502 ms |
105712 KB |
Output is correct |
52 |
Correct |
464 ms |
103152 KB |
Output is correct |
53 |
Correct |
455 ms |
102796 KB |
Output is correct |
54 |
Correct |
463 ms |
102900 KB |
Output is correct |
55 |
Correct |
472 ms |
104488 KB |
Output is correct |