#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXN = 2e5+5;
vector<ll>a(MAXN);
int n,Q;
vector<ll>t(4*MAXN),amx(4*MAXN),lazy(4*MAXN,-INF);
void upd(int v,int l,int r,int tl,int tr,ll val){
if(l > r)return;
if(l == tl && tr == r){
lazy[v] = max(lazy[v],val);
t[v] = max(t[v],amx[v]+lazy[v]);
return;
}
int tm = (tl+tr)/2;
upd(2*v,l,min(r,tm),tl,tm,val),
upd(2*v+1,max(tm+1,l),r,tm+1,tr,val);
t[v] = max({t[2*v],t[2*v+1],lazy[v]+amx[v]});
}
PII query(int v,int l,int r,int tl,int tr){
if(l > r)return {0,0};
if(l == tl && r == tr)return {t[v],amx[v]};
int tm = (tl+tr)/2;
PII a = query(2*v,l,min(r,tm),tl,tm);
PII b = query(2*v+1,max(tm+1,l),r,tm+1,tr);
PII res;
res.fi = max({a.fi,b.fi,max(a.se,b.se)+lazy[v]});
res.se = max(a.se,b.se);
return res;
}
void build(int v,int l,int r){
if(l == r){
amx[v] = a[l];
return;
}
int tm = (l+r)/2;
build(2*v,l,tm);
build(2*v+1,tm+1,r);
amx[v] = max(amx[2*v],amx[2*v+1]);
}
struct que{
int l,r,idx;
bool operator<(const que &z){return z.l < l;}
que(){}
};
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
cin>>Q;
vector<que>q(Q);
vii can(n+1);
for(int i=0;i<Q;i++){
cin>>q[i].l>>q[i].r;
q[i].idx = i;
}
stack<pii>stk;
build(1,1,n);
for(int i=1;i<=n;i++){
while(!stk.empty() && stk.top().fi < a[i])stk.pop();
if(!stk.empty())can[stk.top().se].pb(i);
stk.push({a[i],i});
}
while(!stk.empty())stk.pop();
for(int i=n;i;i--){
while(!stk.empty() && stk.top().fi < a[i])stk.pop();
if(!stk.empty())can[i].pb(stk.top().se);
stk.push({a[i],i});
}
vector<ll>ans(Q);
int ptr = 0;
sort(all(q));
for(int i=n;i>0;i--){
int st = ptr;
while(ptr<sz(q) && q[ptr].l >= i)ptr++;
for(int x:can[i]){
int rg = 2*x-i;
if(rg<=n)upd(1,rg,n,1,n,a[i]+a[x]);
}
for(int j=st;j<ptr;j++){
ans[q[j].idx] = query(1,q[j].l,q[j].r,1,n).fi;
}
}
for(int i=0;i<Q;i++)cout<<ans[i]<<'\n';
}
/*
5
5 2 1 5 3
3
1 4
2 5
1 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
20556 KB |
Output is correct |
2 |
Correct |
10 ms |
20664 KB |
Output is correct |
3 |
Correct |
10 ms |
20664 KB |
Output is correct |
4 |
Correct |
11 ms |
20660 KB |
Output is correct |
5 |
Correct |
11 ms |
20660 KB |
Output is correct |
6 |
Correct |
11 ms |
20592 KB |
Output is correct |
7 |
Correct |
11 ms |
20556 KB |
Output is correct |
8 |
Correct |
12 ms |
20556 KB |
Output is correct |
9 |
Correct |
13 ms |
20660 KB |
Output is correct |
10 |
Correct |
10 ms |
20636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
20556 KB |
Output is correct |
2 |
Correct |
10 ms |
20664 KB |
Output is correct |
3 |
Correct |
10 ms |
20664 KB |
Output is correct |
4 |
Correct |
11 ms |
20660 KB |
Output is correct |
5 |
Correct |
11 ms |
20660 KB |
Output is correct |
6 |
Correct |
11 ms |
20592 KB |
Output is correct |
7 |
Correct |
11 ms |
20556 KB |
Output is correct |
8 |
Correct |
12 ms |
20556 KB |
Output is correct |
9 |
Correct |
13 ms |
20660 KB |
Output is correct |
10 |
Correct |
10 ms |
20636 KB |
Output is correct |
11 |
Correct |
550 ms |
40408 KB |
Output is correct |
12 |
Correct |
563 ms |
40408 KB |
Output is correct |
13 |
Correct |
548 ms |
40528 KB |
Output is correct |
14 |
Correct |
586 ms |
40400 KB |
Output is correct |
15 |
Correct |
551 ms |
40432 KB |
Output is correct |
16 |
Correct |
553 ms |
39756 KB |
Output is correct |
17 |
Correct |
564 ms |
39628 KB |
Output is correct |
18 |
Correct |
554 ms |
39632 KB |
Output is correct |
19 |
Correct |
546 ms |
40284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
273 ms |
33564 KB |
Output is correct |
2 |
Correct |
164 ms |
34964 KB |
Output is correct |
3 |
Correct |
174 ms |
35016 KB |
Output is correct |
4 |
Correct |
232 ms |
33564 KB |
Output is correct |
5 |
Correct |
262 ms |
33600 KB |
Output is correct |
6 |
Correct |
213 ms |
32796 KB |
Output is correct |
7 |
Correct |
204 ms |
32800 KB |
Output is correct |
8 |
Correct |
245 ms |
32804 KB |
Output is correct |
9 |
Correct |
218 ms |
33136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
20556 KB |
Output is correct |
2 |
Correct |
10 ms |
20664 KB |
Output is correct |
3 |
Correct |
10 ms |
20664 KB |
Output is correct |
4 |
Correct |
11 ms |
20660 KB |
Output is correct |
5 |
Correct |
11 ms |
20660 KB |
Output is correct |
6 |
Correct |
11 ms |
20592 KB |
Output is correct |
7 |
Correct |
11 ms |
20556 KB |
Output is correct |
8 |
Correct |
12 ms |
20556 KB |
Output is correct |
9 |
Correct |
13 ms |
20660 KB |
Output is correct |
10 |
Correct |
10 ms |
20636 KB |
Output is correct |
11 |
Correct |
550 ms |
40408 KB |
Output is correct |
12 |
Correct |
563 ms |
40408 KB |
Output is correct |
13 |
Correct |
548 ms |
40528 KB |
Output is correct |
14 |
Correct |
586 ms |
40400 KB |
Output is correct |
15 |
Correct |
551 ms |
40432 KB |
Output is correct |
16 |
Correct |
553 ms |
39756 KB |
Output is correct |
17 |
Correct |
564 ms |
39628 KB |
Output is correct |
18 |
Correct |
554 ms |
39632 KB |
Output is correct |
19 |
Correct |
546 ms |
40284 KB |
Output is correct |
20 |
Correct |
273 ms |
33564 KB |
Output is correct |
21 |
Correct |
164 ms |
34964 KB |
Output is correct |
22 |
Correct |
174 ms |
35016 KB |
Output is correct |
23 |
Correct |
232 ms |
33564 KB |
Output is correct |
24 |
Correct |
262 ms |
33600 KB |
Output is correct |
25 |
Correct |
213 ms |
32796 KB |
Output is correct |
26 |
Correct |
204 ms |
32800 KB |
Output is correct |
27 |
Correct |
245 ms |
32804 KB |
Output is correct |
28 |
Correct |
218 ms |
33136 KB |
Output is correct |
29 |
Runtime error |
110 ms |
43552 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |