#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const ll INF = 1e18;
int N, Q;
vi L;
vi pref;
vi vor, nach;
vvi lift;
vi vorg;
void precompute()
{
pref.resize(sz(L));
partial_sum(all(L), pref.begin());
vor.resize(N); // the last possible minima, like [0, vor[i]) with exclusive border
nach.resize(N); // the next possible minima , like [nach[i], end) with inclusive
for (ll i = 0, needed; i < N; ++i)
{
// vor
needed = pref[i] - 2 * L[i] - 1;
pii ac;
if (needed < 0)
vor[i] = -1;
else
vor[i] = upper_bound(all(pref), needed) - pref.begin();
// nach
needed = pref[i] + L[i] + 1;
if (needed > pref.back())
nach[i] = N + 1; // maybe different value, later for the lift array
else
nach[i] = lower_bound(all(pref), needed) - pref.begin() + 1; // something in [1, N]
}
lift.assign(ceil(log2(N)), vi(N + 2, -1));
vvi todo(N + 2);
for (int i = 0; i < sz(lift[0]); ++i)
{
if (i > 0)
lift[0][i] = max(lift[0][i], lift[0][i - 1]);
for (ll x : todo[i])
{
if (i >= sz(vor) || x < vor[i])
lift[0][i] = max(lift[0][i], x);
else
todo[i + 1].push_back(x);
}
if (i < sz(nach))
todo[nach[i]].push_back(i);
}
for (int j = 1; j < sz(lift); ++j)
for (int i = 0; i < sz(lift[j]); ++i)
{
int step = lift[j - 1][i];
lift[j][i] = (step == -1) ? -1 : lift[j - 1][step];
}
for (int j = 1; j < sz(lift); ++j)
for (int i = 0; i < sz(lift[j]) - 1; ++i)
assert(lift[j][i] <= lift[j][i + 1]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
L.resize(N);
for (int i = 0; i < N; ++i)
cin >> L[i];
cin >> Q;
precompute();
while (Q--)
{
ll x, y, a, b;
cin >> x >> y >> a >> b;
--x;
if (L[x] != y)
{
L[x] = y;
precompute();
}
ll ans = 1;
// assume we start with a minimum
{
int tmp = b - 1;
ll mini = 0; // how many steps
for (int j = sz(lift) - 1; j >= 0; --j)
if (lift[j][tmp] >= a)
tmp = lift[j][tmp], mini += 1 << j;
mini = 2 * mini + 1; // how many segments in total
ans = max(ans, mini);
for (; tmp > a; --tmp)
{
if (a <= vor[tmp])
ans = max(ans, mini + 1);
}
}
// assume we start with a maximum
{
ll buffer = 0;
for (int i = b - 1; i > a; --i)
{
buffer += L[i];
if (L[i - 1] < buffer)
{
int tmp = i - 1;
ll mini = 0; // how many steps
for (int j = sz(lift) - 1; j >= 0; --j)
if (lift[j][tmp] >= a)
tmp = lift[j][tmp], mini += 1 << j;
mini = 2 * mini + 2; // how many segments in total
ans = max(ans, mini);
for (; tmp > a; --tmp)
{
if (a <= vor[tmp])
ans = max(ans, mini + 1);
}
break;
}
}
}
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
5 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
25 |
Correct |
4 ms |
596 KB |
Output is correct |
26 |
Correct |
5 ms |
852 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
5 ms |
820 KB |
Output is correct |
29 |
Correct |
5 ms |
852 KB |
Output is correct |
30 |
Correct |
5 ms |
848 KB |
Output is correct |
31 |
Correct |
5 ms |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
584 KB |
Output is correct |
33 |
Correct |
4 ms |
596 KB |
Output is correct |
34 |
Correct |
4 ms |
596 KB |
Output is correct |
35 |
Correct |
4 ms |
596 KB |
Output is correct |
36 |
Correct |
5 ms |
596 KB |
Output is correct |
37 |
Correct |
4 ms |
596 KB |
Output is correct |
38 |
Correct |
4 ms |
596 KB |
Output is correct |
39 |
Correct |
3 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
5 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
25 |
Correct |
4 ms |
596 KB |
Output is correct |
26 |
Correct |
5 ms |
852 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
5 ms |
820 KB |
Output is correct |
29 |
Correct |
5 ms |
852 KB |
Output is correct |
30 |
Correct |
5 ms |
848 KB |
Output is correct |
31 |
Correct |
5 ms |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
584 KB |
Output is correct |
33 |
Correct |
4 ms |
596 KB |
Output is correct |
34 |
Correct |
4 ms |
596 KB |
Output is correct |
35 |
Correct |
4 ms |
596 KB |
Output is correct |
36 |
Correct |
5 ms |
596 KB |
Output is correct |
37 |
Correct |
4 ms |
596 KB |
Output is correct |
38 |
Correct |
4 ms |
596 KB |
Output is correct |
39 |
Correct |
3 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
596 KB |
Output is correct |
41 |
Correct |
408 ms |
48916 KB |
Output is correct |
42 |
Correct |
535 ms |
55828 KB |
Output is correct |
43 |
Correct |
495 ms |
55496 KB |
Output is correct |
44 |
Correct |
281 ms |
39476 KB |
Output is correct |
45 |
Correct |
537 ms |
57100 KB |
Output is correct |
46 |
Correct |
755 ms |
85444 KB |
Output is correct |
47 |
Correct |
456 ms |
52512 KB |
Output is correct |
48 |
Correct |
715 ms |
75412 KB |
Output is correct |
49 |
Correct |
751 ms |
81852 KB |
Output is correct |
50 |
Correct |
718 ms |
79232 KB |
Output is correct |
51 |
Correct |
712 ms |
77192 KB |
Output is correct |
52 |
Correct |
307 ms |
43080 KB |
Output is correct |
53 |
Correct |
542 ms |
57032 KB |
Output is correct |
54 |
Correct |
549 ms |
56024 KB |
Output is correct |
55 |
Correct |
560 ms |
58496 KB |
Output is correct |
56 |
Correct |
566 ms |
59492 KB |
Output is correct |
57 |
Correct |
544 ms |
58552 KB |
Output is correct |
58 |
Correct |
537 ms |
57596 KB |
Output is correct |
59 |
Correct |
327 ms |
59060 KB |
Output is correct |
60 |
Correct |
297 ms |
59576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
97 ms |
40368 KB |
Output is correct |
3 |
Correct |
129 ms |
55968 KB |
Output is correct |
4 |
Correct |
116 ms |
55596 KB |
Output is correct |
5 |
Correct |
115 ms |
57164 KB |
Output is correct |
6 |
Correct |
163 ms |
85740 KB |
Output is correct |
7 |
Correct |
166 ms |
75644 KB |
Output is correct |
8 |
Correct |
151 ms |
79420 KB |
Output is correct |
9 |
Correct |
157 ms |
77516 KB |
Output is correct |
10 |
Correct |
128 ms |
43276 KB |
Output is correct |
11 |
Correct |
170 ms |
57380 KB |
Output is correct |
12 |
Correct |
175 ms |
56112 KB |
Output is correct |
13 |
Correct |
162 ms |
58632 KB |
Output is correct |
14 |
Correct |
173 ms |
59648 KB |
Output is correct |
15 |
Correct |
182 ms |
57420 KB |
Output is correct |
16 |
Correct |
171 ms |
57704 KB |
Output is correct |
17 |
Correct |
140 ms |
59324 KB |
Output is correct |
18 |
Correct |
136 ms |
59684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Execution timed out |
4035 ms |
51200 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
5 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
25 |
Correct |
4 ms |
596 KB |
Output is correct |
26 |
Correct |
5 ms |
852 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
5 ms |
820 KB |
Output is correct |
29 |
Correct |
5 ms |
852 KB |
Output is correct |
30 |
Correct |
5 ms |
848 KB |
Output is correct |
31 |
Correct |
5 ms |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
584 KB |
Output is correct |
33 |
Correct |
4 ms |
596 KB |
Output is correct |
34 |
Correct |
4 ms |
596 KB |
Output is correct |
35 |
Correct |
4 ms |
596 KB |
Output is correct |
36 |
Correct |
5 ms |
596 KB |
Output is correct |
37 |
Correct |
4 ms |
596 KB |
Output is correct |
38 |
Correct |
4 ms |
596 KB |
Output is correct |
39 |
Correct |
3 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
596 KB |
Output is correct |
41 |
Correct |
408 ms |
48916 KB |
Output is correct |
42 |
Correct |
535 ms |
55828 KB |
Output is correct |
43 |
Correct |
495 ms |
55496 KB |
Output is correct |
44 |
Correct |
281 ms |
39476 KB |
Output is correct |
45 |
Correct |
537 ms |
57100 KB |
Output is correct |
46 |
Correct |
755 ms |
85444 KB |
Output is correct |
47 |
Correct |
456 ms |
52512 KB |
Output is correct |
48 |
Correct |
715 ms |
75412 KB |
Output is correct |
49 |
Correct |
751 ms |
81852 KB |
Output is correct |
50 |
Correct |
718 ms |
79232 KB |
Output is correct |
51 |
Correct |
712 ms |
77192 KB |
Output is correct |
52 |
Correct |
307 ms |
43080 KB |
Output is correct |
53 |
Correct |
542 ms |
57032 KB |
Output is correct |
54 |
Correct |
549 ms |
56024 KB |
Output is correct |
55 |
Correct |
560 ms |
58496 KB |
Output is correct |
56 |
Correct |
566 ms |
59492 KB |
Output is correct |
57 |
Correct |
544 ms |
58552 KB |
Output is correct |
58 |
Correct |
537 ms |
57596 KB |
Output is correct |
59 |
Correct |
327 ms |
59060 KB |
Output is correct |
60 |
Correct |
297 ms |
59576 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
97 ms |
40368 KB |
Output is correct |
63 |
Correct |
129 ms |
55968 KB |
Output is correct |
64 |
Correct |
116 ms |
55596 KB |
Output is correct |
65 |
Correct |
115 ms |
57164 KB |
Output is correct |
66 |
Correct |
163 ms |
85740 KB |
Output is correct |
67 |
Correct |
166 ms |
75644 KB |
Output is correct |
68 |
Correct |
151 ms |
79420 KB |
Output is correct |
69 |
Correct |
157 ms |
77516 KB |
Output is correct |
70 |
Correct |
128 ms |
43276 KB |
Output is correct |
71 |
Correct |
170 ms |
57380 KB |
Output is correct |
72 |
Correct |
175 ms |
56112 KB |
Output is correct |
73 |
Correct |
162 ms |
58632 KB |
Output is correct |
74 |
Correct |
173 ms |
59648 KB |
Output is correct |
75 |
Correct |
182 ms |
57420 KB |
Output is correct |
76 |
Correct |
171 ms |
57704 KB |
Output is correct |
77 |
Correct |
140 ms |
59324 KB |
Output is correct |
78 |
Correct |
136 ms |
59684 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
0 ms |
212 KB |
Output is correct |
81 |
Execution timed out |
4035 ms |
51200 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |