OI XXVIII - lic

// https://szkopul.edu.pl/problemset/problem/w3anjkOGa2eMEBt_-GYEosat/site/?key=statement

// Licznik długu

#include <bits/stdc++.h>

// #define GARY_DBG
#define GARY_LIB

constexpr int sizik = 100 * 1001;
constexpr int WEWNETRZNY = 0, ZEWNETRZNY = 1;

#define ar std::array

typedef std::vector<std::vector<int>> _kra;

int n, z;

ar<int, sizik> A, B, R;
ar<int, sizik> active;

std::set<std::pair<int, int>> dz;

int conv(int i) {
    return n - i + 1;
}

void oblicz_sume() {
    for (int i = n; i >= 1; i--) {
        int sum = A[i] + B[i] + R[i];
        if (sum >= 10) {
            R[i - 1] = 1;
        } else {
            R[i - 1] = 0;
        }
    }
}

void update(int x, int i, int cyfra) {
    int pos = conv(i);

    bool before_eq9 = (A[pos] + B[pos]) == 9;
    bool before_geq10 = (A[pos] + B[pos]) >= 10;

    if (x == WEWNETRZNY) {
        A[pos] = cyfra;
    } else {
        B[pos] = cyfra;
    }

    bool after_eq9 = (A[pos] + B[pos]) == 9;
    bool after_geq10 = (A[pos] + B[pos]) >= 10;

    if (after_eq9 == 1 && after_eq9 == before_eq9) {
        return;
    }
    if (after_geq10 == 1 && after_geq10 == before_geq10) {
        return;
    }

    if (before_eq9) {
        auto it = dz.upper_bound({pos, INT32_MAX});
        assert(it != dz.begin());
        --it;
        const auto [a, b] = *it;
        if (a == b) {
            dz.erase(it);
            active[b] = 0;
        } else if (a == pos) {
            dz.erase(it);
            dz.insert({a + 1, b});

            R[pos] = active[b];
        } else if (b == pos) {
            dz.erase(it);
            dz.insert({a, b - 1});
            active[b] = 0;
            active[b - 1] = after_geq10;
            R[a - 1] = after_geq10;
        } else {
            dz.erase(it);
            dz.insert({a, pos - 1});
            dz.insert({pos + 1, b});
            active[pos - 1] = after_geq10;
            R[a - 1] = after_geq10;
            R[pos] = active[b];
        }

        R[pos - 1] = after_geq10;
    } else if (after_eq9) {
        bool isLeft = false, isRight = false;
        if ((A[pos - 1] + B[pos - 1]) == 9) {
            isLeft = true;
        }
        if ((A[pos + 1] + B[pos + 1]) == 9) {
            isRight = true;
        }

        if (!isLeft && !isRight) {
            dz.insert({pos, pos});
            active[pos] = R[pos];

            R[pos - 1] = R[pos];
        } else if (isLeft && !isRight) {
            auto it = dz.upper_bound({pos - 1, INT32_MAX});
            assert(it != dz.begin());
            --it;
            const auto [a, b] = *it;
            assert(b == pos - 1);

            dz.erase(it);
            dz.insert({a, pos});

            active[b] = 0;
            active[pos] = R[pos];

            R[a - 1] = R[pos];
        } else if (!isLeft && isRight) {
            auto it = dz.upper_bound({pos + 1, INT32_MAX});
            assert(it != dz.begin());
            --it;
            const auto [a, b] = *it;
            assert(a == pos + 1);

            dz.erase(it);
            dz.insert({pos, b});

            R[pos - 1] = active[b];
        } else if (isLeft && isRight) {
            auto it = dz.upper_bound({pos - 1, INT32_MAX});
            assert(it != dz.begin());
            --it;
            const auto [a, b] = *it;
            assert(b == pos - 1);

            it++;
            const auto [a1, b1] = *it;
            assert(a1 == pos + 1);

            dz.erase(it);
            it = dz.upper_bound({pos - 1, INT32_MAX});
            assert(it != dz.begin());
            --it;
            dz.erase(it);

            dz.insert({a, b1});

            active[b] = 0;

            R[a - 1] = active[b1];
        }

    } else if (after_geq10) {
        R[pos - 1] = 1;
        if ((A[pos - 1] + B[pos - 1]) == 9) {
            active[pos - 1] = 1;
        }

        if ((A[pos - 1] + B[pos - 1]) == 9) {
            auto it = dz.upper_bound({pos - 1, INT32_MAX});
            assert(it != dz.begin());
            it--;
            const auto [a, b] = *it;
            R[a - 1] = 1;
        }
    } else if (before_geq10) {
        R[pos - 1] = 0;
        if ((A[pos - 1] + B[pos - 1]) == 9) {
            active[pos - 1] = 0;
        }

        if ((A[pos - 1] + B[pos - 1]) == 9) {
            auto it = dz.upper_bound({pos - 1, INT32_MAX});
            assert(it != dz.begin());
            it--;
            const auto [a, b] = *it;
            R[a - 1] = 0;
        }
    }
}

void oblicz_przedzialy() {
    int prev = -1;
    for (int i = n; i >= 0; i--) {
        int s = A[i] + B[i];
        if (s == 9) {
            if (prev == -1) prev = i;
        } else {
            if (prev != -1) {
                dz.insert({i + 1, prev});
                prev = -1;
            }
        }
    }

    for (const auto& [a, b] : dz) {
        if (R[a] == 1 && R[b] == 1) {
            active[b] = true;
        }
    }
}

int daj_wynik(int i) {
    int pos = conv(i);

    if ((A[pos] + B[pos]) == 9) {
        auto it = dz.upper_bound({pos, INT32_MAX});

        assert(it != dz.begin());
        --it;

        const auto [a, b] = *it;
        if (active[b]) {
            return 0;
        } else {
            return 9;
        }
    } else {
        return (A[pos] + B[pos] + R[pos]) % 10;
    }
}

void solve() {
    std::cin >> n >> z;

    for (int i = 2; i <= n; i++) {
        char c;
        std::cin >> c;
        A[i] = c - '0';
    }
    for (int i = 2; i <= n; i++) {
        char c;
        std::cin >> c;
        B[i] = c - '0';
    }

    oblicz_sume();
    oblicz_przedzialy();

    for (int q = 1; q <= z; q++) {
        char c;
        std::cin >> c;

        if (c == 'W') {
            int i, h;
            std::cin >> i >> h;

            update(WEWNETRZNY, i, h);

        } else if (c == 'Z') {
            int i, h;
            std::cin >> i >> h;

            update(ZEWNETRZNY, i, h);

        } else if (c == 'S') {
            int i;
            std::cin >> i;

            int ansix = daj_wynik(i);
            std::cout << ansix << '\n';
        }
    }
}

int32_t main() {
#ifndef GARY_DBG
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
#endif

    int t = 1;
    // std::cin >> t;

    for (; t > 0; t--) {
        solve();
    }

    return 0;
}