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];
vector<pii> tmp;
stack<int> st;
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 {
tmp.clear();
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()]) tmp.push_back({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()]) tmp.push_back({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()]) tmp.push_back({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()]) tmp.push_back({a, st.top() - 1});
st.pop();
}
int ans = 0;
for(int i = a; i <= b; ++i) {
int flag = 1;
for(auto p : tmp) {
if(p.first <= i && i <= p.second) flag = 0;
}
ans += flag;
}
cout << 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... |