제출 #1324972

#제출 시각아이디문제언어결과실행 시간메모리
1324972madaratSterilizing Spray (JOI15_sterilizing)C++20
100 / 100
547 ms8356 KiB
#include <iostream>
#include <set>
#define endl '\n'

using namespace std;

long long segt[4*100'001];
long long n, k;
long long mas[100'001];
set<long long> st;

void upd(int i, int v, int l=0, int r=n-1, int pos=1){
    if(l==r){
        segt[pos]=v;
    }
    else{
        long long mid=(l+r)/2;
        if(i<=mid)
            upd(i, v, l, mid, pos*2);
        else
            upd(i, v, mid+1, r, pos*2+1);
        segt[pos]=segt[pos*2]+segt[pos*2+1];
    }
}

long long getsum(int ls, int rs, int l=0, int r=n-1, int pos=1){
    if(r<ls || l>rs)
        return 0;
    if(ls<=l && r<=rs)
        return segt[pos];
    long long mid=(l+r)/2;
    return getsum(ls, rs, l, mid, pos*2)+getsum(ls, rs, mid+1, r, pos*2+1);
}

void debug(){
    for(int i=0; i<n; i++)
        cout<<getsum(i, i)<<' ';
    cout<<endl;
    for(int i=0; i<n; i++)
        cout<<mas[i]<<' ';
    cout<<endl;
    cout<<endl;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    long long q; cin>>n>>q>>k;
    for(long long i=0; i<n; i++){
        cin>>mas[i];
        if(mas[i])
            st.insert(i);
        upd(i, mas[i]);
    }

//    debug();

    for(long long i=0; i<q; i++){
        long long s; cin>>s;
        if(s==1){
            long long a, b; cin>>a>>b; a--;
            mas[a]=b;
            if(b)
                st.insert(a);
            else if(st.count(a) && b==0)
                st.erase(a);
            upd(a, b);
        }
        else if(s==2){
            long long l, r; cin>>l>>r; l--, r--;
            if(k==1)
                continue;

            auto it = st.lower_bound(l);
            while(it!=st.end() && *it<=r){
                mas[*it]/=k;
                upd(*it, mas[*it]);
                if(mas[*it]==0)
                    st.erase(*it);
                l=*it+1;
                it = st.lower_bound(l);
            }
//            debug();
        }
        else{
            long long l, r; cin>>l>>r; l--, r--;
            cout<<getsum(l, r)<<endl;
        }
//        cout<<"Sis: ";
//        debug();
    }
    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...