#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;
struct segtree
{
vi arr;
int size;
void init(int _size)
{
size = 1;
while (size < _size)
size *= 2;
arr.assign(2 * size, -INF);
}
void set(int i, ll v)
{
i += size;
while (i > 0)
{
arr[i] = max(arr[i], v);
i /= 2;
}
}
ll query(int l, int r) { return query(l, r, 1, 0, size); }
ll query(int l, int r, int x, int lx, int rx)
{
if (l <= lx && rx <= r)
return arr[x];
if (r <= lx || rx <= l)
return -INF;
int m = (lx + rx) / 2;
return max(query(l, r, 2 * x, lx, m), query(l, r, 2 * x + 1, m, rx));
}
};
segtree seg;
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;
while (Q--)
{
ll x, y, a, b;
cin >> x >> y >> a >> b;
--x;
L[x] = y;
vi B(L.begin() + a, L.begin() + b);
vi pref(sz(B));
partial_sum(all(B), pref.begin());
// starting with lower piece
seg.init(sz(B));
// seg used as dp
// vi dp(sz(B), -INF); // dp[i] := number of zigzag pieces in prefix i with i as minimum
vvpii activate(sz(B));
seg.set(0, 1);
for (int i = 1; i < sz(B); ++i)
{
for (pii tmp : activate[i])
seg.set(tmp.first, tmp.second);
ll needed = pref[i - 1] - B[i] - 1;
pii ac;
if (needed < 0)
ac = {i, 1};
else
{
int r = upper_bound(all(pref), pref[i - 1] - B[i] - 1) - pref.begin();
ac = {i, max(0LL, seg.query(0, r)) + 2};
}
int idx = lower_bound(all(pref), pref[i] + B[i] + 1) - pref.begin();
if (idx < sz(activate))
activate[idx].push_back(ac);
}
ll ans = seg.query(0, seg.size);
ll buffer = 0;
for (int i = sz(B) - 1; i > 0; --i)
{
buffer += B[i];
if (buffer > B[i - 1])
ans = max(ans, seg.arr[seg.size + i - 1] + 1);
}
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
394 ms |
5016 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
364 ms |
4732 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |