#include <bits/stdc++.h>
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Triple Jump"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 5e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rng);
}
int n, Q;
int q[maxn], top;
struct dl { int l, r, id; } qr[maxn];
vector<pair<int, int>> P;
ll a[maxn], nex[2 * maxn], ans[maxn];
struct IT {
struct Node {
ll Le, Ri, All;
} st[4 * maxn];
Node cb(Node x, Node y) {
Node P;
P.Le = max(x.Le, y.Le);
P.Ri = max(x.Ri, y.Ri);
P.All = max({x.All, y.All, x.Le + y.Ri});
return P;
}
void update(int x, int val, int id = 1, int l = 1, int r = n)
{
if(l > x || r < x) return ;
if(l == r) {
st[id].Le = val, st[id].Ri = a[l];
st[id].All = st[id].Le + st[id].Ri;
return ;
}
int mid = (l + r) >> 1;
update(x, val, _left), update(x, val, _right);
st[id] = cb(st[id * 2], st[id * 2 + 1]);
}
Node get(int u, int v, int id = 1, int l = 1, int r = n)
{
if(l > v || r < u) return {0, 0, 0};
if(u <= l && r <= v) return st[id];
int mid = (l + r) >> 1;
return cb(get(u, v, _left), get(u, v, _right));
}
} St;
void solve()
{
cin >> n ;
fi(i, 1, n) cin >> a[i];
cin >> Q;
fi(i, 1, Q) cin >> qr[i].l >> qr[i].r, qr[i].id = i;
//
fi(i, 1, n) {
while(top && a[i] >= a[q[top]]) P.push_back({q[top], i}), --top;
if(top) P.push_back({q[top], i}); q[++top] = i;
}
sort(all(P), [](pair<int, int> x, pair<int, int> y) { return x.first < y.first; } );
for(int i = P.size() - 1; i >= 0; -- i) {
int u, v, id, val; tie(u, v) = P[i];
id = v * 2 - u, val = St.get(id, id).Le;
nex[i] = val, St.update(id, max(1ll * val, a[u] + a[v]));
}
sort(qr + 1, qr + 1 + Q, [](dl x, dl y) { return x.l < y.l; } );
int j = 0;
fi(i, 1, Q) {
while(P[j].first < qr[i].l) {
St.update(2 * P[j].second - P[j].first, nex[j]);
++j;
}
ans[qr[i].id] = St.get(qr[i].l, qr[i].r).All;
}
fi(i, 1, Q) cout << ans[i] << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message
jumps.cpp: In function 'void solve()':
jumps.cpp:84:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
84 | if(top) P.push_back({q[top], i}); q[++top] = i;
| ^~
jumps.cpp:84:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
84 | if(top) P.push_back({q[top], i}); q[++top] = i;
| ^
jumps.cpp: In function 'int main()':
jumps.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8536 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8536 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
207 ms |
27204 KB |
Output is correct |
12 |
Correct |
208 ms |
26972 KB |
Output is correct |
13 |
Correct |
204 ms |
26960 KB |
Output is correct |
14 |
Correct |
206 ms |
27292 KB |
Output is correct |
15 |
Correct |
204 ms |
27188 KB |
Output is correct |
16 |
Correct |
210 ms |
26436 KB |
Output is correct |
17 |
Correct |
210 ms |
26452 KB |
Output is correct |
18 |
Correct |
212 ms |
26308 KB |
Output is correct |
19 |
Correct |
205 ms |
26932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
32052 KB |
Output is correct |
2 |
Correct |
82 ms |
28784 KB |
Output is correct |
3 |
Correct |
79 ms |
29128 KB |
Output is correct |
4 |
Correct |
133 ms |
31940 KB |
Output is correct |
5 |
Correct |
134 ms |
32932 KB |
Output is correct |
6 |
Correct |
142 ms |
31480 KB |
Output is correct |
7 |
Correct |
139 ms |
32068 KB |
Output is correct |
8 |
Correct |
152 ms |
32200 KB |
Output is correct |
9 |
Correct |
133 ms |
32348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8536 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
207 ms |
27204 KB |
Output is correct |
12 |
Correct |
208 ms |
26972 KB |
Output is correct |
13 |
Correct |
204 ms |
26960 KB |
Output is correct |
14 |
Correct |
206 ms |
27292 KB |
Output is correct |
15 |
Correct |
204 ms |
27188 KB |
Output is correct |
16 |
Correct |
210 ms |
26436 KB |
Output is correct |
17 |
Correct |
210 ms |
26452 KB |
Output is correct |
18 |
Correct |
212 ms |
26308 KB |
Output is correct |
19 |
Correct |
205 ms |
26932 KB |
Output is correct |
20 |
Correct |
139 ms |
32052 KB |
Output is correct |
21 |
Correct |
82 ms |
28784 KB |
Output is correct |
22 |
Correct |
79 ms |
29128 KB |
Output is correct |
23 |
Correct |
133 ms |
31940 KB |
Output is correct |
24 |
Correct |
134 ms |
32932 KB |
Output is correct |
25 |
Correct |
142 ms |
31480 KB |
Output is correct |
26 |
Correct |
139 ms |
32068 KB |
Output is correct |
27 |
Correct |
152 ms |
32200 KB |
Output is correct |
28 |
Correct |
133 ms |
32348 KB |
Output is correct |
29 |
Correct |
830 ms |
73752 KB |
Output is correct |
30 |
Correct |
625 ms |
67520 KB |
Output is correct |
31 |
Correct |
617 ms |
68028 KB |
Output is correct |
32 |
Correct |
822 ms |
73492 KB |
Output is correct |
33 |
Correct |
810 ms |
73504 KB |
Output is correct |
34 |
Correct |
819 ms |
71508 KB |
Output is correct |
35 |
Correct |
848 ms |
70852 KB |
Output is correct |
36 |
Correct |
829 ms |
70832 KB |
Output is correct |
37 |
Correct |
857 ms |
72628 KB |
Output is correct |
38 |
Correct |
753 ms |
73572 KB |
Output is correct |
39 |
Correct |
719 ms |
73652 KB |
Output is correct |
40 |
Correct |
704 ms |
70324 KB |
Output is correct |
41 |
Correct |
705 ms |
70076 KB |
Output is correct |
42 |
Correct |
697 ms |
69808 KB |
Output is correct |
43 |
Correct |
721 ms |
71668 KB |
Output is correct |
44 |
Correct |
717 ms |
73656 KB |
Output is correct |
45 |
Correct |
736 ms |
73508 KB |
Output is correct |
46 |
Correct |
714 ms |
70356 KB |
Output is correct |
47 |
Correct |
718 ms |
70100 KB |
Output is correct |
48 |
Correct |
721 ms |
70148 KB |
Output is correct |
49 |
Correct |
709 ms |
72096 KB |
Output is correct |
50 |
Correct |
751 ms |
73560 KB |
Output is correct |
51 |
Correct |
788 ms |
73652 KB |
Output is correct |
52 |
Correct |
749 ms |
71148 KB |
Output is correct |
53 |
Correct |
746 ms |
71012 KB |
Output is correct |
54 |
Correct |
741 ms |
70896 KB |
Output is correct |
55 |
Correct |
753 ms |
72740 KB |
Output is correct |