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;
#define vec vector
#define int long long
const int INF = 1e17;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vec<int> A(N);
set<pair<int, int>> A_s;
for(int i = 0; i<N; i++) {
cin >> A[i];
A_s.insert({-A[i], i});
}
int Q;
cin >> Q;
while(Q--) {
//cerr << "f" << '\n';
int T;
cin >> T;
int X, Y;
cin >> X >> Y;
X--;
if(T==1) {
A_s.erase({-A[X], X});
A[X] = Y;
A_s.insert({-Y, X});
continue;
}
vec<int> l(N, -1);
vec<int> r(N, N);
vec<pair<int, int>> hi{{INF, -1}};
for(int i = X; i<Y; i++) {
while(hi.back().first <= A[i]) {
hi.pop_back();
//cerr << "here1" << '\n';
}
l[i] = hi.back().second;
//cerr << l[i] << ' ';
hi.push_back({A[i], i});
}
//cerr << '\n';
vec<pair<int, int>> hi_r{{INF, N}};
for(int i = Y-1; i>=X; i--) {
while(hi_r.back().first <= A[i]) {
//cerr << "here2" << '\n';
hi_r.pop_back();
}
r[i] = hi_r.back().second;
hi_r.push_back({A[i], i});
}
vec<bool> valid(N);
vec<int> prefixsums(N+2);
for(int i = 1; i<=N; i++) {
prefixsums[i] = prefixsums[i-1] + A[i-1];
}
prefixsums[N+1] = prefixsums[N];
//valid[(*A_s.begin()).second] = true;
int ans = 0;
for(auto [_, i] : A_s) {
if(i>=Y || i<X) continue;
int cl = l[i];
int cr = r[i];
int sum = prefixsums[min(cr, Y)]-prefixsums[max(cl+1, X)];
//cerr << i << ' ' << sum << '\n';
if(cl != -1) {
if(sum >= A[cl]) valid[i] = valid[i] | valid[cl];
}
if(cr != N) {
if(sum >= A[cr]) valid[i] = valid[i] | valid[cr];
}
if(cl == -1 && cr == N) valid[i] = true;
//cerr << i << ": " << valid[i] << '\n';
ans += valid[i];
}
cout << ans << '\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... |