Submission #1364370

#TimeUsernameProblemLanguageResultExecution timeMemory
1364370kawhietStreet Lamps (APIO19_street_lamps)C++20
20 / 100
114 ms8596 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 47
#endif

struct SegmentTree {
    int n;
    vector<int> t;

    SegmentTree(int _n) {
        n = _n;
        t.resize(4 * n);
    }

    int merge(int x, int y) {
        return max(x, y);
    }

    void update(int id, int tl, int tr, int i, int v) {
        if (tl == tr) {
            t[id] = v;
            return;
        }
        int x = (id << 1) + 1, y = x + 1, tm = (tl + tr) >> 1;
        if (i <= tm) {
            update(x, tl, tm, i, v);
        } else {
            update(y, tm + 1, tr, i, v);
        }
        t[id] = merge(t[x], t[y]);
    }

    int get(int id, int tl, int tr, int l, int r) {
        if (r < tl || tr < l) return 0;
        if (l <= tl && tr <= r) return t[id];
        int x = (id << 1) + 1, y = x + 1, tm = (tl + tr) >> 1;
        return merge(get(x, tl, tm, l, r), get(y, tm + 1, tr, l, r));
    }

    void update(int i, int v) { update(0, 0, n - 1, i, v); }
    int get(int l, int r) { return get(0, 0, n - 1, l, r); }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    string s;
    cin >> n >> q >> s;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        a[i] = s[i] - '0';
    }
    SegmentTree k(n);
    for (int i = 0; i < n; i++) {
        if (a[i] == 1) {
            k.update(i, 0);
        } else {
            k.update(i, 1e9);
        }
    }
    for (int t = 1; t <= q; t++) {
        string c;
        cin >> c;
        if (c == "toggle") {
            int i;
            cin >> i;
            a[--i] ^= 1;
            k.update(i, t);
        } else {
            int l, r;
            cin >> l >> r;
            l--; r--; r--;
            int x = k.get(l, r);
            if (x != 1e9) {
                cout << t - x << '\n';
            } else {
                cout << 0 << '\n'; 
            }
        }
    }
    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...