#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<ll,ll> ii;
typedef pair<ii,int> iii;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define RST(i,n) memset(i,n,sizeof i)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(),a.end()
#define X first
#define Y second
#define eb emplace_back
#ifdef cold66
#define debug(...) do{\
fprintf(stderr,"LINE %d: (%s) = ",__LINE__,#__VA_ARGS__);\
_do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<", ";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
_s<<"{";
for(It _it=_ita;_it!=_itb;_it++)
{
_s<<(_it==_ita?"":",")<<*_it;
}
_s<<"}";
return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
#else
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#endif // cold66
//}
template<class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; }
template<class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; }
const ll MAXn=5e5+5,MAXlg=__lg(MAXn)+2;
const ll MOD=1000000007;
const ll INF=0x3f3f3f3f3f3f3f3f;
struct node{
ll tg,mx,data;
} seg[MAXn*4];
ll d[MAXn],ans[MAXn];
vector<ii> q[MAXn];
vector<ll> p[MAXn];
void upd(ll id,ll val){
seg[id].tg = max(seg[id].tg,val);
seg[id].mx = max(seg[id].mx,seg[id].data + val);
}
void push(ll id){
if (seg[id].tg == 0) return;
upd(id*2+1,seg[id].tg), upd(id*2+2,seg[id].tg);
seg[id].tg = 0;
}
void build(ll id,ll L,ll R){
if (L == R-1) {
seg[id].data = seg[id].mx = d[L];
seg[id].tg = 0;
return;
}
ll mid = (L+R)>>1;
build(id*2+1,L,mid);
build(id*2+2,mid,R);
seg[id].mx = seg[id].data = max(seg[id*2+1].data,seg[id*2+2].data);
}
void ins(ll id,ll L,ll R,ll l,ll r,ll val){
if (L >= r || R <= l) return;
if (L == l && R == r) {
upd(id,val);
return;
}
push(id);
ll mid = (L+R)>>1;
if (r <= mid) ins(id*2+1,L,mid,l,r,val);
else if (l >= mid) ins(id*2+2,mid,R,l,r,val);
else {
ins(id*2+1,L,mid,l,mid,val);
ins(id*2+2,mid,R,mid,r,val);
}
seg[id].mx = max(seg[id*2+1].mx,seg[id*2+2].mx);
}
ll qr(ll id,ll L,ll R,ll l,ll r){
if (L >= r || R <= l) return 0;
if (L == l && R == r) {
return seg[id].mx;
}
push(id);
ll mid = (L+R)>>1;
if (r <= mid) return qr(id*2+1,L,mid,l,r);
else if (l >= mid) return qr(id*2+2,mid,R,l,r);
else {
return max(qr(id*2+1,L,mid,l,mid),qr(id*2+2,mid,R,mid,r));
}
}
int main(){
IOS();
ll n;
cin >> n;
for (int i=0;i<n;++i) {
cin >> d[i];
}
ll m;
cin >> m;
for (int i=0;i<m;++i) {
ll l,r;
cin >> l >> r;
l--;
q[l].eb(r,i);
}
vector<ll> stk;
for (ll i=0;i<n;++i) {
if (!SZ(stk)) {
stk.eb(i);
}
else {
while (SZ(stk) && d[stk.back()] < d[i]) {
p[stk.back()].eb(i);
stk.pop_back();
}
if (SZ(stk)) p[stk.back()].eb(i);
stk.eb(i);
}
}
build(0,0,n);
for (ll i=n-3;i>=0;i--) {
for (ll j:p[i]) ins(0,0,n,2*j-i,n,d[i]+d[j]);
for (ii j:q[i]) ans[j.Y] = qr(0,0,n,i+2,j.X);
}
for (int i=0;i<m;++i) cout << ans[i] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23808 KB |
Output is correct |
3 |
Correct |
18 ms |
23808 KB |
Output is correct |
4 |
Correct |
19 ms |
23808 KB |
Output is correct |
5 |
Correct |
16 ms |
23808 KB |
Output is correct |
6 |
Correct |
17 ms |
23808 KB |
Output is correct |
7 |
Correct |
17 ms |
23880 KB |
Output is correct |
8 |
Correct |
16 ms |
23808 KB |
Output is correct |
9 |
Correct |
20 ms |
23876 KB |
Output is correct |
10 |
Correct |
18 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23808 KB |
Output is correct |
3 |
Correct |
18 ms |
23808 KB |
Output is correct |
4 |
Correct |
19 ms |
23808 KB |
Output is correct |
5 |
Correct |
16 ms |
23808 KB |
Output is correct |
6 |
Correct |
17 ms |
23808 KB |
Output is correct |
7 |
Correct |
17 ms |
23880 KB |
Output is correct |
8 |
Correct |
16 ms |
23808 KB |
Output is correct |
9 |
Correct |
20 ms |
23876 KB |
Output is correct |
10 |
Correct |
18 ms |
23936 KB |
Output is correct |
11 |
Correct |
344 ms |
50548 KB |
Output is correct |
12 |
Correct |
346 ms |
50476 KB |
Output is correct |
13 |
Correct |
340 ms |
50628 KB |
Output is correct |
14 |
Correct |
350 ms |
50524 KB |
Output is correct |
15 |
Correct |
338 ms |
50560 KB |
Output is correct |
16 |
Correct |
335 ms |
49788 KB |
Output is correct |
17 |
Correct |
340 ms |
50048 KB |
Output is correct |
18 |
Correct |
334 ms |
49784 KB |
Output is correct |
19 |
Correct |
330 ms |
50424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
185 ms |
47096 KB |
Output is correct |
2 |
Correct |
114 ms |
45768 KB |
Output is correct |
3 |
Correct |
130 ms |
47340 KB |
Output is correct |
4 |
Correct |
182 ms |
46968 KB |
Output is correct |
5 |
Correct |
186 ms |
46968 KB |
Output is correct |
6 |
Correct |
194 ms |
46328 KB |
Output is correct |
7 |
Correct |
178 ms |
46328 KB |
Output is correct |
8 |
Correct |
182 ms |
46116 KB |
Output is correct |
9 |
Correct |
181 ms |
46584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23808 KB |
Output is correct |
3 |
Correct |
18 ms |
23808 KB |
Output is correct |
4 |
Correct |
19 ms |
23808 KB |
Output is correct |
5 |
Correct |
16 ms |
23808 KB |
Output is correct |
6 |
Correct |
17 ms |
23808 KB |
Output is correct |
7 |
Correct |
17 ms |
23880 KB |
Output is correct |
8 |
Correct |
16 ms |
23808 KB |
Output is correct |
9 |
Correct |
20 ms |
23876 KB |
Output is correct |
10 |
Correct |
18 ms |
23936 KB |
Output is correct |
11 |
Correct |
344 ms |
50548 KB |
Output is correct |
12 |
Correct |
346 ms |
50476 KB |
Output is correct |
13 |
Correct |
340 ms |
50628 KB |
Output is correct |
14 |
Correct |
350 ms |
50524 KB |
Output is correct |
15 |
Correct |
338 ms |
50560 KB |
Output is correct |
16 |
Correct |
335 ms |
49788 KB |
Output is correct |
17 |
Correct |
340 ms |
50048 KB |
Output is correct |
18 |
Correct |
334 ms |
49784 KB |
Output is correct |
19 |
Correct |
330 ms |
50424 KB |
Output is correct |
20 |
Correct |
185 ms |
47096 KB |
Output is correct |
21 |
Correct |
114 ms |
45768 KB |
Output is correct |
22 |
Correct |
130 ms |
47340 KB |
Output is correct |
23 |
Correct |
182 ms |
46968 KB |
Output is correct |
24 |
Correct |
186 ms |
46968 KB |
Output is correct |
25 |
Correct |
194 ms |
46328 KB |
Output is correct |
26 |
Correct |
178 ms |
46328 KB |
Output is correct |
27 |
Correct |
182 ms |
46116 KB |
Output is correct |
28 |
Correct |
181 ms |
46584 KB |
Output is correct |
29 |
Correct |
1183 ms |
104052 KB |
Output is correct |
30 |
Correct |
951 ms |
100948 KB |
Output is correct |
31 |
Correct |
991 ms |
104888 KB |
Output is correct |
32 |
Correct |
1213 ms |
104056 KB |
Output is correct |
33 |
Correct |
1166 ms |
104056 KB |
Output is correct |
34 |
Correct |
1154 ms |
101756 KB |
Output is correct |
35 |
Correct |
1152 ms |
101532 KB |
Output is correct |
36 |
Correct |
1128 ms |
101400 KB |
Output is correct |
37 |
Correct |
1139 ms |
102908 KB |
Output is correct |
38 |
Correct |
766 ms |
106744 KB |
Output is correct |
39 |
Correct |
788 ms |
106680 KB |
Output is correct |
40 |
Correct |
771 ms |
103316 KB |
Output is correct |
41 |
Correct |
775 ms |
102784 KB |
Output is correct |
42 |
Correct |
748 ms |
102952 KB |
Output is correct |
43 |
Correct |
796 ms |
104460 KB |
Output is correct |
44 |
Correct |
949 ms |
106364 KB |
Output is correct |
45 |
Correct |
843 ms |
106268 KB |
Output is correct |
46 |
Correct |
833 ms |
103192 KB |
Output is correct |
47 |
Correct |
882 ms |
102776 KB |
Output is correct |
48 |
Correct |
833 ms |
102764 KB |
Output is correct |
49 |
Correct |
833 ms |
104952 KB |
Output is correct |
50 |
Correct |
1006 ms |
105540 KB |
Output is correct |
51 |
Correct |
954 ms |
105592 KB |
Output is correct |
52 |
Correct |
982 ms |
103152 KB |
Output is correct |
53 |
Correct |
939 ms |
102764 KB |
Output is correct |
54 |
Correct |
1087 ms |
102740 KB |
Output is correct |
55 |
Correct |
999 ms |
104560 KB |
Output is correct |