#include<bits/stdc++.h>
#define taskname "A"
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 5e5 + 6;
typedef pair<int,int> ii;
typedef long long ll;
typedef long double ld;
ll lz[maxn * 4] , s[maxn * 4] , res[maxn * 4];
int n , a[maxn];
void push(int x , bool key){
res[x] = max(res[x],s[x]+lz[x]);
if(key){
lz[x*2]=max(lz[x*2],lz[x]);
lz[x*2+1] = max(lz[x*2+1],lz[x]);
}
}
void build(int x , int l , int r){
lz[x] = -2e9;
if(l == r){
s[x] = a[l];
return;
}
int mid = l + r >> 1;
build(x*2,l,mid);build(x*2+1,mid+1,r);
s[x] = max(s[x*2],s[x*2+1]);
}
void update(int x , int l , int r , int L , int R , ll val){
push(x,l!=r);
if(L > r || l > R)return;
if(L <= l && r <= R){
lz[x] = max(lz[x],val);
push(x,l!=r);
return;
}
int mid = l + r >> 1;
update(x*2,l,mid,L,R,val);
update(x*2+1,mid+1,r,L,R,val);
res[x] = max({res[x],res[x*2],res[x*2+1]});
}
ll query(int x , int l , int r , int L , int R){
push(x,l!=r);
if(L > r || l > R)return 0;
if(L <= l && r <= R)return res[x];
int mid = l + r >> 1;
return max(query(x*2,l,mid,L,R),query(x*2+1,mid+1,r,L,R));
}
int q;
ll ans[maxn];
vector<ii> Q[maxn];
vector<int> U[maxn];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP","r",stdin);
freopen(taskname".OUT","w",stdout);
}
cin >> n;
for(int i = 1 ; i <= n ; ++i)cin >> a[i];
build(1,1,n);
vector<int> s;
for(int i = 1 ; i <= n ; ++i){
while(s.size() && a[s.back()] <= a[i]){
U[s.back()].pb(i);s.pop_back();
}
if(s.size())U[s.back()].pb(i);
s.pb(i);
}
cin >> q;
for(int i = 1 ; i <= q ; ++i){
int u , v;cin >> u >> v;Q[u].pb(mp(v,i));
}
for(int i = n ; i >= 1 ; --i){
for(auto & b : U[i]){
if(b * 2 - i <= n){
update(1,1,n,b*2-i,n,a[b]+a[i]);
// cout << i << " " << b << " " << b * 2 - i << endl;
}
}
for(auto & c : Q[i]){
ans[c.second] = query(1,1,n,i,c.first);
}
}
for(int i = 1 ; i <= q ; ++i)cout << ans[i] << '\n';
}
Compilation message
jumps.cpp: In function 'void build(int, int, int)':
jumps.cpp:26:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
jumps.cpp: In function 'void update(int, int, int, int, int, ll)':
jumps.cpp:38:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
jumps.cpp: In function 'll query(int, int, int, int, int)':
jumps.cpp:47:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
jumps.cpp: In function 'int main()':
jumps.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".INP","r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".OUT","w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
21 ms |
23928 KB |
Output is correct |
3 |
Correct |
20 ms |
23928 KB |
Output is correct |
4 |
Correct |
21 ms |
23800 KB |
Output is correct |
5 |
Correct |
20 ms |
23928 KB |
Output is correct |
6 |
Correct |
20 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23916 KB |
Output is correct |
8 |
Correct |
22 ms |
23816 KB |
Output is correct |
9 |
Correct |
20 ms |
23928 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
21 ms |
23928 KB |
Output is correct |
3 |
Correct |
20 ms |
23928 KB |
Output is correct |
4 |
Correct |
21 ms |
23800 KB |
Output is correct |
5 |
Correct |
20 ms |
23928 KB |
Output is correct |
6 |
Correct |
20 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23916 KB |
Output is correct |
8 |
Correct |
22 ms |
23816 KB |
Output is correct |
9 |
Correct |
20 ms |
23928 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
11 |
Correct |
405 ms |
44536 KB |
Output is correct |
12 |
Correct |
379 ms |
44408 KB |
Output is correct |
13 |
Correct |
391 ms |
44408 KB |
Output is correct |
14 |
Correct |
410 ms |
44536 KB |
Output is correct |
15 |
Correct |
376 ms |
44408 KB |
Output is correct |
16 |
Correct |
380 ms |
43768 KB |
Output is correct |
17 |
Correct |
392 ms |
43948 KB |
Output is correct |
18 |
Correct |
385 ms |
43832 KB |
Output is correct |
19 |
Correct |
390 ms |
44408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
45260 KB |
Output is correct |
2 |
Correct |
139 ms |
44920 KB |
Output is correct |
3 |
Correct |
144 ms |
45764 KB |
Output is correct |
4 |
Correct |
210 ms |
45176 KB |
Output is correct |
5 |
Correct |
241 ms |
45308 KB |
Output is correct |
6 |
Correct |
222 ms |
44536 KB |
Output is correct |
7 |
Correct |
215 ms |
44408 KB |
Output is correct |
8 |
Correct |
215 ms |
44536 KB |
Output is correct |
9 |
Correct |
211 ms |
44792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
21 ms |
23928 KB |
Output is correct |
3 |
Correct |
20 ms |
23928 KB |
Output is correct |
4 |
Correct |
21 ms |
23800 KB |
Output is correct |
5 |
Correct |
20 ms |
23928 KB |
Output is correct |
6 |
Correct |
20 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23916 KB |
Output is correct |
8 |
Correct |
22 ms |
23816 KB |
Output is correct |
9 |
Correct |
20 ms |
23928 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
11 |
Correct |
405 ms |
44536 KB |
Output is correct |
12 |
Correct |
379 ms |
44408 KB |
Output is correct |
13 |
Correct |
391 ms |
44408 KB |
Output is correct |
14 |
Correct |
410 ms |
44536 KB |
Output is correct |
15 |
Correct |
376 ms |
44408 KB |
Output is correct |
16 |
Correct |
380 ms |
43768 KB |
Output is correct |
17 |
Correct |
392 ms |
43948 KB |
Output is correct |
18 |
Correct |
385 ms |
43832 KB |
Output is correct |
19 |
Correct |
390 ms |
44408 KB |
Output is correct |
20 |
Correct |
212 ms |
45260 KB |
Output is correct |
21 |
Correct |
139 ms |
44920 KB |
Output is correct |
22 |
Correct |
144 ms |
45764 KB |
Output is correct |
23 |
Correct |
210 ms |
45176 KB |
Output is correct |
24 |
Correct |
241 ms |
45308 KB |
Output is correct |
25 |
Correct |
222 ms |
44536 KB |
Output is correct |
26 |
Correct |
215 ms |
44408 KB |
Output is correct |
27 |
Correct |
215 ms |
44536 KB |
Output is correct |
28 |
Correct |
211 ms |
44792 KB |
Output is correct |
29 |
Correct |
1445 ms |
96496 KB |
Output is correct |
30 |
Correct |
1151 ms |
96248 KB |
Output is correct |
31 |
Correct |
1152 ms |
98024 KB |
Output is correct |
32 |
Correct |
1410 ms |
96564 KB |
Output is correct |
33 |
Correct |
1421 ms |
96376 KB |
Output is correct |
34 |
Correct |
1410 ms |
94352 KB |
Output is correct |
35 |
Correct |
1406 ms |
94072 KB |
Output is correct |
36 |
Correct |
1409 ms |
93948 KB |
Output is correct |
37 |
Correct |
1345 ms |
95352 KB |
Output is correct |
38 |
Correct |
1000 ms |
102136 KB |
Output is correct |
39 |
Correct |
989 ms |
102136 KB |
Output is correct |
40 |
Correct |
935 ms |
98808 KB |
Output is correct |
41 |
Correct |
972 ms |
98296 KB |
Output is correct |
42 |
Correct |
951 ms |
98512 KB |
Output is correct |
43 |
Correct |
1000 ms |
100048 KB |
Output is correct |
44 |
Correct |
1061 ms |
101624 KB |
Output is correct |
45 |
Correct |
1078 ms |
101648 KB |
Output is correct |
46 |
Correct |
1053 ms |
98364 KB |
Output is correct |
47 |
Correct |
1030 ms |
97904 KB |
Output is correct |
48 |
Correct |
1016 ms |
98168 KB |
Output is correct |
49 |
Correct |
1043 ms |
100208 KB |
Output is correct |
50 |
Execution timed out |
194 ms |
69624 KB |
Time limit exceeded (wall clock) |
51 |
Halted |
0 ms |
0 KB |
- |