#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ll long long
#define ii pair<int,int>
#define iii pair<int,pair<int,int>>
#define iil pair<ll,ll>
#define iiil pair<ll,pair<ll,ll>>
#define oo 1e18
#define check(n) (prime[n>>6]&(1<<((n&63)>>1)))
#define set(n) prime[n>>6]|=(1<<((n&63)>>1))
using namespace std;
const int N=5e5+5;
const ll Mod=1e9+7;
const ll MOD=13141702292180207;
const int dx[]={-1,0,0,1};
const int dy[]={0,-1,1,0};
int n;
ll a[N], val[4*N], lz[4*N], gt[4*N], ans[N];
stack<int> s;
vector<int> b[N];
vector<ii> c[N];
void build(int id,int l,int r)
{
if (l>r) return;
if (l==r)
{
val[id]=a[l];
return;
}
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
val[id]=max(val[id*2], val[id*2+1]);
return;
}
void down(int id)
{
lz[id*2]=max(lz[id*2], lz[id]);
lz[id*2+1]=max(lz[id*2+1], lz[id]);
gt[id*2]=max(gt[id*2], lz[id*2] + val[id*2]);
gt[id*2+1]=max(gt[id*2+1], lz[id*2+1] + val[id*2+1]);
}
void up(int id,int l,int r,int u,int v,ll x)
{
if (l>r || v<l || u>r || u>v) return;
if (u<=l && r<=v)
{
lz[id]=max(lz[id],x);
gt[id]=max(gt[id],val[id]+lz[id]);
return;
}
down(id);
int mid=(l+r)/2;
up(id*2,l,mid,u,v,x);
up(id*2+1,mid+1,r,u,v,x);
gt[id]=max(gt[id*2+1], gt[id]);
gt[id]=max(gt[id*2], gt[id]);
return;
}
ll get(int id,int l,int r,int u,int v)
{
if (l>r || u>v || u>r || v<l) return -1e17;
if (u<=l && r<=v) return gt[id];
down(id);
int mid=(l+r)/2;
return max(get(id*2,l,mid,u,v), get(id*2+1,mid+1,r,u,v));
}
void slove()
{
cin >> n; a[0]=1e18;
for (int i=1;i<=n;i++) cin >> a[i];
s.push(0);
for (int i=n;i>=1;i--)
{
while (a[s.top()]<a[i]) s.pop();
if (s.top()>0) b[i].pb(s.top());
s.push(i);
}
while(!s.empty()) s.pop();
s.push(0);
for (int i=1;i<=n;i++)
{
while (a[s.top()]<a[i]) s.pop();
if (s.top()>0) b[s.top()].pb(i);
s.push(i);
}
build(1,1,n);
int q;
cin >> q;
for (int i=1;i<=q;i++)
{
int l,r;
cin >> l >> r;
c[l].pb(ii(r,i));
}
for (int i=n;i>=1;i--)
{
for (int j: b[i])
if (j+j-i<=n) up(1,1,n,j+j-i,n,a[i]+a[j]);
for (ii u: c[i]) ans[u.S]=get(1,1,n,i,u.F);
}
for (int i=1;i<=q;i++) cout << ans[i] << '\n';
}
int main()
{
// freopen("test.inp","r",stdin);
// freopen("test.out","w",stdout);
ios_base::sync_with_stdio(0);
cout.tie(0); cin.tie(0);
int T=1;
//cin >> T;
while(T--) slove();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
29264 KB |
Output is correct |
2 |
Correct |
5 ms |
29392 KB |
Output is correct |
3 |
Correct |
5 ms |
29264 KB |
Output is correct |
4 |
Correct |
5 ms |
29264 KB |
Output is correct |
5 |
Correct |
5 ms |
29264 KB |
Output is correct |
6 |
Correct |
7 ms |
29368 KB |
Output is correct |
7 |
Correct |
5 ms |
29264 KB |
Output is correct |
8 |
Correct |
5 ms |
29264 KB |
Output is correct |
9 |
Correct |
5 ms |
29264 KB |
Output is correct |
10 |
Correct |
5 ms |
29380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
29264 KB |
Output is correct |
2 |
Correct |
5 ms |
29392 KB |
Output is correct |
3 |
Correct |
5 ms |
29264 KB |
Output is correct |
4 |
Correct |
5 ms |
29264 KB |
Output is correct |
5 |
Correct |
5 ms |
29264 KB |
Output is correct |
6 |
Correct |
7 ms |
29368 KB |
Output is correct |
7 |
Correct |
5 ms |
29264 KB |
Output is correct |
8 |
Correct |
5 ms |
29264 KB |
Output is correct |
9 |
Correct |
5 ms |
29264 KB |
Output is correct |
10 |
Correct |
5 ms |
29380 KB |
Output is correct |
11 |
Correct |
202 ms |
48968 KB |
Output is correct |
12 |
Correct |
211 ms |
48980 KB |
Output is correct |
13 |
Correct |
204 ms |
49040 KB |
Output is correct |
14 |
Correct |
223 ms |
48972 KB |
Output is correct |
15 |
Correct |
257 ms |
47188 KB |
Output is correct |
16 |
Correct |
239 ms |
44364 KB |
Output is correct |
17 |
Correct |
239 ms |
44376 KB |
Output is correct |
18 |
Correct |
249 ms |
44364 KB |
Output is correct |
19 |
Correct |
242 ms |
48800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
46920 KB |
Output is correct |
2 |
Correct |
89 ms |
47188 KB |
Output is correct |
3 |
Correct |
98 ms |
47432 KB |
Output is correct |
4 |
Correct |
140 ms |
45896 KB |
Output is correct |
5 |
Correct |
148 ms |
46920 KB |
Output is correct |
6 |
Correct |
131 ms |
45800 KB |
Output is correct |
7 |
Correct |
130 ms |
46664 KB |
Output is correct |
8 |
Correct |
145 ms |
46812 KB |
Output is correct |
9 |
Correct |
137 ms |
48672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
29264 KB |
Output is correct |
2 |
Correct |
5 ms |
29392 KB |
Output is correct |
3 |
Correct |
5 ms |
29264 KB |
Output is correct |
4 |
Correct |
5 ms |
29264 KB |
Output is correct |
5 |
Correct |
5 ms |
29264 KB |
Output is correct |
6 |
Correct |
7 ms |
29368 KB |
Output is correct |
7 |
Correct |
5 ms |
29264 KB |
Output is correct |
8 |
Correct |
5 ms |
29264 KB |
Output is correct |
9 |
Correct |
5 ms |
29264 KB |
Output is correct |
10 |
Correct |
5 ms |
29380 KB |
Output is correct |
11 |
Correct |
202 ms |
48968 KB |
Output is correct |
12 |
Correct |
211 ms |
48980 KB |
Output is correct |
13 |
Correct |
204 ms |
49040 KB |
Output is correct |
14 |
Correct |
223 ms |
48972 KB |
Output is correct |
15 |
Correct |
257 ms |
47188 KB |
Output is correct |
16 |
Correct |
239 ms |
44364 KB |
Output is correct |
17 |
Correct |
239 ms |
44376 KB |
Output is correct |
18 |
Correct |
249 ms |
44364 KB |
Output is correct |
19 |
Correct |
242 ms |
48800 KB |
Output is correct |
20 |
Correct |
143 ms |
46920 KB |
Output is correct |
21 |
Correct |
89 ms |
47188 KB |
Output is correct |
22 |
Correct |
98 ms |
47432 KB |
Output is correct |
23 |
Correct |
140 ms |
45896 KB |
Output is correct |
24 |
Correct |
148 ms |
46920 KB |
Output is correct |
25 |
Correct |
131 ms |
45800 KB |
Output is correct |
26 |
Correct |
130 ms |
46664 KB |
Output is correct |
27 |
Correct |
145 ms |
46812 KB |
Output is correct |
28 |
Correct |
137 ms |
48672 KB |
Output is correct |
29 |
Correct |
900 ms |
100884 KB |
Output is correct |
30 |
Correct |
783 ms |
98160 KB |
Output is correct |
31 |
Correct |
750 ms |
101196 KB |
Output is correct |
32 |
Correct |
855 ms |
98612 KB |
Output is correct |
33 |
Correct |
810 ms |
98380 KB |
Output is correct |
34 |
Correct |
756 ms |
98484 KB |
Output is correct |
35 |
Correct |
793 ms |
97196 KB |
Output is correct |
36 |
Correct |
827 ms |
98248 KB |
Output is correct |
37 |
Correct |
863 ms |
99624 KB |
Output is correct |
38 |
Correct |
631 ms |
106504 KB |
Output is correct |
39 |
Correct |
632 ms |
106576 KB |
Output is correct |
40 |
Correct |
605 ms |
103172 KB |
Output is correct |
41 |
Correct |
604 ms |
102624 KB |
Output is correct |
42 |
Correct |
608 ms |
102732 KB |
Output is correct |
43 |
Correct |
645 ms |
104268 KB |
Output is correct |
44 |
Correct |
655 ms |
105892 KB |
Output is correct |
45 |
Correct |
611 ms |
105908 KB |
Output is correct |
46 |
Correct |
596 ms |
102732 KB |
Output is correct |
47 |
Correct |
593 ms |
102208 KB |
Output is correct |
48 |
Correct |
586 ms |
102180 KB |
Output is correct |
49 |
Correct |
690 ms |
101988 KB |
Output is correct |
50 |
Correct |
811 ms |
104024 KB |
Output is correct |
51 |
Correct |
834 ms |
103840 KB |
Output is correct |
52 |
Correct |
822 ms |
101448 KB |
Output is correct |
53 |
Correct |
773 ms |
101040 KB |
Output is correct |
54 |
Correct |
831 ms |
101196 KB |
Output is correct |
55 |
Correct |
750 ms |
102732 KB |
Output is correct |