#include <bits/stdc++.h>
using namespace std;
const int MAXN = 25e4+5;
const int MAXB = 18;
int arr[MAXN];
int Arr[MAXN];
typedef long long ll;
ll pre[MAXN];
int n, q;
ll sum(int l, int r) {
return pre[r+1] - pre[l];
}
int dp[MAXN];
vector<int> to_activate[MAXN];
typedef array<int, 2> ar2;
ar2 nxt[MAXN][MAXB];
int sjump[MAXN][MAXB];
int spos[MAXN];
int tmx[2*MAXN];
void reset() {
fill(tmx, tmx+2*n+2, -1);
}
void upd(int p, int v) {
p += n+1;
tmx[p] = max(tmx[p], v);
for (p /= 2; p > 0; p /= 2) {
tmx[p] = max(tmx[2*p], tmx[2*p+1]);
}
}
int get_mx(int l, int r) {
l += n+1;
r += n+2;
int ans = -1;
for (; r > l; l /= 2, r /= 2) {
if (l & 1) ans = max(ans, tmx[l++]);
if (r & 1) ans = max(ans, tmx[--r]);
}
return ans;
}
ll vpre(int v) {
return arr[v] + pre[v+1];
}
int lift(int l, int r, int cur = -1) {
if (cur == -1) return max(lift(l, r, l), 1+lift(l, r, spos[l]));
if (cur >= r) return -1;
if (nxt[cur][0][0] >= r) {
for (int i = MAXB-1; i >= 0; i--) {
if (sjump[cur][i] < r) return lift(l, r, sjump[cur][i]);
}
}
else {
for (int i = MAXB-1; i >= 0; i--) {
if (nxt[cur][i][0] < r) return lift(l, r, nxt[cur][i][0]) + nxt[cur][i][1];
}
}
if (sum(cur+1, r-1) > arr[cur]) return 2;
return 1;
}
void solve_reg() {
vector<int> stck;
set<int> active;
// to_activate[n-1].push_back(n);
active.insert(n);
nxt[n][0] = {n, 0};
sjump[n][0] = n;
for (int i = n-1; i >= 0; i--) {
nxt[i][0] = {n, 0};
sjump[i][0] = n;
while (!stck.empty() && vpre(i) <= vpre(stck.back())) stck.pop_back();
// if (stck.empty()) {
if (sum(i+1, n-1) > arr[i]) {
int mn = i+1, mx = n;
while (mx > mn+1) {
int mid = (mn+mx)/2;
if (sum(i+1, mid-1) > arr[i]) mx = mid;
else mn = mid;
}
nxt[i][0] = {*active.lower_bound(mx), 2};
}
if (!stck.empty()) {
ar2 alt = nxt[stck.back()][0];
if (alt[0] < nxt[i][0][0]) nxt[i][0] = alt;
sjump[i][0] = stck.back();
}
stck.push_back(i);
if (sum(0, i-1) > arr[i]) {
int mn = -1;
int mx = i-1;
while (mx > mn+1) {
int mid = (mn+mx)/2;
if (sum(mid+1, i-1) > arr[i]) mn = mid;
else mx = mid;
}
to_activate[mx].push_back(i);
}
for (int v: to_activate[i]) {
active.insert(v);
}
spos[i] = *active.begin();
}
for (int j = 1; j < MAXB; j++) {
for (int i = 0; i <= n; i++) {
nxt[i][j] = {nxt[nxt[i][j-1][0]][j-1][0], nxt[i][j-1][1] + nxt[nxt[i][j-1][0]][j-1][1]};
sjump[i][j] = sjump[sjump[i][j-1]][j-1];
}
}
while (q--) {
int x, y, l, r;
cin >> x >> y >> l >> r;
assert(arr[x-1] == y);
cout << lift(l, r) << '\n';
}
}
int solve() {
reset();
for (int i = 0; i < n; i++) to_activate[i].clear();
pre[0] = 0;
for (int i = 0; i < n; i++) pre[i+1] = pre[i] + arr[i];
dp[n] = 0;
to_activate[n-1].push_back(n);
vector<int> fin_activate;
for (int i = n-1; i >= 0; i--) {
for (int v: to_activate[i]) {
upd(v, dp[v]);
}
if (i == n-1) {
dp[i] = 1;
}
else {
if (sum(i+1, n-1) <= arr[i]) dp[i] = -1;
else {
int mn = i+1, mx = n;
while (mx > mn+1) {
int mid = (mn+mx)/2;
if (sum(i+1, mid-1) > arr[i]) mx = mid;
else mn = mid;
}
dp[i] = 2+get_mx(mx, n);
assert(dp[i] >= 2);
}
}
if (sum(0, i-1) > arr[i]) {
int mn = -1;
int mx = i-1;
while (mx > mn+1) {
int mid = (mn+mx)/2;
if (sum(mid+1, i-1) > arr[i]) mn = mid;
else mx = mid;
}
if (mn == -1) fin_activate.push_back(i);
else to_activate[mn].push_back(i);
}
}
for (int v: fin_activate) {
upd(v, dp[v]);
}
return max(dp[0], 1+get_mx(1, n));
}
void solve_spec() {
int N = n;
for (int i = 0; i < N; i++) Arr[i] = arr[i];
while (q--) {
int x, y, l, r;
cin >> x >> y >> l >> r;
Arr[x-1] = y;
vector<int> cur;
for (int i = l; i < r; i++) arr[i-l] = Arr[i];
n = r-l;
cout << solve() << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n;
pre[0] = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
pre[i+1] = pre[i] + arr[i];
}
// implement a mon stack to keep track of next smaller. we want to track value + prefix
cin >> q;
if (q <= 10) solve_spec();
else solve_reg();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10584 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10688 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10584 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10688 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10588 KB |
Output is correct |
36 |
Correct |
2 ms |
10588 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
1 ms |
10588 KB |
Output is correct |
40 |
Correct |
1 ms |
10672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10584 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10688 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10588 KB |
Output is correct |
36 |
Correct |
2 ms |
10588 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
1 ms |
10588 KB |
Output is correct |
40 |
Correct |
1 ms |
10672 KB |
Output is correct |
41 |
Correct |
13 ms |
11100 KB |
Output is correct |
42 |
Correct |
13 ms |
11284 KB |
Output is correct |
43 |
Correct |
15 ms |
11352 KB |
Output is correct |
44 |
Correct |
14 ms |
11608 KB |
Output is correct |
45 |
Correct |
53 ms |
16368 KB |
Output is correct |
46 |
Correct |
158 ms |
18000 KB |
Output is correct |
47 |
Correct |
120 ms |
17844 KB |
Output is correct |
48 |
Correct |
200 ms |
20564 KB |
Output is correct |
49 |
Correct |
241 ms |
20496 KB |
Output is correct |
50 |
Correct |
164 ms |
18200 KB |
Output is correct |
51 |
Correct |
150 ms |
19500 KB |
Output is correct |
52 |
Correct |
96 ms |
18000 KB |
Output is correct |
53 |
Correct |
211 ms |
19512 KB |
Output is correct |
54 |
Correct |
196 ms |
19548 KB |
Output is correct |
55 |
Correct |
164 ms |
20480 KB |
Output is correct |
56 |
Correct |
142 ms |
18000 KB |
Output is correct |
57 |
Correct |
90 ms |
16724 KB |
Output is correct |
58 |
Correct |
155 ms |
19500 KB |
Output is correct |
59 |
Correct |
41 ms |
16504 KB |
Output is correct |
60 |
Correct |
67 ms |
16460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
95 ms |
68524 KB |
Output is correct |
3 |
Correct |
136 ms |
82772 KB |
Output is correct |
4 |
Correct |
142 ms |
82868 KB |
Output is correct |
5 |
Correct |
144 ms |
83336 KB |
Output is correct |
6 |
Correct |
131 ms |
82784 KB |
Output is correct |
7 |
Correct |
142 ms |
82768 KB |
Output is correct |
8 |
Correct |
135 ms |
82512 KB |
Output is correct |
9 |
Correct |
141 ms |
82772 KB |
Output is correct |
10 |
Correct |
116 ms |
68948 KB |
Output is correct |
11 |
Correct |
149 ms |
83496 KB |
Output is correct |
12 |
Correct |
151 ms |
82768 KB |
Output is correct |
13 |
Correct |
164 ms |
86356 KB |
Output is correct |
14 |
Correct |
147 ms |
84260 KB |
Output is correct |
15 |
Correct |
154 ms |
84560 KB |
Output is correct |
16 |
Correct |
150 ms |
83540 KB |
Output is correct |
17 |
Correct |
128 ms |
83536 KB |
Output is correct |
18 |
Correct |
158 ms |
86548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Runtime error |
136 ms |
166420 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10584 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10688 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
3 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10588 KB |
Output is correct |
36 |
Correct |
2 ms |
10588 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
1 ms |
10588 KB |
Output is correct |
40 |
Correct |
1 ms |
10672 KB |
Output is correct |
41 |
Correct |
13 ms |
11100 KB |
Output is correct |
42 |
Correct |
13 ms |
11284 KB |
Output is correct |
43 |
Correct |
15 ms |
11352 KB |
Output is correct |
44 |
Correct |
14 ms |
11608 KB |
Output is correct |
45 |
Correct |
53 ms |
16368 KB |
Output is correct |
46 |
Correct |
158 ms |
18000 KB |
Output is correct |
47 |
Correct |
120 ms |
17844 KB |
Output is correct |
48 |
Correct |
200 ms |
20564 KB |
Output is correct |
49 |
Correct |
241 ms |
20496 KB |
Output is correct |
50 |
Correct |
164 ms |
18200 KB |
Output is correct |
51 |
Correct |
150 ms |
19500 KB |
Output is correct |
52 |
Correct |
96 ms |
18000 KB |
Output is correct |
53 |
Correct |
211 ms |
19512 KB |
Output is correct |
54 |
Correct |
196 ms |
19548 KB |
Output is correct |
55 |
Correct |
164 ms |
20480 KB |
Output is correct |
56 |
Correct |
142 ms |
18000 KB |
Output is correct |
57 |
Correct |
90 ms |
16724 KB |
Output is correct |
58 |
Correct |
155 ms |
19500 KB |
Output is correct |
59 |
Correct |
41 ms |
16504 KB |
Output is correct |
60 |
Correct |
67 ms |
16460 KB |
Output is correct |
61 |
Correct |
1 ms |
10584 KB |
Output is correct |
62 |
Correct |
95 ms |
68524 KB |
Output is correct |
63 |
Correct |
136 ms |
82772 KB |
Output is correct |
64 |
Correct |
142 ms |
82868 KB |
Output is correct |
65 |
Correct |
144 ms |
83336 KB |
Output is correct |
66 |
Correct |
131 ms |
82784 KB |
Output is correct |
67 |
Correct |
142 ms |
82768 KB |
Output is correct |
68 |
Correct |
135 ms |
82512 KB |
Output is correct |
69 |
Correct |
141 ms |
82772 KB |
Output is correct |
70 |
Correct |
116 ms |
68948 KB |
Output is correct |
71 |
Correct |
149 ms |
83496 KB |
Output is correct |
72 |
Correct |
151 ms |
82768 KB |
Output is correct |
73 |
Correct |
164 ms |
86356 KB |
Output is correct |
74 |
Correct |
147 ms |
84260 KB |
Output is correct |
75 |
Correct |
154 ms |
84560 KB |
Output is correct |
76 |
Correct |
150 ms |
83540 KB |
Output is correct |
77 |
Correct |
128 ms |
83536 KB |
Output is correct |
78 |
Correct |
158 ms |
86548 KB |
Output is correct |
79 |
Correct |
2 ms |
10588 KB |
Output is correct |
80 |
Correct |
1 ms |
10588 KB |
Output is correct |
81 |
Runtime error |
136 ms |
166420 KB |
Execution killed with signal 6 |
82 |
Halted |
0 ms |
0 KB |
- |