#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 5e5 + 23;
const ll inf = 1e18;
#define F first
#define S second
#define pb push_back
#define sz(x) ((int)x.size())
#define kill(x) cout<<x , exit(0);
#define all(x) x.begin(),x.end()
#define lc (v<<1)
#define rc ((v<<1)|1)
#define debug(x) cerr<< #x << " : " << x << '\n';
#define int ll
struct Seg {
int t[N<<2];
int mx[N<<2];
int lz[N<<2];
Seg() {
for(int i = 0 ; i<(N<<2); i ++) t[i] = mx[i] = lz[i] = 0;
}
void shift(int v) {
if(lz[v] == 0) return;
lz[lc] = max(lz[lc],lz[v]);
lz[rc] = max(lz[rc],lz[v]);
t[lc] = max(t[lc],mx[lc] + lz[lc]);
t[rc] = max(t[rc],mx[rc] + lz[rc]);
lz[v] = 0;
}
// khode a[i] ha
void set(int pos,int x,int v=1,int tl= 1,int tr= N) {
if(tr-tl == 1) {
mx[v] = x;
return;
}
int mid=(tl+tr)/2;
if(pos < mid)
set(pos,x,lc,tl,mid);
else
set(pos,x,rc,mid,tr);
mx[v] = max(mx[lc],mx[rc]);
}
void add(int l,int r,int x,int v=1,int tl = 1,int tr= N) {
if(l > r) return;
if(l == tl && r ==tr-1) {
lz[v] = max(lz[v],x);
t[v] = max(t[v],mx[v] + lz[v]);
return;
}
shift(v);
int mid=(tl+tr)/2;
add(l,min(mid-1,r),x,lc,tl,mid);
add(max(l,mid),r,x,rc,mid,tr);
t[v] = max(t[lc],t[rc]);
}
int get(int l,int r,int v=1,int tl=1,int tr = N) {
if(l >r) return 0;
if(l == tl && r == tr-1) return t[v];
shift(v);
int mid=(tl+tr)/2;
return max(get(l,min(mid-1,r),lc,tl,mid), get(max(l,mid),r,rc,mid,tr));
}
}seg;
int n,q;
int a[N];
vector<pair<int,int>> Q[N];
vector<pair<int,int>> here[N];
int ans[N];
vector<int> st;
int L[N],R[N];
vector<pair<int,int> > edges;
int Mx[N];
int32_t main() {
cin>>n;
for(int i = 1; i<= n ; i ++) {
cin>>a[i];
seg.set(i,a[i]);
}
cin>>q;
for(int i = 0 ; i< q; i ++) {
int l,r; cin>>l>>r;
Q[l].pb({r,i});
}
st.pb(0);
for(int i = 1; i<= n ; i++) {
while(st.back() && a[st.back()] < a[i]) st.pop_back();
if(sz(st) > 1) {
L[i] = st.back();
}
st.pb(i);
} st.clear();
st.pb(0);
for(int i = n ; i >= 1;i --) {
while(st.back() && a[st.back()] < a[i]) st.pop_back();
if(sz(st) > 1) {
R[i]= st.back();
}
st.pb(i);
}
for(int i= 1; i <= n ; i ++) {
if(R[i] != 0 && R[i] - i + R[i] <= n) {
//Mx[R[i]-i+R[i]] = max(Mx[R[i]-i +R[i]], a[i] + a[R[i]]);
here[i].pb({R[i]-i+R[i],a[i] + a[R[i]]});
}
if(L[i] != 0 && i-L[i] + i <= n) {
//Mx[i-L[i]+i] = max(Mx[i-L[i]+i],a[i] + a[L[i]]);
here[L[i]].pb({i-L[i]+i,a[i] + a[L[i]]});
}
}
for(int i = n; i >= 1; i --) {
for(auto [l,val] : here[i]) seg.add(l,n,val);
for(auto [r,id] : Q[i]) ans[id] = seg.get(i,r);
}
for(int i = 0 ; i< q;i ++) {
cout<<ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
80008 KB |
Output is correct |
2 |
Correct |
17 ms |
79960 KB |
Output is correct |
3 |
Correct |
17 ms |
77772 KB |
Output is correct |
4 |
Correct |
17 ms |
79964 KB |
Output is correct |
5 |
Correct |
17 ms |
79960 KB |
Output is correct |
6 |
Correct |
19 ms |
79964 KB |
Output is correct |
7 |
Correct |
17 ms |
80216 KB |
Output is correct |
8 |
Correct |
18 ms |
79956 KB |
Output is correct |
9 |
Correct |
18 ms |
79964 KB |
Output is correct |
10 |
Correct |
18 ms |
80216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
80008 KB |
Output is correct |
2 |
Correct |
17 ms |
79960 KB |
Output is correct |
3 |
Correct |
17 ms |
77772 KB |
Output is correct |
4 |
Correct |
17 ms |
79964 KB |
Output is correct |
5 |
Correct |
17 ms |
79960 KB |
Output is correct |
6 |
Correct |
19 ms |
79964 KB |
Output is correct |
7 |
Correct |
17 ms |
80216 KB |
Output is correct |
8 |
Correct |
18 ms |
79956 KB |
Output is correct |
9 |
Correct |
18 ms |
79964 KB |
Output is correct |
10 |
Correct |
18 ms |
80216 KB |
Output is correct |
11 |
Correct |
341 ms |
107124 KB |
Output is correct |
12 |
Correct |
352 ms |
105484 KB |
Output is correct |
13 |
Correct |
355 ms |
107208 KB |
Output is correct |
14 |
Correct |
345 ms |
107160 KB |
Output is correct |
15 |
Correct |
366 ms |
107280 KB |
Output is correct |
16 |
Correct |
348 ms |
106592 KB |
Output is correct |
17 |
Correct |
352 ms |
106548 KB |
Output is correct |
18 |
Correct |
365 ms |
106256 KB |
Output is correct |
19 |
Correct |
346 ms |
107088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
97456 KB |
Output is correct |
2 |
Correct |
154 ms |
94856 KB |
Output is correct |
3 |
Correct |
144 ms |
94916 KB |
Output is correct |
4 |
Correct |
232 ms |
97480 KB |
Output is correct |
5 |
Correct |
226 ms |
97416 KB |
Output is correct |
6 |
Correct |
233 ms |
97212 KB |
Output is correct |
7 |
Correct |
214 ms |
97104 KB |
Output is correct |
8 |
Correct |
212 ms |
97160 KB |
Output is correct |
9 |
Correct |
232 ms |
97616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
80008 KB |
Output is correct |
2 |
Correct |
17 ms |
79960 KB |
Output is correct |
3 |
Correct |
17 ms |
77772 KB |
Output is correct |
4 |
Correct |
17 ms |
79964 KB |
Output is correct |
5 |
Correct |
17 ms |
79960 KB |
Output is correct |
6 |
Correct |
19 ms |
79964 KB |
Output is correct |
7 |
Correct |
17 ms |
80216 KB |
Output is correct |
8 |
Correct |
18 ms |
79956 KB |
Output is correct |
9 |
Correct |
18 ms |
79964 KB |
Output is correct |
10 |
Correct |
18 ms |
80216 KB |
Output is correct |
11 |
Correct |
341 ms |
107124 KB |
Output is correct |
12 |
Correct |
352 ms |
105484 KB |
Output is correct |
13 |
Correct |
355 ms |
107208 KB |
Output is correct |
14 |
Correct |
345 ms |
107160 KB |
Output is correct |
15 |
Correct |
366 ms |
107280 KB |
Output is correct |
16 |
Correct |
348 ms |
106592 KB |
Output is correct |
17 |
Correct |
352 ms |
106548 KB |
Output is correct |
18 |
Correct |
365 ms |
106256 KB |
Output is correct |
19 |
Correct |
346 ms |
107088 KB |
Output is correct |
20 |
Correct |
225 ms |
97456 KB |
Output is correct |
21 |
Correct |
154 ms |
94856 KB |
Output is correct |
22 |
Correct |
144 ms |
94916 KB |
Output is correct |
23 |
Correct |
232 ms |
97480 KB |
Output is correct |
24 |
Correct |
226 ms |
97416 KB |
Output is correct |
25 |
Correct |
233 ms |
97212 KB |
Output is correct |
26 |
Correct |
214 ms |
97104 KB |
Output is correct |
27 |
Correct |
212 ms |
97160 KB |
Output is correct |
28 |
Correct |
232 ms |
97616 KB |
Output is correct |
29 |
Correct |
1361 ms |
142820 KB |
Output is correct |
30 |
Correct |
1138 ms |
136824 KB |
Output is correct |
31 |
Correct |
1144 ms |
136792 KB |
Output is correct |
32 |
Correct |
1377 ms |
142972 KB |
Output is correct |
33 |
Correct |
1395 ms |
142912 KB |
Output is correct |
34 |
Correct |
1308 ms |
140812 KB |
Output is correct |
35 |
Correct |
1325 ms |
140168 KB |
Output is correct |
36 |
Correct |
1318 ms |
140304 KB |
Output is correct |
37 |
Correct |
1335 ms |
141932 KB |
Output is correct |
38 |
Correct |
1052 ms |
145416 KB |
Output is correct |
39 |
Correct |
1064 ms |
145548 KB |
Output is correct |
40 |
Correct |
998 ms |
142200 KB |
Output is correct |
41 |
Correct |
992 ms |
141688 KB |
Output is correct |
42 |
Correct |
966 ms |
141672 KB |
Output is correct |
43 |
Correct |
1018 ms |
143440 KB |
Output is correct |
44 |
Correct |
1091 ms |
145092 KB |
Output is correct |
45 |
Correct |
1103 ms |
145172 KB |
Output is correct |
46 |
Correct |
1069 ms |
141948 KB |
Output is correct |
47 |
Correct |
1065 ms |
141664 KB |
Output is correct |
48 |
Correct |
1044 ms |
141676 KB |
Output is correct |
49 |
Correct |
1037 ms |
143544 KB |
Output is correct |
50 |
Correct |
1194 ms |
144096 KB |
Output is correct |
51 |
Correct |
1171 ms |
144340 KB |
Output is correct |
52 |
Correct |
1116 ms |
142084 KB |
Output is correct |
53 |
Correct |
1123 ms |
141608 KB |
Output is correct |
54 |
Correct |
1175 ms |
141692 KB |
Output is correct |
55 |
Correct |
1136 ms |
143444 KB |
Output is correct |