#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=500050;
const int M=2*N;
const int inf=1e9+7;
int a[N],ls[M],rs[M],tsz,root,mx1[M],mx2[M],lzy[M];
void Build(int &c, int ss, int se)
{
c=++tsz;mx2[c]=-inf;lzy[c]=-inf;
if(ss==se){ mx1[c]=a[ss];return;}
int mid=ss+se>>1;
Build(ls[c],ss,mid);
Build(rs[c],mid+1,se);
mx1[c]=max(mx1[ls[c]],mx1[rs[c]]);
}
void Upd(int c, int x)
{
mx2[c]=max(mx2[c],mx1[c]+x);
lzy[c]=max(lzy[c],x);
}
void Push(int c)
{
if(lzy[c]!=-inf)
{
Upd(ls[c],lzy[c]);
Upd(rs[c],lzy[c]);
lzy[c]=-inf;
}
}
void Set(int c, int ss, int se, int qs, int qe, int x)
{
if(qs>qe || qs>se || ss>qe) return;
if(qs<=ss && qe>=se){ Upd(c,x);return;}
Push(c);
int mid=ss+se>>1;
Set(ls[c],ss,mid,qs,qe,x);
Set(rs[c],mid+1,se,qs,qe,x);
mx2[c]=max(mx2[ls[c]],mx2[rs[c]]);
}
int Get(int c, int ss, int se, int qs, int qe)
{
if(qs>qe || qs>se || ss>qe) return -inf;
if(qs<=ss && qe>=se) return mx2[c];
Push(c);
int mid=ss+se>>1;
return max(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
int n,q,ans[N];
vector<pair<int,int>> Qs[N];
void Add(int x, int y)
{
int z=y+y-x;
//printf("%i %i %i\n",x,y,z);
Set(root,1,n,z,n,a[x]+a[y]);
}
void Solve()
{
stack<int> st;
for(int i=n;i>=1;i--)
{
while(st.size() && a[st.top()]<=a[i])
{
Add(i,st.top());
st.pop();
}
if(st.size()) Add(i,st.top());
st.push(i);
for(auto qu:Qs[i])
{
ans[qu.second]=Get(root,1,n,i,qu.first);
//printf("%i:(%i %i)\n",qu.second,i,qu.first);
}
}
//for(int i=1;i<=n;i++) printf("%i ",Get(root,1,n,i,i));printf("\n");
}
int main()
{
scanf("%i",&n);
for(int i=1;i<=n;i++) scanf("%i",&a[i]);
Build(root,1,n);
scanf("%i",&q);
for(int i=1;i<=q;i++)
{
int l,r;
scanf("%i %i",&l,&r);
Qs[l].pb({r,i});
}
Solve();
for(int i=1;i<=q;i++) printf("%i\n",ans[i]);
return 0;
}
Compilation message
jumps.cpp: In function 'void Build(int&, int, int)':
jumps.cpp:12:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
12 | int mid=ss+se>>1;
| ~~^~~
jumps.cpp: In function 'void Set(int, int, int, int, int, int)':
jumps.cpp:36:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int mid=ss+se>>1;
| ~~^~~
jumps.cpp: In function 'int Get(int, int, int, int, int)':
jumps.cpp:46:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int mid=ss+se>>1;
| ~~^~~
jumps.cpp: In function 'int main()':
jumps.cpp:79:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
79 | scanf("%i",&n);
| ~~~~~^~~~~~~~~
jumps.cpp:80:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
80 | for(int i=1;i<=n;i++) scanf("%i",&a[i]);
| ~~~~~^~~~~~~~~~~~
jumps.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
82 | scanf("%i",&q);
| ~~~~~^~~~~~~~~
jumps.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
86 | scanf("%i %i",&l,&r);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
8 ms |
12140 KB |
Output is correct |
4 |
Correct |
8 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
8 ms |
12140 KB |
Output is correct |
7 |
Correct |
8 ms |
12140 KB |
Output is correct |
8 |
Correct |
8 ms |
12140 KB |
Output is correct |
9 |
Correct |
8 ms |
12140 KB |
Output is correct |
10 |
Correct |
8 ms |
12140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
8 ms |
12140 KB |
Output is correct |
4 |
Correct |
8 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
8 ms |
12140 KB |
Output is correct |
7 |
Correct |
8 ms |
12140 KB |
Output is correct |
8 |
Correct |
8 ms |
12140 KB |
Output is correct |
9 |
Correct |
8 ms |
12140 KB |
Output is correct |
10 |
Correct |
8 ms |
12140 KB |
Output is correct |
11 |
Correct |
410 ms |
30956 KB |
Output is correct |
12 |
Correct |
381 ms |
31084 KB |
Output is correct |
13 |
Correct |
376 ms |
30956 KB |
Output is correct |
14 |
Correct |
377 ms |
30956 KB |
Output is correct |
15 |
Correct |
391 ms |
30956 KB |
Output is correct |
16 |
Correct |
391 ms |
30444 KB |
Output is correct |
17 |
Correct |
375 ms |
30316 KB |
Output is correct |
18 |
Correct |
388 ms |
30316 KB |
Output is correct |
19 |
Correct |
394 ms |
30828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
22636 KB |
Output is correct |
2 |
Correct |
104 ms |
23360 KB |
Output is correct |
3 |
Correct |
104 ms |
22508 KB |
Output is correct |
4 |
Correct |
170 ms |
22508 KB |
Output is correct |
5 |
Correct |
170 ms |
22508 KB |
Output is correct |
6 |
Correct |
166 ms |
21868 KB |
Output is correct |
7 |
Correct |
166 ms |
21740 KB |
Output is correct |
8 |
Correct |
169 ms |
21740 KB |
Output is correct |
9 |
Correct |
169 ms |
22208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
8 ms |
12140 KB |
Output is correct |
4 |
Correct |
8 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
8 ms |
12140 KB |
Output is correct |
7 |
Correct |
8 ms |
12140 KB |
Output is correct |
8 |
Correct |
8 ms |
12140 KB |
Output is correct |
9 |
Correct |
8 ms |
12140 KB |
Output is correct |
10 |
Correct |
8 ms |
12140 KB |
Output is correct |
11 |
Correct |
410 ms |
30956 KB |
Output is correct |
12 |
Correct |
381 ms |
31084 KB |
Output is correct |
13 |
Correct |
376 ms |
30956 KB |
Output is correct |
14 |
Correct |
377 ms |
30956 KB |
Output is correct |
15 |
Correct |
391 ms |
30956 KB |
Output is correct |
16 |
Correct |
391 ms |
30444 KB |
Output is correct |
17 |
Correct |
375 ms |
30316 KB |
Output is correct |
18 |
Correct |
388 ms |
30316 KB |
Output is correct |
19 |
Correct |
394 ms |
30828 KB |
Output is correct |
20 |
Correct |
168 ms |
22636 KB |
Output is correct |
21 |
Correct |
104 ms |
23360 KB |
Output is correct |
22 |
Correct |
104 ms |
22508 KB |
Output is correct |
23 |
Correct |
170 ms |
22508 KB |
Output is correct |
24 |
Correct |
170 ms |
22508 KB |
Output is correct |
25 |
Correct |
166 ms |
21868 KB |
Output is correct |
26 |
Correct |
166 ms |
21740 KB |
Output is correct |
27 |
Correct |
169 ms |
21740 KB |
Output is correct |
28 |
Correct |
169 ms |
22208 KB |
Output is correct |
29 |
Correct |
1520 ms |
61680 KB |
Output is correct |
30 |
Correct |
1388 ms |
61744 KB |
Output is correct |
31 |
Correct |
1309 ms |
61420 KB |
Output is correct |
32 |
Correct |
1507 ms |
61804 KB |
Output is correct |
33 |
Correct |
1510 ms |
61500 KB |
Output is correct |
34 |
Correct |
1502 ms |
59244 KB |
Output is correct |
35 |
Correct |
1465 ms |
58844 KB |
Output is correct |
36 |
Correct |
1519 ms |
58896 KB |
Output is correct |
37 |
Correct |
1494 ms |
60396 KB |
Output is correct |
38 |
Correct |
908 ms |
67308 KB |
Output is correct |
39 |
Correct |
925 ms |
67180 KB |
Output is correct |
40 |
Correct |
889 ms |
63980 KB |
Output is correct |
41 |
Correct |
890 ms |
63560 KB |
Output is correct |
42 |
Correct |
892 ms |
63468 KB |
Output is correct |
43 |
Correct |
883 ms |
65120 KB |
Output is correct |
44 |
Correct |
988 ms |
66668 KB |
Output is correct |
45 |
Correct |
980 ms |
66796 KB |
Output is correct |
46 |
Correct |
965 ms |
63596 KB |
Output is correct |
47 |
Correct |
978 ms |
63116 KB |
Output is correct |
48 |
Correct |
967 ms |
63116 KB |
Output is correct |
49 |
Correct |
966 ms |
65132 KB |
Output is correct |
50 |
Correct |
1127 ms |
65004 KB |
Output is correct |
51 |
Correct |
1125 ms |
64876 KB |
Output is correct |
52 |
Correct |
1084 ms |
62444 KB |
Output is correct |
53 |
Correct |
1087 ms |
61924 KB |
Output is correct |
54 |
Correct |
1097 ms |
61820 KB |
Output is correct |
55 |
Correct |
1145 ms |
63712 KB |
Output is correct |