#include <bits/stdc++.h>
using namespace std ;
const int MAX = 5e5 + 10 ;
int arr[MAX] ;
int n ;
vector< pair<int , int> >queries[MAX] ;
vector<int>v[MAX] ;
int ans[MAX] ;
struct Node
{
int x = 0 , y = 0 , val = 0 ;
};
Node tree[4 * MAX] ;
Node Merge(Node &a , Node &b)
{
Node c ;
c.x = max(a.x , b.x) ;
c.y = max(a.y , b.y) ;
c.val = max({a.val , b.val , a.y + b.x}) ;
return c ;
}
void build(int node , int l , int r)
{
if(l == r)
{
tree[node].x = arr[l] ;
tree[node].val = tree[node].x + tree[node].y ;
return ;
}
int mid = (l + r) >> 1 ;
build(node << 1 , l , mid) ;
build(node << 1 | 1 , mid+1 , r) ;
tree[node] = Merge(tree[node << 1] , tree[node << 1 | 1]) ;
}
void update(int node , int l , int r , int idx , int val)
{
if(idx > r || idx < l)
return ;
if(l == r)
{
tree[node].y = max(tree[node].y , val) ;
tree[node].val = tree[node].x + tree[node].y ;
return ;
}
int mid = (l + r) >> 1 ;
update(node << 1 , l , mid , idx , val) ;
update(node << 1 | 1 , mid+1 , r , idx , val) ;
tree[node] = Merge(tree[node << 1] , tree[node << 1 | 1]) ;
}
Node tmp ;
Node query(int node , int l , int r , int from , int to)
{
if(from > r || to < l)
return tmp ;
if(l >= from && r <= to)
return tree[node] ;
int mid = (l + r) >> 1 ;
Node x = query(node << 1 , l , mid , from , to) ;
Node y = query(node << 1 | 1 , mid+1 , r , from , to) ;
return Merge(x , y) ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 1 ; i <= n ; ++i)
cin>>arr[i] ;
int q ;
cin>>q ;
for(int i = 1 ; i <= q ; ++i)
{
int l , r ;
cin>>l>>r ;
queries[l].emplace_back(r , i) ;
}
stack<int>s ;
for(int i = 1 ; i <= n ; ++i)
{
while(s.size() && arr[i] >= arr[s.top()])
v[s.top()].push_back(i) , s.pop() ;
if(s.size())
v[s.top()].push_back(i) ;
s.push(i) ;
}
build(1 , 1 , n) ;
for(int i = n ; i >= 1 ; --i)
{
for(auto &j : v[i])
{
if(2*j - i <= n)
update(1 , 1 , n , 2*j - i , arr[i] + arr[j]) ;
}
for(auto &p : queries[i])
ans[p.second] = query(1 , 1 , n , i+2 , p.first).val ;
}
for(int i = 1 ; i <= q ; ++i)
cout<<ans[i]<<"\n" ;
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23720 KB |
Output is correct |
3 |
Correct |
14 ms |
23816 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23764 KB |
Output is correct |
7 |
Correct |
16 ms |
23756 KB |
Output is correct |
8 |
Correct |
16 ms |
23756 KB |
Output is correct |
9 |
Correct |
16 ms |
23804 KB |
Output is correct |
10 |
Correct |
15 ms |
23780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23720 KB |
Output is correct |
3 |
Correct |
14 ms |
23816 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23764 KB |
Output is correct |
7 |
Correct |
16 ms |
23756 KB |
Output is correct |
8 |
Correct |
16 ms |
23756 KB |
Output is correct |
9 |
Correct |
16 ms |
23804 KB |
Output is correct |
10 |
Correct |
15 ms |
23780 KB |
Output is correct |
11 |
Correct |
479 ms |
42620 KB |
Output is correct |
12 |
Correct |
536 ms |
42508 KB |
Output is correct |
13 |
Correct |
476 ms |
42504 KB |
Output is correct |
14 |
Correct |
474 ms |
42660 KB |
Output is correct |
15 |
Correct |
493 ms |
42700 KB |
Output is correct |
16 |
Correct |
476 ms |
41980 KB |
Output is correct |
17 |
Correct |
465 ms |
42052 KB |
Output is correct |
18 |
Correct |
453 ms |
41816 KB |
Output is correct |
19 |
Correct |
449 ms |
42436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
214 ms |
38852 KB |
Output is correct |
2 |
Correct |
137 ms |
38684 KB |
Output is correct |
3 |
Correct |
133 ms |
39520 KB |
Output is correct |
4 |
Correct |
219 ms |
38928 KB |
Output is correct |
5 |
Correct |
218 ms |
38852 KB |
Output is correct |
6 |
Correct |
209 ms |
38212 KB |
Output is correct |
7 |
Correct |
210 ms |
38084 KB |
Output is correct |
8 |
Correct |
206 ms |
38160 KB |
Output is correct |
9 |
Correct |
211 ms |
38468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
23720 KB |
Output is correct |
3 |
Correct |
14 ms |
23816 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23764 KB |
Output is correct |
7 |
Correct |
16 ms |
23756 KB |
Output is correct |
8 |
Correct |
16 ms |
23756 KB |
Output is correct |
9 |
Correct |
16 ms |
23804 KB |
Output is correct |
10 |
Correct |
15 ms |
23780 KB |
Output is correct |
11 |
Correct |
479 ms |
42620 KB |
Output is correct |
12 |
Correct |
536 ms |
42508 KB |
Output is correct |
13 |
Correct |
476 ms |
42504 KB |
Output is correct |
14 |
Correct |
474 ms |
42660 KB |
Output is correct |
15 |
Correct |
493 ms |
42700 KB |
Output is correct |
16 |
Correct |
476 ms |
41980 KB |
Output is correct |
17 |
Correct |
465 ms |
42052 KB |
Output is correct |
18 |
Correct |
453 ms |
41816 KB |
Output is correct |
19 |
Correct |
449 ms |
42436 KB |
Output is correct |
20 |
Correct |
214 ms |
38852 KB |
Output is correct |
21 |
Correct |
137 ms |
38684 KB |
Output is correct |
22 |
Correct |
133 ms |
39520 KB |
Output is correct |
23 |
Correct |
219 ms |
38928 KB |
Output is correct |
24 |
Correct |
218 ms |
38852 KB |
Output is correct |
25 |
Correct |
209 ms |
38212 KB |
Output is correct |
26 |
Correct |
210 ms |
38084 KB |
Output is correct |
27 |
Correct |
206 ms |
38160 KB |
Output is correct |
28 |
Correct |
211 ms |
38468 KB |
Output is correct |
29 |
Correct |
1430 ms |
82144 KB |
Output is correct |
30 |
Correct |
1167 ms |
81560 KB |
Output is correct |
31 |
Correct |
1185 ms |
83580 KB |
Output is correct |
32 |
Correct |
1379 ms |
82136 KB |
Output is correct |
33 |
Correct |
1387 ms |
82088 KB |
Output is correct |
34 |
Correct |
1356 ms |
79812 KB |
Output is correct |
35 |
Correct |
1385 ms |
79536 KB |
Output is correct |
36 |
Correct |
1396 ms |
79428 KB |
Output is correct |
37 |
Correct |
1368 ms |
80908 KB |
Output is correct |
38 |
Correct |
1068 ms |
87872 KB |
Output is correct |
39 |
Correct |
1073 ms |
87780 KB |
Output is correct |
40 |
Correct |
1048 ms |
84348 KB |
Output is correct |
41 |
Correct |
1051 ms |
84016 KB |
Output is correct |
42 |
Correct |
1051 ms |
83920 KB |
Output is correct |
43 |
Correct |
1064 ms |
85780 KB |
Output is correct |
44 |
Correct |
1143 ms |
87144 KB |
Output is correct |
45 |
Correct |
1128 ms |
87228 KB |
Output is correct |
46 |
Correct |
1142 ms |
83964 KB |
Output is correct |
47 |
Correct |
1201 ms |
83612 KB |
Output is correct |
48 |
Correct |
1121 ms |
83576 KB |
Output is correct |
49 |
Correct |
1148 ms |
85648 KB |
Output is correct |
50 |
Correct |
1246 ms |
85052 KB |
Output is correct |
51 |
Correct |
1209 ms |
85148 KB |
Output is correct |
52 |
Correct |
1196 ms |
82580 KB |
Output is correct |
53 |
Correct |
1199 ms |
82232 KB |
Output is correct |
54 |
Correct |
1189 ms |
82264 KB |
Output is correct |
55 |
Correct |
1234 ms |
84084 KB |
Output is correct |