#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
using namespace std;
typedef struct qry
{
int st,dr,ind;
} QRY;
int A[500005],St[500005],Dr[500005],Rez[500005],Max[2000005],AddMax[2000005],Ans[2000005];
QRY Q[500005];
vector<int> Per[500005];
bool cmp(QRY a, QRY b)
{
return a.st>b.st;
}
void build(int nod, int st, int dr)
{
if(st==dr)
{
Max[nod]=Ans[nod]=A[st];
return;
}
int mij=(st+dr)/2;
build(2*nod,st,mij);
build(2*nod+1,mij+1,dr);
Max[nod]=max(Max[2*nod],Max[2*nod+1]);
Ans[nod]=max(Ans[2*nod],Ans[2*nod+1]);
}
void update(int nod, int st, int dr, int l, int val)
{
if(l<=st)
{
AddMax[nod]=max(AddMax[nod],val);
return;
}
Ans[nod]=max(Ans[nod],Max[nod]+AddMax[nod]);
AddMax[2*nod]=max(AddMax[2*nod],AddMax[nod]);
AddMax[2*nod+1]=max(AddMax[2*nod+1],AddMax[nod]);
int mij=(st+dr)/2;
if(l<=mij)
update(2*nod,st,mij,l,val);
update(2*nod+1,mij+1,dr,l,val);
Ans[nod]=max(max(Max[2*nod]+AddMax[2*nod],Ans[2*nod]),max(Max[2*nod+1]+AddMax[2*nod+1],Ans[2*nod+1]));
}
int query(int nod, int st, int dr, int l, int r)
{
Ans[nod]=max(Ans[nod],Max[nod]+AddMax[nod]);
AddMax[2*nod]=max(AddMax[2*nod],AddMax[nod]);
AddMax[2*nod+1]=max(AddMax[2*nod+1],AddMax[nod]);
if(l<=st && dr<=r)
return Ans[nod];
int mij=(st+dr)/2;
int rez=0;
if(l<=mij)
rez=max(rez,query(2*nod,st,mij,l,r));
if(mij<r)
rez=max(rez,query(2*nod+1,mij+1,dr,l,r));
return rez;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1; i<=n; i++)
cin>>A[i];
deque<int> S;
for(int i=1; i<=n; i++)
{
while(!S.empty() && A[S.back()]<=A[i])
{
Dr[S.back()]=i;
S.pop_back();
}
if(!S.empty())
St[i]=S.back();
S.push_back(i);
}
//max 2*n perechi relevante pt a si b
for(int i=1; i<=n; i++)
{
if(St[i]!=0)
Per[St[i]].push_back(i);
if(Dr[i]!=0)
Per[i].push_back(Dr[i]);
}
int q;
cin>>q;
for(int i=1; i<=q; i++)
{
cin>>Q[i].st>>Q[i].dr;
Q[i].ind=i;
}
sort(Q+1,Q+q+1,cmp);
build(1,1,n);
int ind=n;
for(int i=1; i<=q; i++)
{
while(ind>=Q[i].st)
{
for(auto y:Per[ind])
{
if(y+y-ind<=n)
update(1,1,n,y+y-ind,A[ind]+A[y]);
}
ind--;
}
Rez[Q[i].ind]=query(1,1,n,Q[i].st,Q[i].dr);
}
for(int i=1; i<=q; i++)
cout<<Rez[i]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
16 ms |
12152 KB |
Output is correct |
3 |
Correct |
14 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
11 ms |
12164 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
5 ms |
12152 KB |
Output is correct |
8 |
Correct |
13 ms |
12152 KB |
Output is correct |
9 |
Correct |
30 ms |
12112 KB |
Output is correct |
10 |
Correct |
15 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
16 ms |
12152 KB |
Output is correct |
3 |
Correct |
14 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
11 ms |
12164 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
5 ms |
12152 KB |
Output is correct |
8 |
Correct |
13 ms |
12152 KB |
Output is correct |
9 |
Correct |
30 ms |
12112 KB |
Output is correct |
10 |
Correct |
15 ms |
12152 KB |
Output is correct |
11 |
Correct |
391 ms |
30192 KB |
Output is correct |
12 |
Correct |
384 ms |
30240 KB |
Output is correct |
13 |
Correct |
384 ms |
30104 KB |
Output is correct |
14 |
Correct |
396 ms |
30144 KB |
Output is correct |
15 |
Correct |
387 ms |
30292 KB |
Output is correct |
16 |
Correct |
436 ms |
29768 KB |
Output is correct |
17 |
Correct |
399 ms |
29688 KB |
Output is correct |
18 |
Correct |
434 ms |
29560 KB |
Output is correct |
19 |
Correct |
383 ms |
30116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
27052 KB |
Output is correct |
2 |
Correct |
115 ms |
26104 KB |
Output is correct |
3 |
Correct |
114 ms |
29176 KB |
Output is correct |
4 |
Correct |
198 ms |
28892 KB |
Output is correct |
5 |
Correct |
198 ms |
28920 KB |
Output is correct |
6 |
Correct |
193 ms |
28320 KB |
Output is correct |
7 |
Correct |
194 ms |
28152 KB |
Output is correct |
8 |
Correct |
192 ms |
28152 KB |
Output is correct |
9 |
Correct |
199 ms |
28504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
16 ms |
12152 KB |
Output is correct |
3 |
Correct |
14 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
11 ms |
12164 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
5 ms |
12152 KB |
Output is correct |
8 |
Correct |
13 ms |
12152 KB |
Output is correct |
9 |
Correct |
30 ms |
12112 KB |
Output is correct |
10 |
Correct |
15 ms |
12152 KB |
Output is correct |
11 |
Correct |
391 ms |
30192 KB |
Output is correct |
12 |
Correct |
384 ms |
30240 KB |
Output is correct |
13 |
Correct |
384 ms |
30104 KB |
Output is correct |
14 |
Correct |
396 ms |
30144 KB |
Output is correct |
15 |
Correct |
387 ms |
30292 KB |
Output is correct |
16 |
Correct |
436 ms |
29768 KB |
Output is correct |
17 |
Correct |
399 ms |
29688 KB |
Output is correct |
18 |
Correct |
434 ms |
29560 KB |
Output is correct |
19 |
Correct |
383 ms |
30116 KB |
Output is correct |
20 |
Correct |
198 ms |
27052 KB |
Output is correct |
21 |
Correct |
115 ms |
26104 KB |
Output is correct |
22 |
Correct |
114 ms |
29176 KB |
Output is correct |
23 |
Correct |
198 ms |
28892 KB |
Output is correct |
24 |
Correct |
198 ms |
28920 KB |
Output is correct |
25 |
Correct |
193 ms |
28320 KB |
Output is correct |
26 |
Correct |
194 ms |
28152 KB |
Output is correct |
27 |
Correct |
192 ms |
28152 KB |
Output is correct |
28 |
Correct |
199 ms |
28504 KB |
Output is correct |
29 |
Incorrect |
1165 ms |
74092 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |