#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a; --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(v) v.begin(), v.end()
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + (ull) rng() % (r - l + 1);}
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} else return false;
}
template<class T>
T Abs(const T &x) {
return (x < 0 ? -x : x);
}
const int N = 5e5 + 5;
const int inf = 1e9;
struct node {
int res, mx, lz;
node() {
res = mx = lz = 0;
}
};
int n, q, a[N];
vector<pii> qrs[N];
vector<pii> candidates;
node st[N << 2];
void push(int id, int x) {
maximize(st[id].lz, x);
maximize(st[id].res, st[id].mx + x);
}
void down(int id) {
if(!st[id].lz) return;
push(id << 1, st[id].lz);
push(id << 1 | 1, st[id].lz);
st[id].lz = 0;
}
void upd_point(int u, int x) {
int id = 1, l = 1, r = n;
while(l ^ r) {
down(id);
int m = l + r >> 1;
if(u > m) id = id << 1 | 1, l = m + 1;
else id = id << 1, r = m;
}
st[id].mx = x;
while(id > 1) id >>= 1, st[id].mx = max(st[id << 1].mx, st[id << 1 | 1].mx);
}
void update(int u, int v, int x, int id = 1, int l = 1, int r = n) {
if(u > r || v < l) return;
if(u <= l && r <= v) {
push(id, x);
return;
}
down(id);
int m = l + r >> 1;
update(u, v, x, id << 1, l, m);
update(u, v, x, id << 1 | 1, m + 1, r);
st[id].res = max(st[id << 1].res, st[id << 1 | 1].res);
}
int query(int u, int v, int id = 1, int l = 1, int r = n) {
if(u > r || v < l) return 0;
if(u <= l && r <= v) return st[id].res;
down(id);
int m = l + r >> 1;
return max(query(u, v, id << 1, l, m), query(u, v, id << 1 | 1, m + 1, r));
}
void process() {
cin >> n;
FOR(i, 1, n) cin >> a[i];
cin >> q;
REP(i, 0, q) {
int l, r; cin >> l >> r;
qrs[l].emplace_back(r, i);
}
vector<int> stk;
FOR(i, 1, n) {
while(!stk.empty() && a[stk.back()] < a[i]) stk.pop_back();
if(stk.size()) candidates.emplace_back(stk.back(), i);
stk.push_back(i);
}
while(!stk.empty()) stk.pop_back();
FORD(i, n, 1) {
while(!stk.empty() && a[stk.back()] < a[i]) stk.pop_back();
if(stk.size()) candidates.emplace_back(i, stk.back());
stk.push_back(i);
}
sort(all(candidates));
vector<int> ans(q);
FORD(i, n, 1) {
upd_point(i, a[i]);
while(!candidates.empty() && candidates.back().fi == i) {
auto [l, r] = candidates.back(); candidates.pop_back();
update(2 * r - l, n, a[l] + a[r]);
}
for(auto [r, id] : qrs[i]) {
ans[id] = query(i, r);
}
}
for(auto x : ans) cout << x << '\n';
}
signed main() {
if(fopen("test.inp","r")) {
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int ntests = 1;
// cin >> ntests;
while(ntests--) process();
return 0;
}
// https://oj.uz/problem/view/JOI19_jumps
Compilation message
jumps.cpp: In function 'void upd_point(int, int)':
jumps.cpp:69:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
69 | int m = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void update(int, int, int, int, int, int)':
jumps.cpp:84:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
84 | int m = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int query(int, int, int, int, int)':
jumps.cpp:94:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int m = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int main()':
jumps.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen("test.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen("test.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
35676 KB |
Output is correct |
2 |
Correct |
13 ms |
35676 KB |
Output is correct |
3 |
Correct |
14 ms |
35676 KB |
Output is correct |
4 |
Correct |
15 ms |
35452 KB |
Output is correct |
5 |
Correct |
14 ms |
35676 KB |
Output is correct |
6 |
Correct |
15 ms |
35712 KB |
Output is correct |
7 |
Correct |
14 ms |
35672 KB |
Output is correct |
8 |
Correct |
14 ms |
35520 KB |
Output is correct |
9 |
Correct |
16 ms |
35484 KB |
Output is correct |
10 |
Correct |
17 ms |
35676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
35676 KB |
Output is correct |
2 |
Correct |
13 ms |
35676 KB |
Output is correct |
3 |
Correct |
14 ms |
35676 KB |
Output is correct |
4 |
Correct |
15 ms |
35452 KB |
Output is correct |
5 |
Correct |
14 ms |
35676 KB |
Output is correct |
6 |
Correct |
15 ms |
35712 KB |
Output is correct |
7 |
Correct |
14 ms |
35672 KB |
Output is correct |
8 |
Correct |
14 ms |
35520 KB |
Output is correct |
9 |
Correct |
16 ms |
35484 KB |
Output is correct |
10 |
Correct |
17 ms |
35676 KB |
Output is correct |
11 |
Correct |
208 ms |
54352 KB |
Output is correct |
12 |
Correct |
208 ms |
54360 KB |
Output is correct |
13 |
Correct |
192 ms |
54436 KB |
Output is correct |
14 |
Correct |
210 ms |
54352 KB |
Output is correct |
15 |
Correct |
204 ms |
54352 KB |
Output is correct |
16 |
Correct |
255 ms |
53840 KB |
Output is correct |
17 |
Correct |
205 ms |
53840 KB |
Output is correct |
18 |
Correct |
206 ms |
53772 KB |
Output is correct |
19 |
Correct |
204 ms |
54264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
42440 KB |
Output is correct |
2 |
Correct |
77 ms |
41540 KB |
Output is correct |
3 |
Correct |
82 ms |
40896 KB |
Output is correct |
4 |
Correct |
137 ms |
42388 KB |
Output is correct |
5 |
Correct |
142 ms |
42444 KB |
Output is correct |
6 |
Correct |
140 ms |
41672 KB |
Output is correct |
7 |
Correct |
136 ms |
41684 KB |
Output is correct |
8 |
Correct |
134 ms |
41676 KB |
Output is correct |
9 |
Correct |
136 ms |
41932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
35676 KB |
Output is correct |
2 |
Correct |
13 ms |
35676 KB |
Output is correct |
3 |
Correct |
14 ms |
35676 KB |
Output is correct |
4 |
Correct |
15 ms |
35452 KB |
Output is correct |
5 |
Correct |
14 ms |
35676 KB |
Output is correct |
6 |
Correct |
15 ms |
35712 KB |
Output is correct |
7 |
Correct |
14 ms |
35672 KB |
Output is correct |
8 |
Correct |
14 ms |
35520 KB |
Output is correct |
9 |
Correct |
16 ms |
35484 KB |
Output is correct |
10 |
Correct |
17 ms |
35676 KB |
Output is correct |
11 |
Correct |
208 ms |
54352 KB |
Output is correct |
12 |
Correct |
208 ms |
54360 KB |
Output is correct |
13 |
Correct |
192 ms |
54436 KB |
Output is correct |
14 |
Correct |
210 ms |
54352 KB |
Output is correct |
15 |
Correct |
204 ms |
54352 KB |
Output is correct |
16 |
Correct |
255 ms |
53840 KB |
Output is correct |
17 |
Correct |
205 ms |
53840 KB |
Output is correct |
18 |
Correct |
206 ms |
53772 KB |
Output is correct |
19 |
Correct |
204 ms |
54264 KB |
Output is correct |
20 |
Correct |
141 ms |
42440 KB |
Output is correct |
21 |
Correct |
77 ms |
41540 KB |
Output is correct |
22 |
Correct |
82 ms |
40896 KB |
Output is correct |
23 |
Correct |
137 ms |
42388 KB |
Output is correct |
24 |
Correct |
142 ms |
42444 KB |
Output is correct |
25 |
Correct |
140 ms |
41672 KB |
Output is correct |
26 |
Correct |
136 ms |
41684 KB |
Output is correct |
27 |
Correct |
134 ms |
41676 KB |
Output is correct |
28 |
Correct |
136 ms |
41932 KB |
Output is correct |
29 |
Correct |
766 ms |
73140 KB |
Output is correct |
30 |
Correct |
579 ms |
71320 KB |
Output is correct |
31 |
Correct |
547 ms |
71340 KB |
Output is correct |
32 |
Correct |
773 ms |
73136 KB |
Output is correct |
33 |
Correct |
811 ms |
73312 KB |
Output is correct |
34 |
Correct |
749 ms |
70832 KB |
Output is correct |
35 |
Correct |
749 ms |
70536 KB |
Output is correct |
36 |
Correct |
747 ms |
70696 KB |
Output is correct |
37 |
Correct |
810 ms |
72108 KB |
Output is correct |
38 |
Correct |
562 ms |
78772 KB |
Output is correct |
39 |
Correct |
579 ms |
78804 KB |
Output is correct |
40 |
Correct |
571 ms |
75512 KB |
Output is correct |
41 |
Correct |
545 ms |
75184 KB |
Output is correct |
42 |
Correct |
552 ms |
75016 KB |
Output is correct |
43 |
Correct |
548 ms |
76708 KB |
Output is correct |
44 |
Correct |
593 ms |
78256 KB |
Output is correct |
45 |
Correct |
587 ms |
78248 KB |
Output is correct |
46 |
Correct |
639 ms |
75108 KB |
Output is correct |
47 |
Correct |
581 ms |
74684 KB |
Output is correct |
48 |
Correct |
583 ms |
74804 KB |
Output is correct |
49 |
Correct |
572 ms |
76744 KB |
Output is correct |
50 |
Correct |
626 ms |
76388 KB |
Output is correct |
51 |
Correct |
641 ms |
76460 KB |
Output is correct |
52 |
Correct |
671 ms |
73816 KB |
Output is correct |
53 |
Correct |
639 ms |
73724 KB |
Output is correct |
54 |
Correct |
629 ms |
73716 KB |
Output is correct |
55 |
Correct |
697 ms |
75208 KB |
Output is correct |