제출 #440524

#제출 시각아이디문제언어결과실행 시간메모리
440524KULIKOLD가로등 (APIO19_street_lamps)C++17
60 / 100
295 ms17092 KiB
//#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int DIM = 3E5+7;
int n,q;
int A[DIM];
struct node{
    int type,a,b;
} Q[DIM];
const int SZ = 107;
vector<node> V[SZ];
int ans[DIM];
void solve(){
    for(int i = q;i>=1;--i){
        V[i] = V[i+1];
        if (Q[i].type==1)
            V[i].push_back({i,Q[i].a,Q[i].b});
    }
    for(int i = 1;i<=q;++i){
        for(auto to:V[i]){
            ll flag = 0;
            for(int j = to.a;j<to.b;++j){
                if (!A[j]){
                    flag = 1;
                    break;
                }
            }
            if (!flag)
                ans[to.type]++;
        }
        if (Q[i].type==0)
            A[Q[i].a]^=1;

        if (Q[i].type)
        cout<<ans[i]<<endl;
    }

}
int last[DIM],sum[DIM];
void solve1(){

    for(int i = 1;i<=q;++i){
        if (Q[i].type==1){
            cout<<(i-last[Q[i].a])*A[Q[i].a]+sum[Q[i].a]<<endl;
        }
        else{
            sum[Q[i].a]+=(i-last[Q[i].a])*A[Q[i].a];
            last[Q[i].a] = i;
            A[Q[i].a]^=1;
        }
    }
}
int tt[DIM],T[DIM*4];
const int INF = 1E9;
void buildtree(int t,int tl,int tr){
    if (tl==tr){
        T[t] = tt[tl];
        return;
    }
    int tm = (tl+tr)/2;
    buildtree(t*2+1,tl,tm);
    buildtree(t*2+2,tm+1,tr);
    T[t] = max(T[t*2+1],T[t*2+2]);
}
int query(int t,int tl,int tr,int l,int r){
    if (tl>r || l>tr)
        return 0;
    if (l<=tl && tr<=r)
        return T[t];
    int tm = (tl+tr)/2;
    return max(query(t*2+1,tl,tm,l,r),query(t*2+2,tm+1,tr,l,r));
}
void solve2(){
    for(int i = 1;i<=n;++i){
        if (!A[i])
            tt[i] = INF;
    }
    for(int i = 1;i<=q;++i){
        if (Q[i].type==0)
            tt[Q[i].a] = i;
    }
    buildtree(0,1,n);
    for(int i = 1;i<=q;++i){
        if (Q[i].type==1){
            cout<<max(0,i-query(0,1,n,Q[i].a,Q[i].b-1))<<endl;
        }
    }
}
int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>q;
    for(int i = 1;i<=n;++i){
        char ch;
        cin>>ch;
        A[i] = int(ch-'0');
    }
    int flag = 0,flag1 = 0;
    for(int i = 1;i<=q;++i){
        string s;
        cin>>s;
        Q[i].type = (s=="query");
        if (Q[i].type==1) {
            cin >> Q[i].a >> Q[i].b;
            if (Q[i].b-Q[i].a!=1)
                flag = 1;
        }
        else cin>>Q[i].a;
        if (Q[i].type==0)
            if (A[Q[i].a])
                flag1 = 1;
    }
    if (n<=100 && q<=100)
        solve();
    else if (flag==0)
        solve1();
    else if (flag1==0)
        solve2();
    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...