#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7; // !
pair<int,int> t[4 * N];
int lz[4 * N], a[N];
vector<int> x[N];
vector<pair<int,int>> qry[N];
void build(int u, int l, int r) {
if(l == r) {
t[u] = {-0, a[r]};
return;
}
int mid = (l + r) / 2;
build(2 * u, l, mid); build(2 * u + 1, mid + 1, r);
t[u] = {max(t[2 * u].f, t[2 * u + 1].f), max(t[2 * u].s, t[2 * u + 1].s)};
}
void push(int u, int l, int r) {
t[u].f = max(t[u].f, lz[u] + t[u].s);
if(l != r) {
lz[2 * u] = max(lz[2 * u], lz[u]);
lz[2 * u + 1] = max(lz[2 * u + 1], lz[u]);
}
lz[u] = 0;
}
void upd(int u, int st,int en, int l, int r, int v) {
if(lz[u]) push(u, l, r);
if(l > en || r < st) return;
if(st <= l && r <= en) {
lz[u] = v;
push(u, l, r);
return;
}
int mid = (l + r) / 2;
upd(2 * u, st, en, l, mid, v); upd(2 * u + 1, st, en, mid + 1, r, v);
t[u] = {max(t[2 * u].f, t[2 * u + 1].f), max(t[2 * u].s, t[2 * u + 1].s)};
}
int get(int u, int st, int en, int l, int r) {
if(lz[u]) push(u ,l ,r);
if(l > en || r < st) return 0;
if(st <= l && r <= en) return t[u].f;
int mid = (l + r) / 2;
return max(get(2 * u, st, en, l, mid), get(2 * u + 1, st, en, mid + 1, r));
}
main(){
int n;
cin >> n;
stack<int> st;
for(int i = 1; i <= n; i++) {
cin >> a[i];
while(st.size() && a[st.top()] < a[i]) st.pop();
if(st.size()) x[st.top()].push_back(i);
st.push(i);
}
while(st.size()) st.pop();
for(int i = n; i >= 1; i--) {
while(st.size() && a[st.top()] < a[i]) st.pop();
if(st.size()) x[i].push_back(st.top());
st.push(i);
}
int q; cin >> q;
vector<int> ans(q +2);
for(int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
qry[l].push_back({i, r});
}
build(1, 1, n);
for(int l = n; l >= 1; l--) {
for(int j = 0; j < x[l].size(); j++) {
int r = x[l][j];
upd(1, 2 * r - l, n, 1, n, a[l] + a[r]);
// cout << 2 * r - l << " __ " << n << " " << a[l] + a[r] << endl;
}
for(int i = 0; i < qry[l].size(); i++) {
ans[qry[l][i].f] = get(1, l, qry[l][i].s, 1, n);
}
}
for(int i = 1; i <= q; i++) cout << ans[i] << " ";
}
Compilation message
jumps.cpp:48:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
48 | main(){
| ^~~~
jumps.cpp: In function 'int main()':
jumps.cpp:73:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int j = 0; j < x[l].size(); j++) {
| ~~^~~~~~~~~~~~~
jumps.cpp:78:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 0; i < qry[l].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9656 KB |
Output is correct |
5 |
Correct |
5 ms |
9636 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9744 KB |
Output is correct |
9 |
Correct |
4 ms |
9704 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9656 KB |
Output is correct |
5 |
Correct |
5 ms |
9636 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9744 KB |
Output is correct |
9 |
Correct |
4 ms |
9704 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
379 ms |
37332 KB |
Output is correct |
12 |
Correct |
384 ms |
37200 KB |
Output is correct |
13 |
Correct |
387 ms |
37324 KB |
Output is correct |
14 |
Correct |
373 ms |
37224 KB |
Output is correct |
15 |
Correct |
373 ms |
37244 KB |
Output is correct |
16 |
Correct |
397 ms |
36768 KB |
Output is correct |
17 |
Correct |
372 ms |
36764 KB |
Output is correct |
18 |
Correct |
386 ms |
36544 KB |
Output is correct |
19 |
Correct |
380 ms |
37264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
32704 KB |
Output is correct |
2 |
Correct |
112 ms |
33184 KB |
Output is correct |
3 |
Correct |
108 ms |
33208 KB |
Output is correct |
4 |
Correct |
210 ms |
32700 KB |
Output is correct |
5 |
Correct |
173 ms |
32792 KB |
Output is correct |
6 |
Correct |
145 ms |
32148 KB |
Output is correct |
7 |
Correct |
151 ms |
32172 KB |
Output is correct |
8 |
Correct |
138 ms |
32036 KB |
Output is correct |
9 |
Correct |
164 ms |
32368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9656 KB |
Output is correct |
5 |
Correct |
5 ms |
9636 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9744 KB |
Output is correct |
9 |
Correct |
4 ms |
9704 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
379 ms |
37332 KB |
Output is correct |
12 |
Correct |
384 ms |
37200 KB |
Output is correct |
13 |
Correct |
387 ms |
37324 KB |
Output is correct |
14 |
Correct |
373 ms |
37224 KB |
Output is correct |
15 |
Correct |
373 ms |
37244 KB |
Output is correct |
16 |
Correct |
397 ms |
36768 KB |
Output is correct |
17 |
Correct |
372 ms |
36764 KB |
Output is correct |
18 |
Correct |
386 ms |
36544 KB |
Output is correct |
19 |
Correct |
380 ms |
37264 KB |
Output is correct |
20 |
Correct |
179 ms |
32704 KB |
Output is correct |
21 |
Correct |
112 ms |
33184 KB |
Output is correct |
22 |
Correct |
108 ms |
33208 KB |
Output is correct |
23 |
Correct |
210 ms |
32700 KB |
Output is correct |
24 |
Correct |
173 ms |
32792 KB |
Output is correct |
25 |
Correct |
145 ms |
32148 KB |
Output is correct |
26 |
Correct |
151 ms |
32172 KB |
Output is correct |
27 |
Correct |
138 ms |
32036 KB |
Output is correct |
28 |
Correct |
164 ms |
32368 KB |
Output is correct |
29 |
Runtime error |
78 ms |
32092 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |