Submission #1370161

#TimeUsernameProblemLanguageResultExecution timeMemory
1370161eldorbek_008Street Lamps (APIO19_street_lamps)C++20
20 / 100
207 ms23904 KiB
#include <bits/stdc++.h>
using namespace std;

#define int int64_t

int min(int a, int b) {
    return (a < b ? a : b);
}
int max(int a, int b) {
    return (a > b ? a : b);
}

struct node {
    int sm = 0, mx = 0;
};

struct segtree {
    int n;
    vector<node> t;

    segtree(int x) {
        n = x;
        t.resize(4 * x + 1);
    };

    void upd_sum(int node, int l, int r, int i, int x) {
        if (l == r) {
            t[node].sm = x;
        } else {
            int mid = (l + r) >> 1;
            if (i <= mid)
                upd_sum(2 * node, l, mid, i, x);
            else
                upd_sum(2 * node + 1, mid + 1, r, i, x);
            t[node].sm = t[2 * node].sm + t[2 * node + 1].sm;
        }
    }
    void upd_max(int node, int l, int r, int i, int x) {
        if (l == r) {
            t[node].mx = x;
        } else {
            int mid = (l + r) >> 1;
            if (i <= mid)
                upd_max(2 * node, l, mid, i, x);
            else
                upd_max(2 * node + 1, mid + 1, r, i, x);
            t[node].mx = max(t[2 * node].mx, t[2 * node + 1].mx);
        }
    }

    int get_sum(int node, int l, int r, int L, int R) {
        if (l > r or L > r or l > R) {
            return 0;
        } else if (L <= l and r <= R) {
            return t[node].sm;
        } else {
            int mid = (l + r) >> 1;
            int i = get_sum(2 * node, l, mid, L, R);
            int j = get_sum(2 * node + 1, mid + 1, r, L, R);
            return i + j;
        }
    }
    int get_max(int node, int l, int r, int L, int R) {
        if (l > r or L > r or l > R) {
            return 0;
        } else if (L <= l and r <= R) {
            return t[node].mx;
        } else {
            int mid = (l + r) >> 1;
            int i = get_max(2 * node, l, mid, L, R);
            int j = get_max(2 * node + 1, mid + 1, r, L, R);
            return max(i, j);
        }
    }
};

void solution() {
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    vector<int> f(n + 1);
    for (int i = 1; i <= n; i++) {
        f[i] = int(s[i - 1]) - 48;
    }
    
    segtree st(n + 1);
    for (int i = 1; i <= n; i++) {
        if (f[i] == 1) {
            st.upd_sum(1, 1, n, i, 1);
            st.upd_max(1, 1, n, i, 1);
        }
    }

    int tm = 2;
    while (q--) {
        cin >> s;
        if (s == "query") {
            int l, r;
            cin >> l >> r;
            int sm = st.get_sum(1, 1, n, l, r - 1);
            int mx = st.get_max(1, 1, n, l, r - 1);
            if (sm == r - l) {
                cout << tm - mx << '\n';
            } else {
                cout << 0 << '\n';
            }
        } else {
            int i;
            cin >> i;
            if (f[i] == 0) {
                st.upd_sum(1, 1, n, i, 1);
                st.upd_max(1, 1, n, i, tm);
            } else {
                st.upd_sum(1, 1, n, i, 0);
                st.upd_max(1, 1, n, i, 1);
            }
            f[i] ^= 1;
        }
        tm++;
    }
}

int32_t main() {
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;
    while (t--) {
        solution();
    }

    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...