#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define semicolon ;
#define ryan bear
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
struct node
{
ll am,bm,sm;
node(ll x=-INF,ll y=-INF):am(x),bm(y),sm(x+y){}
node(ll x,ll y,ll z):am(x),bm(y),sm(z){}
node operator^(node x)
{
return node(max(am,x.am),max(bm,x.bm),max({sm,x.sm,bm+x.am}));
}
};
ll a[500010],b[500010];
struct seg
{
node t[2000010];
void si(int n,int s,int e,int x)
{
if(s==e)
{
t[n]=node(a[s],b[s]);
return;
}
int m=s+e>>1;
if(x>m)
si(n*2+1,m+1,e,x);
else
si(n*2,s,m,x);
t[n]=t[n*2]^t[n*2+1];
return;
}
node sq(int n,int s,int e,int S,int E)
{
if(s>E||S>e)
return node();
if(S<=s&&e<=E)
return t[n];
int m=s+e>>1;
return sq(n*2,s,m,S,E)^sq(n*2+1,m+1,e,S,E);
}
}st;
vector<pl>qv[500010];
vector<pi>av[500010];
ll ans[500010];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,q;
cin>>n;
for(int i=0;i++<n;)
cin>>a[i],b[i]=-INF;
vector<int>v;
v.eb(n);
for(int i=n;--i>0;)
{
while(!v.empty())
{
int j=v.back();
if(2*j-i<=n)
qv[i].eb(2*j-i,a[i]+a[j]);
if(a[j]<=a[i])
v.pop_back();
else
break;
}
v.eb(i);
}
cin>>q;
for(int i=0;i<q;i++)
{
int l,r;
cin>>l>>r;
av[l].eb(r,i);
}
for(int i=n;i>0;i--)
{
for(pl&t:qv[i])
if(b[t.fi]<t.se)
b[t.fi]=t.se,st.si(1,1,n,t.fi);
for(pi&t:av[i])
ans[t.se]=st.sq(1,1,n,i+2,t.fi).sm;
}
for(int i=0;i<q;i++)
cout<<ans[i]<<'\n';
cout.flush();
return 0;
}
Compilation message
jumps.cpp: In member function 'void seg::si(int, int, int, int)':
jumps.cpp:38:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m=s+e>>1;
~^~
jumps.cpp: In member function 'node seg::sq(int, int, int, int, int)':
jumps.cpp:52:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m=s+e>>1;
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
70776 KB |
Output is correct |
2 |
Correct |
41 ms |
70776 KB |
Output is correct |
3 |
Correct |
39 ms |
70776 KB |
Output is correct |
4 |
Correct |
39 ms |
70852 KB |
Output is correct |
5 |
Correct |
39 ms |
70904 KB |
Output is correct |
6 |
Correct |
39 ms |
70780 KB |
Output is correct |
7 |
Correct |
39 ms |
70848 KB |
Output is correct |
8 |
Correct |
39 ms |
70776 KB |
Output is correct |
9 |
Correct |
42 ms |
70856 KB |
Output is correct |
10 |
Correct |
41 ms |
70776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
70776 KB |
Output is correct |
2 |
Correct |
41 ms |
70776 KB |
Output is correct |
3 |
Correct |
39 ms |
70776 KB |
Output is correct |
4 |
Correct |
39 ms |
70852 KB |
Output is correct |
5 |
Correct |
39 ms |
70904 KB |
Output is correct |
6 |
Correct |
39 ms |
70780 KB |
Output is correct |
7 |
Correct |
39 ms |
70848 KB |
Output is correct |
8 |
Correct |
39 ms |
70776 KB |
Output is correct |
9 |
Correct |
42 ms |
70856 KB |
Output is correct |
10 |
Correct |
41 ms |
70776 KB |
Output is correct |
11 |
Correct |
333 ms |
91184 KB |
Output is correct |
12 |
Correct |
338 ms |
91232 KB |
Output is correct |
13 |
Correct |
337 ms |
91032 KB |
Output is correct |
14 |
Correct |
389 ms |
91256 KB |
Output is correct |
15 |
Correct |
374 ms |
91192 KB |
Output is correct |
16 |
Correct |
374 ms |
90536 KB |
Output is correct |
17 |
Correct |
364 ms |
90488 KB |
Output is correct |
18 |
Correct |
385 ms |
90520 KB |
Output is correct |
19 |
Correct |
363 ms |
91128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
85880 KB |
Output is correct |
2 |
Correct |
108 ms |
82796 KB |
Output is correct |
3 |
Correct |
111 ms |
81912 KB |
Output is correct |
4 |
Correct |
158 ms |
86008 KB |
Output is correct |
5 |
Correct |
145 ms |
85880 KB |
Output is correct |
6 |
Correct |
138 ms |
85240 KB |
Output is correct |
7 |
Correct |
142 ms |
85240 KB |
Output is correct |
8 |
Correct |
140 ms |
85240 KB |
Output is correct |
9 |
Correct |
139 ms |
85496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
70776 KB |
Output is correct |
2 |
Correct |
41 ms |
70776 KB |
Output is correct |
3 |
Correct |
39 ms |
70776 KB |
Output is correct |
4 |
Correct |
39 ms |
70852 KB |
Output is correct |
5 |
Correct |
39 ms |
70904 KB |
Output is correct |
6 |
Correct |
39 ms |
70780 KB |
Output is correct |
7 |
Correct |
39 ms |
70848 KB |
Output is correct |
8 |
Correct |
39 ms |
70776 KB |
Output is correct |
9 |
Correct |
42 ms |
70856 KB |
Output is correct |
10 |
Correct |
41 ms |
70776 KB |
Output is correct |
11 |
Correct |
333 ms |
91184 KB |
Output is correct |
12 |
Correct |
338 ms |
91232 KB |
Output is correct |
13 |
Correct |
337 ms |
91032 KB |
Output is correct |
14 |
Correct |
389 ms |
91256 KB |
Output is correct |
15 |
Correct |
374 ms |
91192 KB |
Output is correct |
16 |
Correct |
374 ms |
90536 KB |
Output is correct |
17 |
Correct |
364 ms |
90488 KB |
Output is correct |
18 |
Correct |
385 ms |
90520 KB |
Output is correct |
19 |
Correct |
363 ms |
91128 KB |
Output is correct |
20 |
Correct |
147 ms |
85880 KB |
Output is correct |
21 |
Correct |
108 ms |
82796 KB |
Output is correct |
22 |
Correct |
111 ms |
81912 KB |
Output is correct |
23 |
Correct |
158 ms |
86008 KB |
Output is correct |
24 |
Correct |
145 ms |
85880 KB |
Output is correct |
25 |
Correct |
138 ms |
85240 KB |
Output is correct |
26 |
Correct |
142 ms |
85240 KB |
Output is correct |
27 |
Correct |
140 ms |
85240 KB |
Output is correct |
28 |
Correct |
139 ms |
85496 KB |
Output is correct |
29 |
Correct |
1093 ms |
133004 KB |
Output is correct |
30 |
Correct |
965 ms |
124820 KB |
Output is correct |
31 |
Correct |
981 ms |
122880 KB |
Output is correct |
32 |
Correct |
1109 ms |
133060 KB |
Output is correct |
33 |
Correct |
1093 ms |
133112 KB |
Output is correct |
34 |
Correct |
1439 ms |
131852 KB |
Output is correct |
35 |
Correct |
1185 ms |
131576 KB |
Output is correct |
36 |
Correct |
1333 ms |
131624 KB |
Output is correct |
37 |
Correct |
2538 ms |
132984 KB |
Output is correct |
38 |
Correct |
771 ms |
138656 KB |
Output is correct |
39 |
Correct |
1234 ms |
138704 KB |
Output is correct |
40 |
Correct |
1044 ms |
136568 KB |
Output is correct |
41 |
Execution timed out |
284 ms |
118696 KB |
Time limit exceeded (wall clock) |
42 |
Halted |
0 ms |
0 KB |
- |