#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define print(x) cout << #x <<" = " << x << endl
#define pprint(x) cout << #x <<" = (" << x.first <<" , " << x.second <<")\n"
#define ceil(a, b) ((a + b - 1) / b)
#define all(x) x.begin(), x.end()
#define INF 1000000000000000000
#define MAXN 1000005
#define MOD 1000000007
#define eps (1e-9)
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#define get(L, R) ((L + R) | (L != R))
using namespace std;
struct node {
int A , B, C;
};
struct query {
int L, R, id;
bool operator<(query b) {
return L > b.L;
}
};
int n, q;
vector<query> qry;
vector<int> a, ans;
vector<vector<int>> ps;
vector<node> seg;
void build() {
stack<int> st;
rep(i, 1, n) {
while(st.size() && a[st.top()] <= a[i]) {
ps[st.top()].push_back(i), st.pop();
}
if(st.size()) ps[st.top()].push_back(i);
st.push(i);
}
// rep(i, 1, n) {
// print(i), puts("");
// for(auto j : ps[i]) print(j);
// puts("\n\n");
// }
return;
}
void pull(int L, int R) {
int mid = (L + R) / 2, nd = get(L, R);
seg[nd].A = max(seg[get(L, mid)].A, seg[get(mid + 1,R)].A);
seg[nd].C = max({seg[get(L, mid)].C, seg[get(mid + 1,R)].C, seg[nd].A + seg[nd].B });
return;
}
void build1(int L, int R) {
if(L == R) seg[get(L, R)].A = seg[get(L,R)].C = a[L] ;
else {
int mid = (L + R) / 2;
build1(L, mid), build1(mid + 1, R);
pull(L, R);
}
return;
}
void modify(int L, int R, int l, int r, int val) {
// pprint(pii(L,R)), print(val);
if(l > R || r < L) return;
if(l <= L && r >= R) {
// print(val);
seg[get(L, R)].B = max(seg[get(L, R)].B, val);
seg[get(L, R)].C = max(seg[get(L,R)].C, seg[get(L, R)].B + seg[get(L,R)].A);
// print(seg[get(L,R)].C);
}
else {
int mid = (L + R) / 2;
modify(L, mid, l, r, val), modify(mid + 1, R, l, r, val);
pull(L,R);
}
return;
}
pii query(int L, int R, int l, int r) {
if(l > R || r < L) return {0, 0};
// pprint(pii(L,R));
if(l <= L && r >= R) {
// pprint(pii(seg[get(L,R)].A, seg[get(L,R)].C));
return pii(seg[get(L,R)].A, seg[get(L,R)].C);
}
else {
int mid = (L + R) / 2;
pii a = query(L, mid, l, r), b = query(mid + 1, R, l, r);
int ans = max(max(a.first, b.first) + seg[get(L,R)].B, max(a.second, b.second));
pii rr = pii(max(a.first, b.first), ans);
// pprint(pii(L,R)), pprint(rr);
return rr;
}
}
void proc(int id) {
for(int i : ps[id]) {
int rr = 2 * i - id;
if(rr > n) continue;
modify(1, n, rr, n, a[i] + a[id]);
}
return;
}
void solve() {
build(), build1(1, n), sort(all(qry));
int cur = n;
for(auto i : qry) {
while(cur >= i.L) proc(cur--);
// pprint(pii(i.L, i.R));
pii aa = query(1, n, i.L + 2, i.R);
ans[i.id] = aa.second;
// puts("\n\n");
}
return;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n, seg.resize(2 * n + 2);
a.assign(n + 1 ,0), ps.assign(n + 1, vector<int>(0));
rep(i, 1, n) cin >> a[i];
cin >> q, qry.resize(q), ans.resize(q);
rep(i, 0, q-1) cin >> qry[i].L >> qry[i].R, qry[i].id = i;
solve();
rep(i, 0, q-1) cout << ans[i] <<"\n";
return 0;
}
Compilation message
jumps.cpp:3: warning: ignoring #pragma loop [-Wunknown-pragmas]
3 | #pragma loop-opt(on)
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
367 ms |
26468 KB |
Output is correct |
12 |
Correct |
371 ms |
26196 KB |
Output is correct |
13 |
Correct |
370 ms |
26304 KB |
Output is correct |
14 |
Correct |
369 ms |
26308 KB |
Output is correct |
15 |
Correct |
381 ms |
26468 KB |
Output is correct |
16 |
Correct |
383 ms |
25828 KB |
Output is correct |
17 |
Correct |
370 ms |
25700 KB |
Output is correct |
18 |
Correct |
375 ms |
25700 KB |
Output is correct |
19 |
Correct |
384 ms |
26340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
25188 KB |
Output is correct |
2 |
Correct |
103 ms |
24056 KB |
Output is correct |
3 |
Correct |
109 ms |
25700 KB |
Output is correct |
4 |
Correct |
178 ms |
25188 KB |
Output is correct |
5 |
Correct |
180 ms |
25188 KB |
Output is correct |
6 |
Correct |
176 ms |
24676 KB |
Output is correct |
7 |
Correct |
175 ms |
24548 KB |
Output is correct |
8 |
Correct |
174 ms |
24420 KB |
Output is correct |
9 |
Correct |
176 ms |
24808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
367 ms |
26468 KB |
Output is correct |
12 |
Correct |
371 ms |
26196 KB |
Output is correct |
13 |
Correct |
370 ms |
26304 KB |
Output is correct |
14 |
Correct |
369 ms |
26308 KB |
Output is correct |
15 |
Correct |
381 ms |
26468 KB |
Output is correct |
16 |
Correct |
383 ms |
25828 KB |
Output is correct |
17 |
Correct |
370 ms |
25700 KB |
Output is correct |
18 |
Correct |
375 ms |
25700 KB |
Output is correct |
19 |
Correct |
384 ms |
26340 KB |
Output is correct |
20 |
Correct |
191 ms |
25188 KB |
Output is correct |
21 |
Correct |
103 ms |
24056 KB |
Output is correct |
22 |
Correct |
109 ms |
25700 KB |
Output is correct |
23 |
Correct |
178 ms |
25188 KB |
Output is correct |
24 |
Correct |
180 ms |
25188 KB |
Output is correct |
25 |
Correct |
176 ms |
24676 KB |
Output is correct |
26 |
Correct |
175 ms |
24548 KB |
Output is correct |
27 |
Correct |
174 ms |
24420 KB |
Output is correct |
28 |
Correct |
176 ms |
24808 KB |
Output is correct |
29 |
Correct |
1196 ms |
90084 KB |
Output is correct |
30 |
Correct |
991 ms |
86756 KB |
Output is correct |
31 |
Correct |
987 ms |
90912 KB |
Output is correct |
32 |
Correct |
1199 ms |
89896 KB |
Output is correct |
33 |
Correct |
1193 ms |
89956 KB |
Output is correct |
34 |
Correct |
1191 ms |
87628 KB |
Output is correct |
35 |
Correct |
1173 ms |
87456 KB |
Output is correct |
36 |
Correct |
1175 ms |
87268 KB |
Output is correct |
37 |
Correct |
1191 ms |
88840 KB |
Output is correct |
38 |
Correct |
898 ms |
89956 KB |
Output is correct |
39 |
Correct |
906 ms |
89956 KB |
Output is correct |
40 |
Correct |
894 ms |
86756 KB |
Output is correct |
41 |
Correct |
910 ms |
86244 KB |
Output is correct |
42 |
Correct |
885 ms |
86120 KB |
Output is correct |
43 |
Correct |
891 ms |
87908 KB |
Output is correct |
44 |
Correct |
933 ms |
89868 KB |
Output is correct |
45 |
Correct |
939 ms |
90048 KB |
Output is correct |
46 |
Correct |
920 ms |
87012 KB |
Output is correct |
47 |
Correct |
923 ms |
86628 KB |
Output is correct |
48 |
Correct |
911 ms |
86376 KB |
Output is correct |
49 |
Correct |
947 ms |
88420 KB |
Output is correct |
50 |
Correct |
1002 ms |
90104 KB |
Output is correct |
51 |
Correct |
992 ms |
90092 KB |
Output is correct |
52 |
Correct |
980 ms |
87560 KB |
Output is correct |
53 |
Correct |
969 ms |
87268 KB |
Output is correct |
54 |
Correct |
970 ms |
87268 KB |
Output is correct |
55 |
Correct |
979 ms |
88932 KB |
Output is correct |