#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const int N = 5e5+10;
const int INF = INT_MAX;
const int mod = 1e9+7;
int n,a[N];
int t;
vector<pair<int,int>> q[N];
vector<int> st;
ll ans[N],lazy[4*N];
struct Node{
ll best,mx,Mx;
Node() {};
Node(ll _best, ll _mx, ll _Mx) {
best = _best;
mx = _mx;
Mx = _mx;
};
}tree[4*N];
Node operator + (Node u, Node v) {
Node res = u;
res.best = max({res.best,u.mx+v.Mx,v.best});
res.mx = max(res.mx,v.mx);
res.Mx = max(res.Mx,v.Mx);
return res;
}
void build(int id = 1, int l = 1, int r = n) {
if(l == r) {
tree[id].Mx = a[l];
tree[id].best = a[l];
return;
}
int mid = l + r >> 1;
build(id << 1,l,mid);
build(id << 1 | 1,mid+1,r);
tree[id] = tree[id << 1] + tree[id << 1 | 1];
}
void down(int id, int l, int r) {
ll t = lazy[id];
if(t == 0) {
return;
}
tree[id].mx = max(tree[id].mx,t);
tree[id].best = tree[id].mx+tree[id].Mx;
if(l < r) {
lazy[id << 1] = max(lazy[id << 1],t);
lazy[id << 1 | 1] = max(lazy[id << 1 | 1],t);
}
t = 0;
}
void update(int u, int v, ll val, int id = 1, int l = 1, int r = n) {
down(id,l,r);
if(u > v || l > r || r < u || v < l) {
return;
}
if(l == r) {
lazy[id] = val;
down(id,l,r);
return;
}
int mid = l + r >> 1;
update(u,v,val,id << 1,l,mid);
update(u,v,val,id << 1 | 1,mid+1,r);
tree[id] = tree[id << 1] + tree[id << 1 | 1];
}
Node get(int u, int v, int id = 1, int l = 1, int r = n) {
if(u > v || l > r || r < u || v < l) {
return {0,0,0};
}
if(u <= l && r <= v) {
return tree[id];
}
int mid = l + r >> 1;
return get(u,v,id << 1,l,mid)+get(u,v,id << 1 | 1,mid+1,r);
}
signed main()
{
if (fopen("triplejump.inp", "r")) {
freopen("triplejump.inp", "r", stdin);
freopen("triplejump.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
}
cin >> t;
for(int i = 1 ; i <= t ; i++) {
int l,r; cin >> l >> r;
q[l].push_back({r,i});
}
build();
for(int i = n ; i > 0 ; i--) {
while(!st.empty() && a[st.back()] < a[i]) {
update(2*st.back()-i,n,a[i]+a[st.back()]);
st.pop_back();
}
if(!st.empty()) {
update(2*st.back()-i,n,a[i]+a[st.back()]);
}
st.push_back(i);
for(auto p:q[i]) {
ans[p.second] = get(i,p.first).best;
}
}
for(int i = 1 ; i <= t ; i++) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
jumps.cpp: In function 'void build(int, int, int)':
jumps.cpp:42:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void update(int, int, long long int, int, int, int)':
jumps.cpp:72:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'Node get(int, int, int, int, int)':
jumps.cpp:85:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
85 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int main()':
jumps.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen("triplejump.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen("triplejump.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16984 KB |
Output is correct |
5 |
Correct |
3 ms |
16836 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16732 KB |
Output is correct |
8 |
Correct |
4 ms |
16732 KB |
Output is correct |
9 |
Correct |
3 ms |
16732 KB |
Output is correct |
10 |
Correct |
3 ms |
16728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16984 KB |
Output is correct |
5 |
Correct |
3 ms |
16836 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16732 KB |
Output is correct |
8 |
Correct |
4 ms |
16732 KB |
Output is correct |
9 |
Correct |
3 ms |
16732 KB |
Output is correct |
10 |
Correct |
3 ms |
16728 KB |
Output is correct |
11 |
Correct |
764 ms |
37756 KB |
Output is correct |
12 |
Correct |
486 ms |
37716 KB |
Output is correct |
13 |
Correct |
504 ms |
38308 KB |
Output is correct |
14 |
Correct |
723 ms |
37972 KB |
Output is correct |
15 |
Correct |
727 ms |
37880 KB |
Output is correct |
16 |
Correct |
740 ms |
37236 KB |
Output is correct |
17 |
Correct |
728 ms |
37236 KB |
Output is correct |
18 |
Correct |
743 ms |
37204 KB |
Output is correct |
19 |
Correct |
725 ms |
37604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4022 ms |
29652 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16984 KB |
Output is correct |
5 |
Correct |
3 ms |
16836 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16732 KB |
Output is correct |
8 |
Correct |
4 ms |
16732 KB |
Output is correct |
9 |
Correct |
3 ms |
16732 KB |
Output is correct |
10 |
Correct |
3 ms |
16728 KB |
Output is correct |
11 |
Correct |
764 ms |
37756 KB |
Output is correct |
12 |
Correct |
486 ms |
37716 KB |
Output is correct |
13 |
Correct |
504 ms |
38308 KB |
Output is correct |
14 |
Correct |
723 ms |
37972 KB |
Output is correct |
15 |
Correct |
727 ms |
37880 KB |
Output is correct |
16 |
Correct |
740 ms |
37236 KB |
Output is correct |
17 |
Correct |
728 ms |
37236 KB |
Output is correct |
18 |
Correct |
743 ms |
37204 KB |
Output is correct |
19 |
Correct |
725 ms |
37604 KB |
Output is correct |
20 |
Execution timed out |
4022 ms |
29652 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |