Submission #568904

#TimeUsernameProblemLanguageResultExecution timeMemory
568904birthdaycakeStreet Lamps (APIO19_street_lamps)C++17
20 / 100
43 ms14912 KiB
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define mod 1000000007
#define boost ios_base::sync_with_stdio(false), cin.tie(NULL);
using namespace std;
 
 
 
int pref[300001],sub[300001],ans[300001],l[300001],seg[2000001];
int N,Left,Right,Value;

void Update(){
    int ind = N + Left - 1;
    seg[ind] = Value;
    while(ind /= 2) seg[ind] = max(seg[ind * 2], seg[ind * 2 + 1]);
}


int Query(int l = 1, int r = N, int ind = 1){
    if(l > Right || r < Left) return 0;
    if(l >= Left && r <= Right) return seg[ind];
    int mid = (l + r) / 2;
    return max(Query(l, mid + 1, ind * 2), Query(mid + 1, r, ind * 2 + 1));
}
signed main() {
 
    int n,q; cin >> n >> q;
    string a; cin >> a;
    if(n <= 100 && q <= 100){
        for(int i = 0; i < n; i++){
            if(a[i] == '1') pref[i]++;
            if(i) pref[i] += pref[i - 1];
            sub[i] = pref[i];
        }
        vector<pair<string,pair<int,int>>>qw(q);
        for(int i = 0; i < q; i++){
            cin >> qw[i].first >> qw[i].second.first;
            if(qw[i].first != "toggle") cin >> qw[i].second.second;
            else qw[i].second.second = 0;
            int a = qw[i].second.first - 1, b = qw[i].second.second - 1;
            if(qw[i].first == "query"){
                int ans = 0;
                for(int j = 0; j <= i; j++){
                    if(qw[j].first == "query"){
                        if(sub[b - 1] - (a == 0 ? 0 : sub[a - 1]) == (b - a)) ans++;
                    }else{
                        if(sub[b - 1] - (a == 0 ? 0 : sub[a - 1]) == (b - a)) ans++;
                        int c = qw[j].second.first - 1;
                        if(sub[c] - (c == 0 ? 0 : sub[c - 1]) == 1){
                            for(int k = c; k < n; k++) sub[k]--;
                        }else{
                            for(int k = c; k < n; k++) sub[k]++;
                        }
                    }
                }
                cout << ans << endl;
                for(int j = 0; j < n; j++){
                    sub[j] = pref[j];
                }
            }
            
            
        }
        return 0;
    }
    int f = 0;
    vector<pair<string,pair<int,int>>>qw(q);
    for(int i = 0; i < q; i++){
        cin >> qw[i].first >> qw[i].second.first >> qw[i].second.second;
        if(qw[i].second.second - qw[i].second.first > 1) f = 1;
    }
    if(f){
        N = exp2(ceil(log2(n)));
        for(int i = 0; i < n; i++){
            if(a[i] == '0'){
                Value = 1e18;
                Left = i + 1;
                Update();
            }
        }
        for(int i = 0; i < q; i++){
            if(qw[i].first == "query"){
                Left = qw[i].second.first; Right = qw[i].second.second - 1;
                int ans = Query();
                if(ans == 1e18){
                    cout << 0 << endl;
                }else{
                    cout << i - ans + 1 << endl;
                }
            }else{
                Value = i;
                Left = qw[i].second.first;
                Update();
            }
        }
        return 0;
    }else{
        for(int i = 0; i < q; i++){
            string t; t = qw[i].first;
            if(t == "toggle"){
                int x = qw[i].second.first;
                x--;
                if(a[x] == '0'){
                    a[x] = '1';
                    l[x] = i + 1;
                }else{
                    ans[x] += (i - l[x] + 1);
                    a[x] = '0';
                }
                
            }else{
                int x = qw[i].second.first;
                x--;
                if(a[x] == '1'){
                    ans[x] += (i - l[x] + 1);
                    l[x] = i + 1;
                }
                cout << ans[x] << endl;
            }
        }
    }
    
    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...