#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
const int mx = 5e5;
int seg[4*mx+5], lazy[4*mx+5], a[mx+1], b[mx+2];
set<int> st;
void upd (int l, int r, int bl, int br, int val, int id) {
if (l==bl && r==br) {
seg[id] += val;
lazy[id] += val;
return;
}
int mid = (bl+br)/2;
if (r<=mid) upd (l, r, bl, mid, val, 2*id);
else if (l>mid) upd (l, r, mid+1, br, val, 2*id+1);
else {
upd (l, mid, bl, mid, val, 2*id);
upd (mid+1, r, mid+1, br, val, 2*id+1);
}
seg[id] = max(seg[2*id], seg[2*id+1])+lazy[id];
}
int qry (int l, int r, int bl, int br, int id) {
if (l==bl && r==br) return seg[id];
int mid = (bl+br)/2;
if (r<=mid) return lazy[id] + qry (l, r, bl, mid, 2*id);
if (l>mid) return lazy[id] + qry (l, r, mid+1, br, 2*id+1);
return lazy[id] + max( qry(l, mid, bl, mid, 2*id), qry(mid+1, r, mid+1, br, 2*id+1));
}
void adij (int i, int j) {
int x = 2*j-i;
if (x>=*st.rbegin()) return;
auto it = st.upper_bound(x);
if (it!=st.begin()) {
if (a[i]+a[j] > b[*prev(it)]) {
upd (x, *it-1, 1, mx, a[i]+a[j] - b[*prev(it)], 1);
b[x] = a[i]+a[j];
st.insert(x);
}
}
else {
upd (x, *it-1, 1, mx, a[i]+a[j], 1);
b[x] = a[i]+a[j];
st.insert(x);
}
while (a[i]+a[j] > b[*it]) {
upd (*it, *next(it)-1, 1, mx, a[i]+a[j]-b[*it], 1);
it = st.erase(it);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q; cin >> n;
st.insert(n+1);
b[n+1] = 1e9;
vii rs[n+1];
rep (i,1,n) cin >> a[i];
cin >> q;
int ans[q+1] = {};
rep (i,1,q) {
int l, r; cin >> l >> r;
rs[l].pb({r,i});
}
stack<int> js;
rrep (i,n,1) {
upd (i, i, 1, mx, a[i], 1);
while ( len(js) && a[js.top()] < a[i]) {
adij(i, js.top());
js.pop();
}
if (len(js)) adij(i, js.top());
js.push(i);
for (ii ri : rs[i]) ans[ri.se] = qry (i+2, ri.fi, 1, mx, 1);
}
rep (i,1,q) cout << ans[i] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
222 ms |
19304 KB |
Output is correct |
12 |
Correct |
295 ms |
19168 KB |
Output is correct |
13 |
Correct |
219 ms |
19216 KB |
Output is correct |
14 |
Correct |
299 ms |
19172 KB |
Output is correct |
15 |
Correct |
222 ms |
19360 KB |
Output is correct |
16 |
Correct |
220 ms |
18640 KB |
Output is correct |
17 |
Correct |
213 ms |
18604 KB |
Output is correct |
18 |
Correct |
216 ms |
18540 KB |
Output is correct |
19 |
Correct |
285 ms |
19284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
11540 KB |
Output is correct |
2 |
Correct |
130 ms |
21116 KB |
Output is correct |
3 |
Correct |
142 ms |
11340 KB |
Output is correct |
4 |
Correct |
179 ms |
11260 KB |
Output is correct |
5 |
Correct |
219 ms |
11344 KB |
Output is correct |
6 |
Correct |
182 ms |
10956 KB |
Output is correct |
7 |
Correct |
246 ms |
10932 KB |
Output is correct |
8 |
Correct |
177 ms |
10920 KB |
Output is correct |
9 |
Correct |
208 ms |
11328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
222 ms |
19304 KB |
Output is correct |
12 |
Correct |
295 ms |
19168 KB |
Output is correct |
13 |
Correct |
219 ms |
19216 KB |
Output is correct |
14 |
Correct |
299 ms |
19172 KB |
Output is correct |
15 |
Correct |
222 ms |
19360 KB |
Output is correct |
16 |
Correct |
220 ms |
18640 KB |
Output is correct |
17 |
Correct |
213 ms |
18604 KB |
Output is correct |
18 |
Correct |
216 ms |
18540 KB |
Output is correct |
19 |
Correct |
285 ms |
19284 KB |
Output is correct |
20 |
Correct |
177 ms |
11540 KB |
Output is correct |
21 |
Correct |
130 ms |
21116 KB |
Output is correct |
22 |
Correct |
142 ms |
11340 KB |
Output is correct |
23 |
Correct |
179 ms |
11260 KB |
Output is correct |
24 |
Correct |
219 ms |
11344 KB |
Output is correct |
25 |
Correct |
182 ms |
10956 KB |
Output is correct |
26 |
Correct |
246 ms |
10932 KB |
Output is correct |
27 |
Correct |
177 ms |
10920 KB |
Output is correct |
28 |
Correct |
208 ms |
11328 KB |
Output is correct |
29 |
Correct |
1002 ms |
42480 KB |
Output is correct |
30 |
Correct |
846 ms |
66864 KB |
Output is correct |
31 |
Correct |
847 ms |
42408 KB |
Output is correct |
32 |
Correct |
1041 ms |
42336 KB |
Output is correct |
33 |
Correct |
1004 ms |
42324 KB |
Output is correct |
34 |
Correct |
999 ms |
41624 KB |
Output is correct |
35 |
Correct |
965 ms |
41828 KB |
Output is correct |
36 |
Correct |
1002 ms |
41616 KB |
Output is correct |
37 |
Correct |
936 ms |
42300 KB |
Output is correct |
38 |
Correct |
698 ms |
47960 KB |
Output is correct |
39 |
Correct |
711 ms |
47968 KB |
Output is correct |
40 |
Correct |
766 ms |
46268 KB |
Output is correct |
41 |
Correct |
669 ms |
46024 KB |
Output is correct |
42 |
Correct |
648 ms |
46064 KB |
Output is correct |
43 |
Correct |
730 ms |
46948 KB |
Output is correct |
44 |
Correct |
757 ms |
47424 KB |
Output is correct |
45 |
Correct |
727 ms |
47448 KB |
Output is correct |
46 |
Correct |
753 ms |
45868 KB |
Output is correct |
47 |
Correct |
712 ms |
45928 KB |
Output is correct |
48 |
Correct |
863 ms |
45684 KB |
Output is correct |
49 |
Correct |
801 ms |
47200 KB |
Output is correct |
50 |
Correct |
861 ms |
45600 KB |
Output is correct |
51 |
Correct |
833 ms |
45388 KB |
Output is correct |
52 |
Correct |
834 ms |
44680 KB |
Output is correct |
53 |
Correct |
803 ms |
44492 KB |
Output is correct |
54 |
Correct |
820 ms |
44532 KB |
Output is correct |
55 |
Correct |
797 ms |
45328 KB |
Output is correct |