#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 int INF = 1e9;
int N, Q;
vi L;
vi pref;
vi vor, nach;
vvi lift;
vi vorg;
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;
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];
}
while (Q--)
{
ll x, y, a, b;
cin >> x >> y >> a >> b;
--x;
L[x] = y;
ll ans = -INF;
// assume we start with a minimum
{
int tmp = b - 1;
int 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
if (a <= vor[tmp])
++mini;
ans = mini;
}
// 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
if (a <= vor[tmp])
++mini;
ans = max(ans, mini);
break;
}
}
}
cout << ans << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
95 ms |
40388 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
131 ms |
51256 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |