#pragma GCC optimize("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define e1 first
#define e2 second
#define int int_fast32_t
#define uint uint_fast64_t
#define ll int_fast64_t
#define ull uint_fast64_t
#define int16 int_fast16_t
#define uint16 uint_fast16_t
#define int32 int_fast32_t
#define uint32 uint_fast32_t
#define int64 int_fast64_t
#define uint64 uint_fast64_t
#define ld long double
#define float long double
#define size(x) (int)x.size()
#define satori int testCases; cin>>testCases; while(testCases--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define all(r) begin(r),end(r)
#define rall(r) rbegin(r),rend(r)
#define time chrono::high_resolution_clock().now().time_since_epoch().count()
#define elapsed(__begin,__end) (__end-__begin)/CLOCKS_PER_SEC
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
float pi=3.14159265358979323846;
const int MAXN=5e5+10,base=(1<<19),inf=1e9+2137;
int ans[MAXN],a[MAXN],b[base*2],c[base*2],bc[base*2];
vector<int> ud[MAXN],s;
vector<pair<int,int>> qr[MAXN];
void add(int u,int v)
{
u+=base;
while(u)
{
b[u]=max(b[u],v),u/=2;
bc[u]=max({bc[u],bc[u*2],bc[u*2+1],b[u*2]+c[u*2+1]});
}
}
pair<int,pair<int,int>> get(int l,int r,int u=1,int cl=0,int cr=base-1)
{
if(cl>r||cr<l)
return mp(-inf,mp(-inf,-inf));
if(cl>=l&&cr<=r)
return mp(bc[u],mp(b[u],c[u]));
auto lo=get(l,r,u*2,cl,(cl+cr)/2),hi=get(l,r,u*2+1,(cl+cr)/2+1,cr);
return mp(max({lo.e1,hi.e1,lo.e2.e1+hi.e2.e2}),mp(max(lo.e2.e1,hi.e2.e1),max(lo.e2.e2,hi.e2.e2)));
}
int32_t main()
{
fastio;
int n,q,l,r;
cin>>n;
for(int i=base;i<base*2;i++)
c[i]=-inf;
for(int i=1;i<=n;i++)
{
cin>>a[i];
c[base+i]=a[i];
while(size(s)&&a[s.back()]<a[i])
ud[s.back()].eb(i),s.pop_back();
if(size(s))
ud[s.back()].eb(i);
if(size(s)&&a[s.back()]==a[i])
s.pop_back();
s.eb(i);
}
cin>>q;
for(int i=0;i<q;i++)
cin>>l>>r,qr[l].eb(mp(r,i));
for(int i=0;i<base*2;i++)
b[i]=bc[i]=-inf;
for(int i=base-1;i>=0;i--)
c[i]=max(c[i*2],c[i*2+1]);
for(int i=n;i>=1;i--)
{
for(auto xd:ud[i])
{
r=xd;
if(r*2-i>n)
continue;
add(r*2-i-1,a[i]+a[r]);
}
for(auto xd:qr[i])
ans[xd.e2]=get(i,xd.e1).e1;
}
for(int i=0;i<q;i++)
cout<<ans[i]<<'\n';
}
Compilation message
jumps.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
2 | #pragma GCC optimization ("O3")
|
jumps.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
48504 KB |
Output is correct |
2 |
Correct |
32 ms |
48632 KB |
Output is correct |
3 |
Correct |
32 ms |
48504 KB |
Output is correct |
4 |
Correct |
32 ms |
48640 KB |
Output is correct |
5 |
Correct |
32 ms |
48504 KB |
Output is correct |
6 |
Correct |
32 ms |
48504 KB |
Output is correct |
7 |
Correct |
31 ms |
48512 KB |
Output is correct |
8 |
Correct |
32 ms |
48512 KB |
Output is correct |
9 |
Correct |
32 ms |
48504 KB |
Output is correct |
10 |
Correct |
32 ms |
48504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
48504 KB |
Output is correct |
2 |
Correct |
32 ms |
48632 KB |
Output is correct |
3 |
Correct |
32 ms |
48504 KB |
Output is correct |
4 |
Correct |
32 ms |
48640 KB |
Output is correct |
5 |
Correct |
32 ms |
48504 KB |
Output is correct |
6 |
Correct |
32 ms |
48504 KB |
Output is correct |
7 |
Correct |
31 ms |
48512 KB |
Output is correct |
8 |
Correct |
32 ms |
48512 KB |
Output is correct |
9 |
Correct |
32 ms |
48504 KB |
Output is correct |
10 |
Correct |
32 ms |
48504 KB |
Output is correct |
11 |
Correct |
403 ms |
74964 KB |
Output is correct |
12 |
Correct |
397 ms |
74872 KB |
Output is correct |
13 |
Correct |
408 ms |
75032 KB |
Output is correct |
14 |
Correct |
399 ms |
74872 KB |
Output is correct |
15 |
Correct |
408 ms |
74872 KB |
Output is correct |
16 |
Correct |
404 ms |
74488 KB |
Output is correct |
17 |
Correct |
400 ms |
74360 KB |
Output is correct |
18 |
Correct |
454 ms |
74232 KB |
Output is correct |
19 |
Correct |
406 ms |
74872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
59384 KB |
Output is correct |
2 |
Correct |
106 ms |
57976 KB |
Output is correct |
3 |
Correct |
106 ms |
59624 KB |
Output is correct |
4 |
Correct |
162 ms |
59388 KB |
Output is correct |
5 |
Correct |
162 ms |
59384 KB |
Output is correct |
6 |
Correct |
157 ms |
58616 KB |
Output is correct |
7 |
Correct |
156 ms |
58488 KB |
Output is correct |
8 |
Correct |
157 ms |
58488 KB |
Output is correct |
9 |
Correct |
159 ms |
58744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
48504 KB |
Output is correct |
2 |
Correct |
32 ms |
48632 KB |
Output is correct |
3 |
Correct |
32 ms |
48504 KB |
Output is correct |
4 |
Correct |
32 ms |
48640 KB |
Output is correct |
5 |
Correct |
32 ms |
48504 KB |
Output is correct |
6 |
Correct |
32 ms |
48504 KB |
Output is correct |
7 |
Correct |
31 ms |
48512 KB |
Output is correct |
8 |
Correct |
32 ms |
48512 KB |
Output is correct |
9 |
Correct |
32 ms |
48504 KB |
Output is correct |
10 |
Correct |
32 ms |
48504 KB |
Output is correct |
11 |
Correct |
403 ms |
74964 KB |
Output is correct |
12 |
Correct |
397 ms |
74872 KB |
Output is correct |
13 |
Correct |
408 ms |
75032 KB |
Output is correct |
14 |
Correct |
399 ms |
74872 KB |
Output is correct |
15 |
Correct |
408 ms |
74872 KB |
Output is correct |
16 |
Correct |
404 ms |
74488 KB |
Output is correct |
17 |
Correct |
400 ms |
74360 KB |
Output is correct |
18 |
Correct |
454 ms |
74232 KB |
Output is correct |
19 |
Correct |
406 ms |
74872 KB |
Output is correct |
20 |
Correct |
162 ms |
59384 KB |
Output is correct |
21 |
Correct |
106 ms |
57976 KB |
Output is correct |
22 |
Correct |
106 ms |
59624 KB |
Output is correct |
23 |
Correct |
162 ms |
59388 KB |
Output is correct |
24 |
Correct |
162 ms |
59384 KB |
Output is correct |
25 |
Correct |
157 ms |
58616 KB |
Output is correct |
26 |
Correct |
156 ms |
58488 KB |
Output is correct |
27 |
Correct |
157 ms |
58488 KB |
Output is correct |
28 |
Correct |
159 ms |
58744 KB |
Output is correct |
29 |
Correct |
1085 ms |
104128 KB |
Output is correct |
30 |
Correct |
956 ms |
100984 KB |
Output is correct |
31 |
Correct |
950 ms |
104924 KB |
Output is correct |
32 |
Correct |
1062 ms |
104288 KB |
Output is correct |
33 |
Correct |
1078 ms |
104096 KB |
Output is correct |
34 |
Correct |
1063 ms |
101752 KB |
Output is correct |
35 |
Correct |
1056 ms |
101496 KB |
Output is correct |
36 |
Correct |
1059 ms |
101752 KB |
Output is correct |
37 |
Correct |
1056 ms |
102888 KB |
Output is correct |
38 |
Correct |
725 ms |
106728 KB |
Output is correct |
39 |
Correct |
734 ms |
106744 KB |
Output is correct |
40 |
Correct |
714 ms |
103416 KB |
Output is correct |
41 |
Correct |
702 ms |
103100 KB |
Output is correct |
42 |
Correct |
713 ms |
103036 KB |
Output is correct |
43 |
Correct |
722 ms |
104696 KB |
Output is correct |
44 |
Correct |
777 ms |
107004 KB |
Output is correct |
45 |
Correct |
767 ms |
107000 KB |
Output is correct |
46 |
Correct |
754 ms |
103800 KB |
Output is correct |
47 |
Correct |
750 ms |
103416 KB |
Output is correct |
48 |
Correct |
762 ms |
103416 KB |
Output is correct |
49 |
Correct |
763 ms |
105336 KB |
Output is correct |
50 |
Correct |
854 ms |
107384 KB |
Output is correct |
51 |
Correct |
860 ms |
107256 KB |
Output is correct |
52 |
Correct |
845 ms |
104884 KB |
Output is correct |
53 |
Correct |
837 ms |
104444 KB |
Output is correct |
54 |
Correct |
829 ms |
104440 KB |
Output is correct |
55 |
Correct |
844 ms |
105996 KB |
Output is correct |