This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |