#include <bits/stdc++.h>
#define Y8o "Triple Jump"
#define maxn 500005
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define f first
#define s second
using namespace std;
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);
}
void iof() {
if(fopen(Y8o".inp", "r"))
{
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
int n, Q, q[maxn], nex[maxn];
ll a[maxn], ans[maxn];
vector<pair<int, int>> P;
struct dl {
int f, s, id;
bool operator < (const dl & other)
const {
if(f == other.f) return s < other.s;
return f < other.f;
}
} qr[maxn];
struct SEGTREE {
struct dl {
int ma, le, ri;
} st[4 * maxn];
dl cb(dl x, dl y)
{
dl best;
best.le = max(x.le, y.le);
best.ri = max(x.ri, y.ri);
best.ma = max({ x.ma, y.ma, x.le + y.ri });
return best;
}
void update(int i, ll val, int id = 1, int l = 1, int r = n) {
if(i < l || r < i) return ;
if(l == r) {
st[id] = {val + a[l], val, a[l]};
return ;
}
int mid = (l + r) >> 1;
update(i, val, id * 2, l, mid), update(i, val, id * 2 + 1, mid + 1, r);
st[id] = cb(st[id * 2], st[id * 2 + 1]);
}
dl get(int u, int v, int id = 1, int l = 1, int r = n) {
if(v < l || r < u) return {0, 0, 0};
if(u <= l && r <= v) return st[id];
int mid = (l + r) >> 1;
return cb( get(u, v, id * 2, l, mid), get(u, v, id * 2 + 1, mid + 1, r) );
}
} ST;
void solve()
{
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
int d = 1, c = 0;
for(int i = 1; i <= n; i ++)
{
while(d <= c && a[i] >= a[q[c]]) P.push_back({ q[c], i }), c --;
if(c) P.push_back({ q[c], i });
q[++ c] = i;
}
sort(P.begin(), P.end());
//
for(int i = P.size() - 1; i >= 0; i --)
{
int u, v, val, id; tie(u, v) = P[i];
id = 2 * v - u, val = ST.get(id, id).le;
nex[i] = val, ST.update( id, max(1ll * val, a[u] + a[v]) );
}
cin >> Q;
for(int i = 1; i <= Q; i ++) cin >> qr[i].f >> qr[i].s, qr[i].id = i;
sort(qr + 1, qr + Q + 1);
int j = 0;
for(int i = 1; i <= Q; i ++)
{
int l = qr[i].f, r = qr[i].s, id = qr[i].id;
while(l > P[j].f) {
int u, v; tie(u, v) = P[j];
ST.update(2 * v - u, nex[j]);
j ++;
}
ans[id] = ST.get(l, r).ma ;
}
for(int i = 1; i <= Q; i ++) cout << ans[i] << '\n';
}
int main()
{
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
ctime();
return 0;
}
Compilation message
jumps.cpp: In member function 'void SEGTREE::update(int, long long int, int, int, int)':
jumps.cpp:58:27: warning: narrowing conversion of '(val + a[l])' from 'long long int' to 'int' [-Wnarrowing]
58 | st[id] = {val + a[l], val, a[l]};
| ~~~~^~~~~~
jumps.cpp:58:35: warning: narrowing conversion of 'val' from 'long long int' to 'int' [-Wnarrowing]
58 | st[id] = {val + a[l], val, a[l]};
| ^~~
jumps.cpp:58:43: warning: narrowing conversion of 'a[l]' from 'long long int' to 'int' [-Wnarrowing]
58 | st[id] = {val + a[l], val, a[l]};
| ~~~^
jumps.cpp: In function 'void iof()':
jumps.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8664 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8664 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
227 ms |
24928 KB |
Output is correct |
12 |
Correct |
227 ms |
24692 KB |
Output is correct |
13 |
Correct |
225 ms |
24632 KB |
Output is correct |
14 |
Correct |
234 ms |
25040 KB |
Output is correct |
15 |
Correct |
224 ms |
24916 KB |
Output is correct |
16 |
Correct |
229 ms |
24156 KB |
Output is correct |
17 |
Correct |
228 ms |
24028 KB |
Output is correct |
18 |
Correct |
233 ms |
24016 KB |
Output is correct |
19 |
Correct |
236 ms |
24656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
19920 KB |
Output is correct |
2 |
Correct |
77 ms |
18108 KB |
Output is correct |
3 |
Correct |
76 ms |
18888 KB |
Output is correct |
4 |
Correct |
143 ms |
19804 KB |
Output is correct |
5 |
Correct |
165 ms |
20156 KB |
Output is correct |
6 |
Correct |
139 ms |
18928 KB |
Output is correct |
7 |
Correct |
141 ms |
19092 KB |
Output is correct |
8 |
Correct |
143 ms |
18964 KB |
Output is correct |
9 |
Correct |
140 ms |
19388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8664 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
227 ms |
24928 KB |
Output is correct |
12 |
Correct |
227 ms |
24692 KB |
Output is correct |
13 |
Correct |
225 ms |
24632 KB |
Output is correct |
14 |
Correct |
234 ms |
25040 KB |
Output is correct |
15 |
Correct |
224 ms |
24916 KB |
Output is correct |
16 |
Correct |
229 ms |
24156 KB |
Output is correct |
17 |
Correct |
228 ms |
24028 KB |
Output is correct |
18 |
Correct |
233 ms |
24016 KB |
Output is correct |
19 |
Correct |
236 ms |
24656 KB |
Output is correct |
20 |
Correct |
153 ms |
19920 KB |
Output is correct |
21 |
Correct |
77 ms |
18108 KB |
Output is correct |
22 |
Correct |
76 ms |
18888 KB |
Output is correct |
23 |
Correct |
143 ms |
19804 KB |
Output is correct |
24 |
Correct |
165 ms |
20156 KB |
Output is correct |
25 |
Correct |
139 ms |
18928 KB |
Output is correct |
26 |
Correct |
141 ms |
19092 KB |
Output is correct |
27 |
Correct |
143 ms |
18964 KB |
Output is correct |
28 |
Correct |
140 ms |
19388 KB |
Output is correct |
29 |
Correct |
877 ms |
54908 KB |
Output is correct |
30 |
Correct |
608 ms |
49220 KB |
Output is correct |
31 |
Correct |
686 ms |
51004 KB |
Output is correct |
32 |
Correct |
854 ms |
54904 KB |
Output is correct |
33 |
Correct |
861 ms |
55016 KB |
Output is correct |
34 |
Correct |
823 ms |
52764 KB |
Output is correct |
35 |
Correct |
837 ms |
52544 KB |
Output is correct |
36 |
Correct |
881 ms |
52628 KB |
Output is correct |
37 |
Correct |
852 ms |
53884 KB |
Output is correct |
38 |
Correct |
772 ms |
55032 KB |
Output is correct |
39 |
Correct |
765 ms |
54876 KB |
Output is correct |
40 |
Correct |
772 ms |
51888 KB |
Output is correct |
41 |
Correct |
741 ms |
51392 KB |
Output is correct |
42 |
Correct |
747 ms |
51124 KB |
Output is correct |
43 |
Correct |
771 ms |
52952 KB |
Output is correct |
44 |
Correct |
767 ms |
54936 KB |
Output is correct |
45 |
Correct |
768 ms |
54936 KB |
Output is correct |
46 |
Correct |
769 ms |
51764 KB |
Output is correct |
47 |
Correct |
772 ms |
51672 KB |
Output is correct |
48 |
Correct |
766 ms |
51580 KB |
Output is correct |
49 |
Correct |
763 ms |
53612 KB |
Output is correct |
50 |
Correct |
785 ms |
55076 KB |
Output is correct |
51 |
Correct |
791 ms |
55020 KB |
Output is correct |
52 |
Correct |
789 ms |
52636 KB |
Output is correct |
53 |
Correct |
774 ms |
52148 KB |
Output is correct |
54 |
Correct |
774 ms |
52084 KB |
Output is correct |
55 |
Correct |
794 ms |
54008 KB |
Output is correct |