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>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define inf (1987654321)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
int n, q;
int arr[100005];
ll sum[100005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 1; i <= n; ++i) cin >> arr[i];
for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + arr[i];
cin >> q;
while(q--) {
int t, a, b;
cin >> t >> a >> b;
if(t == 1) {
arr[a] = b;
for(int i = a; i <= n; ++i) sum[i] = sum[i - 1] + arr[i];
}
else {
int mx[100005];
stack<int> st;
for(int i = a; i <= b; ++i) mx[i] = i - 1;
for(int i = a; i <= b; ++i) {
while(!st.empty() && arr[st.top()] <= arr[i]) {
if(st.top() + 1 < i && sum[i - 1] - sum[st.top()] < arr[st.top()]) mx[st.top() + 1] = i - 1;
st.pop();
}
st.push(i);
}
while(!st.empty()) {
if(st.top() + 1 < b + 1 && sum[b] - sum[st.top()] < arr[st.top()]) mx[st.top() + 1] = b;
st.pop();
}
for(int i = b; i >= a; --i) {
while(!st.empty() && arr[st.top()] <= arr[i]) {
if(i + 1 < st.top() && sum[st.top() - 1] - sum[i] < arr[st.top()]) mx[i + 1] = max(mx[i + 1], st.top() - 1);
st.pop();
}
st.push(i);
}
while(!st.empty()) {
if(a < st.top() && sum[st.top() - 1] - sum[a - 1] < arr[st.top()]) mx[a] = max(mx[a], mx[st.top() - 1]);
st.pop();
}
int ans = 0, i = a;
while(i <= b) {
ans += mx[i] - i + 1;
i = max(i + 1, mx[i] + 1);
}
cout << b - a + 1 - ans << '\n';
}
}
return 0;
}
# | 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... |