#include<bits/stdc++.h>
using namespace std;
const int nmax=(1<<19)+42,MX=20;
int n,q;
int inp[nmax];
vector< pair<int,int> > start;
struct info
{
int best_sum;
int mx_first;
int mx_second;
};
info tree[2*nmax];
info my_merge(info l,info r)
{
info ret;
ret.mx_first=max(l.mx_first,r.mx_first);
ret.mx_second=max(l.mx_second,r.mx_second);
ret.best_sum=max(l.best_sum,r.best_sum);
ret.best_sum=max(ret.best_sum,l.mx_first+r.mx_second);
return ret;
}
void update(int node,int l,int r,int pos,pair<int,int> val)
{
if(l==r)
{
tree[node].mx_first=max(tree[node].mx_first,val.first);
tree[node].mx_second=val.second;
tree[node].best_sum=tree[node].mx_first+tree[node].mx_second;
return;
}
int av=(l+r)/2;
if(pos<=av)update(node*2,l,av,pos,val);
else update(node*2+1,av+1,r,pos,val);
tree[node]=my_merge(tree[node*2],tree[node*2+1]);
}
info query(int node,int l,int r,int lq,int rq)
{
if(l==lq&&r==rq)return tree[node];
int av=(l+r)/2;
info ret;ret.best_sum=0;ret.mx_first=0;ret.mx_second=0;
if(lq<=av)ret=my_merge(ret,query(node*2,l,av,lq,min(av,rq)));
if(av<rq)ret=my_merge(ret,query(node*2+1,av+1,r,max(av+1,lq),rq));
return ret;
}
stack<int> active;
struct que
{
int l,r,id;
};
que help[nmax];
bool cmp(que a,que b)
{
return a.l>b.l;
}
int output[nmax];
int main()
{
scanf("%i",&n);
for(int i=1;i<=n;i++)scanf("%i",&inp[i]);
for(int i=1;i<=n;i++)
update(1,1,n,i,{0,inp[i]});
for(int i=1;i<=n;i++)
{
while(active.size()&&inp[active.top()]<=inp[i])
{
start.push_back({active.top(),i});
active.pop();
}
if(active.size())start.push_back({active.top(),i});
active.push(i);
}
sort(start.begin(),start.end());
reverse(start.begin(),start.end());
/*
cout<<"start: "<<endl;
for(auto k:start)cout<<k.first<<" "<<k.second<<endl;
*/
scanf("%i",&q);
for(int i=1;i<=q;i++)
{
scanf("%i%i",&help[i].l,&help[i].r);
help[i].id=i;
}
sort(help+1,help+q+1,cmp);
int pointer_start=0;
int pointer_help=1;
for(int l=n;l>=1;l--)
{
//cout<<"l= "<<l<<" "<<start[pointer_start].first<<" "<<pointer_start<<" "<<pointer_help<<endl;
while(pointer_start<start.size()&&start[pointer_start].first==l)
{
int a=start[pointer_start].first;
int b=start[pointer_start].second;
int c=b+b-a;
//cout<<"a= "<<a<<" b= "<<b<<" c= "<<c<<endl;
if(c<=n)
{
update(1,1,n,c,{inp[a]+inp[b],inp[c]});
//cout<<"update "<<c<<" "<<inp[a]+inp[b]<<" with "<<inp[c]<<endl;
}
pointer_start++;
}
while(pointer_help<=q&&help[pointer_help].l==l)
{
output[help[pointer_help].id]=query(1,1,n,help[pointer_help].l,help[pointer_help].r).best_sum;
pointer_help++;
}
}
for(int i=1;i<=q;i++)
printf("%i\n",output[i]);
return 0;
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:115:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(pointer_start<start.size()&&start[pointer_start].first==l)
~~~~~~~~~~~~~^~~~~~~~~~~~~
jumps.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&n);
~~~~~^~~~~~~~~
jumps.cpp:74:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++)scanf("%i",&inp[i]);
~~~~~^~~~~~~~~~~~~~
jumps.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&q);
~~~~~^~~~~~~~~
jumps.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i%i",&help[i].l,&help[i].r);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
655 ms |
18372 KB |
Output is correct |
12 |
Correct |
660 ms |
18124 KB |
Output is correct |
13 |
Correct |
661 ms |
18124 KB |
Output is correct |
14 |
Correct |
665 ms |
18296 KB |
Output is correct |
15 |
Correct |
661 ms |
18296 KB |
Output is correct |
16 |
Correct |
645 ms |
17648 KB |
Output is correct |
17 |
Correct |
660 ms |
17656 KB |
Output is correct |
18 |
Correct |
644 ms |
17648 KB |
Output is correct |
19 |
Correct |
652 ms |
18280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
13264 KB |
Output is correct |
2 |
Correct |
183 ms |
11248 KB |
Output is correct |
3 |
Correct |
182 ms |
11604 KB |
Output is correct |
4 |
Correct |
275 ms |
13288 KB |
Output is correct |
5 |
Correct |
273 ms |
13288 KB |
Output is correct |
6 |
Correct |
269 ms |
12648 KB |
Output is correct |
7 |
Correct |
266 ms |
12496 KB |
Output is correct |
8 |
Correct |
263 ms |
12496 KB |
Output is correct |
9 |
Correct |
275 ms |
12776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
655 ms |
18372 KB |
Output is correct |
12 |
Correct |
660 ms |
18124 KB |
Output is correct |
13 |
Correct |
661 ms |
18124 KB |
Output is correct |
14 |
Correct |
665 ms |
18296 KB |
Output is correct |
15 |
Correct |
661 ms |
18296 KB |
Output is correct |
16 |
Correct |
645 ms |
17648 KB |
Output is correct |
17 |
Correct |
660 ms |
17656 KB |
Output is correct |
18 |
Correct |
644 ms |
17648 KB |
Output is correct |
19 |
Correct |
652 ms |
18280 KB |
Output is correct |
20 |
Correct |
277 ms |
13264 KB |
Output is correct |
21 |
Correct |
183 ms |
11248 KB |
Output is correct |
22 |
Correct |
182 ms |
11604 KB |
Output is correct |
23 |
Correct |
275 ms |
13288 KB |
Output is correct |
24 |
Correct |
273 ms |
13288 KB |
Output is correct |
25 |
Correct |
269 ms |
12648 KB |
Output is correct |
26 |
Correct |
266 ms |
12496 KB |
Output is correct |
27 |
Correct |
263 ms |
12496 KB |
Output is correct |
28 |
Correct |
275 ms |
12776 KB |
Output is correct |
29 |
Correct |
1743 ms |
46440 KB |
Output is correct |
30 |
Correct |
1528 ms |
42392 KB |
Output is correct |
31 |
Correct |
1529 ms |
44332 KB |
Output is correct |
32 |
Correct |
1730 ms |
46412 KB |
Output is correct |
33 |
Correct |
1684 ms |
46464 KB |
Output is correct |
34 |
Correct |
1670 ms |
44152 KB |
Output is correct |
35 |
Correct |
1659 ms |
43772 KB |
Output is correct |
36 |
Correct |
1672 ms |
43856 KB |
Output is correct |
37 |
Correct |
1652 ms |
45264 KB |
Output is correct |
38 |
Correct |
1440 ms |
46564 KB |
Output is correct |
39 |
Correct |
1447 ms |
46544 KB |
Output is correct |
40 |
Correct |
1418 ms |
43052 KB |
Output is correct |
41 |
Correct |
1393 ms |
42576 KB |
Output is correct |
42 |
Correct |
1401 ms |
42832 KB |
Output is correct |
43 |
Correct |
1434 ms |
44436 KB |
Output is correct |
44 |
Correct |
1535 ms |
46532 KB |
Output is correct |
45 |
Correct |
1492 ms |
46676 KB |
Output is correct |
46 |
Correct |
1486 ms |
43348 KB |
Output is correct |
47 |
Correct |
1488 ms |
43004 KB |
Output is correct |
48 |
Correct |
1494 ms |
43088 KB |
Output is correct |
49 |
Correct |
1499 ms |
45040 KB |
Output is correct |
50 |
Correct |
1559 ms |
46544 KB |
Output is correct |
51 |
Correct |
1569 ms |
46544 KB |
Output is correct |
52 |
Correct |
1548 ms |
44112 KB |
Output is correct |
53 |
Correct |
1548 ms |
43856 KB |
Output is correct |
54 |
Correct |
1544 ms |
43784 KB |
Output is correct |
55 |
Correct |
1594 ms |
45524 KB |
Output is correct |