Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Alternating String

#include <atcoder/segtree>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

int op(int a, int b) { return a + b; }

int e() { return 0; }

void solve() {
    int n, q;
    cin >> n >> q;

    string str;
    cin >> str;

    segtree<int, op, e> st(n + 5);
    st.set(0, 1);

    for (int i = 1; i < n; i++) {
        int x = str[i] - '0';
        int y = str[i - 1] - '0';
        st.set(i, x ^ y);
    }

    for (int zz = 0; zz < q; zz++) {
        int type, L, R;
        cin >> type >> L >> R;
        L--;
        R--;

        if (type == 1) {
            st.set(L, 1 ^ st.get(L));
            st.set(R + 1, 1 ^ st.get(R + 1));
        }
        if (type == 2) {
            // Atcoder's segtre returns answer for range [o, c)
            // We query the answer for [L + 1 ... R]
            int diff_count = st.prod(L + 1, R + 1);
            if (diff_count == R - L) {
                cout << "Yes"
                     << "\n";
            } else {
                cout << "No"
                     << "\n";
            }
        }
    }
}

int main() {
    solve();
    return 0;
}