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;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e9+7;
const int LIM=25e4+7;
int tr[4*LIM], dp[LIM], N=1;
vector<int>V[LIM];
ll sum[LIM];
ll cnt(int l, int r) {
return sum[r]-(l?sum[l-1]:0);
}
void upd(int v, int x) {
v+=N;
while(v) {
tr[v]=max(tr[v], x);
v/=2;
}
}
int cnt2(int l, int r) {
if(l>r) return -INF;
l+=N; r+=N;
int ans=max(tr[l], tr[r]);
while(l/2!=r/2) {
if(l%2==0) ans=max(ans, tr[l+1]);
if(r%2==1) ans=max(ans, tr[r-1]);
l/=2; r/=2;
}
return ans;
}
int solve(vector<ll>T) {
int n=T.size();
rep(i, n+1) V[i].clear();
rep(i, n) {
sum[i]=T[i];
if(i) sum[i]+=sum[i-1];
dp[i]=-INF;
}
N=1;
while(N<n) N*=2;
rep(i, 2*N) tr[i]=-INF;
rep(i, n) {
for(auto j : V[i]) upd(j, dp[j]);
if(i==0) dp[i]=1;
else if(cnt(0, i-1)>T[i]) {
dp[i]=2;
int po=0, ko=i-1;
while(po<ko) {
int sr=(po+ko+1)/2;
if(cnt(sr, i-1)>T[i]) po=sr; else ko=sr-1;
}
dp[i]=max(dp[i], cnt2(0, po-1)+2);
}
if(cnt(i+1, n-1)>T[i]) {
int po=i+1, ko=n-1;
while(po<ko) {
int sr=(po+ko)/2;
if(cnt(i+1, sr)>T[i]) ko=sr; else po=sr+1;
}
V[po+1].pb(i);
}
}
int ans=max(1, dp[n-1]);
rep(i, n-1) if(cnt(i+1, n-1)>T[i]) ans=max(ans, dp[i]+1);
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
vector<ll>T(n);
rep(i, n) cin >> T[i];
int q;
cin >> q;
while(q--) {
ll x, y, a, b;
cin >> x >> y >> a >> b; --x; --b;
T[x]=y;
vector<ll>P;
while(a<=b) {
P.pb(T[a]);
++a;
}
cout << solve(P) << '\n';
}
}
# | 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... |