#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; struct S { bool to[2]; int into[2], outfrom[2]; ll res; }; S op(S x, S y) { S z{}; rep(i, 2) { z.to[i] = y.to[x.to[i]]; z.into[y.to[i]] += x.into[i]; z.into[i] += y.into[i]; z.outfrom[i] += x.outfrom[i]; z.outfrom[i] += y.outfrom[x.to[i]]; z.res += (ll)x.into[i] * y.outfrom[i]; } z.res += x.res + y.res; return z; } S e() { S s; rep(i, 2) s.to[i] = i; return s; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; string x; cin >> x; segtree seg([&]() { vector init((n + 1) / 2); for (int i = -1; i < n; i += 2) { S s{}; char op = i == -1 ? '+' : x[i]; bool val = x[i+1] == 'T'; rep(i, 2) { int j = op == '+' ? i | val : op == '*' ? i & val : i ^ val; s.to[i] = j; s.outfrom[i] = j; } s.into[val] = 1; s.res = val; init[(i + 1) / 2] = s; } return init; }()); rep(_, q) { int l, r; cin >> l >> r; l--; l = (l + 1) / 2; r = (r + 1) / 2; cout << seg.prod(l, r).res << '\n'; } }