Submission #1081402

#TimeUsernameProblemLanguageResultExecution timeMemory
1081402kwongwengFish 2 (JOI22_fish2)C++17
25 / 100
4086 ms10416 KiB
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef long double ld;
#define FOR(i,a,b) for(int i=a; i<b; i++)
#define ROF(i,a,b) for(int i=a; i>=b; i--)
#define pb push_back
#define fi first
#define se second
#define ms memset


int main(){
    ios::sync_with_stdio(false);
    cout.tie(0); cin.tie(0);
    int n; cin>>n; vector<ll> a(n); FOR(i,0,n) cin>>a[i];
    set<pair<ll,int>> A;
    FOR(i,0,n) A.insert({-a[i],i});
    vector<ll> pre(n+1); FOR(i,1,n+1) pre[i] = pre[i-1] + a[i-1];
    stack<int> s; vi l(n), r(n), ans(n);
    s.push(0); l[0]=-1;
    FOR(i,1,n){
        while (!s.empty()){
            if (a[s.top()] <= a[i]){
                s.pop();
            }else break;
        }
        if (s.empty()) l[i]=-1;
        else l[i] = s.top();
        s.push(i);
    }
    while (!s.empty()) s.pop();
    s.push(n-1); r[n-1] = n;
    ROF(i,n-2,0){
        while (!s.empty()){
            if (a[s.top()] <= a[i]){
                s.pop();
            }else break;
        }
        if (s.empty()) r[i]=n;
        else r[i] = s.top();
        s.push(i);
    }
    while (!s.empty()) s.pop();
    int q; cin>>q;
    FOR(_,0,q){
        int op; cin>>op;
        if (op==1){
            int pos, val; cin>>pos>>val; pos--; 
            A.erase({-a[pos],pos}); a[pos] = val; A.insert({-a[pos],pos});
            FOR(i,pos+1,n+1){
                pre[i] = pre[i-1] + a[i-1];
            }
            s.push(0); l[0]=-1;
            FOR(i,1,n){
                while (!s.empty()){
                    if (a[s.top()] <= a[i]){
                        s.pop();
                    }else break;
                }
                if (s.empty()) l[i]=-1;
                else l[i] = s.top();
                s.push(i);
            }
            while (!s.empty()) s.pop();
            s.push(n-1); r[n-1] = n;
            ROF(i,n-2,0){
                while (!s.empty()){
                    if (a[s.top()] <= a[i]){
                        s.pop();
                    }else break;
                }
                if (s.empty()) r[i]=n;
                else r[i] = s.top();
                s.push(i);
            }
            while (!s.empty()) s.pop();
            continue;
        }
        int L,R; cin>>L>>R; L--; R--;
        FOR(i,L,R+1) ans[i]=0;
        int answer = 0;
        for (auto it = A.begin(); it != A.end(); it++){
            int pos = (*it).se;
            if (pos<L || R<pos) continue;
            if (l[pos] < L && r[pos] > R){
                ans[pos]=1; continue;
            }
            if (l[pos] >= L && pre[min(R+1,r[pos])]-pre[max(L,l[pos]+1)] >= a[l[pos]]) ans[pos] |= ans[l[pos]];
            if (r[pos] <= R && pre[min(R+1,r[pos])]-pre[max(L,l[pos]+1)] >= a[r[pos]]) ans[pos] |= ans[r[pos]];
        }
        FOR(i,L,R+1) answer += ans[i];
        FOR(i,L,R+1) ans[i] = 0;
        cout<<answer<<"\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...