description: <image>
  
  As mentioned previously William really likes playing video games. In one of his favorite games, the player character is in a universe where every planet is designated by a binary number from 0 to 2^n - 1. On each planet, there are gates that allow the player to move from planet i to planet j if the binary representations of i and j differ in exactly one bit.
  
  William wants to test you and see how you can handle processing the following queries in this game universe:
  
    * Destroy planets with numbers from l to r inclusively. These planets cannot be moved to anymore.
    * Figure out if it is possible to reach planet b from planet a using some number of planetary gates. It is guaranteed that the planets a and b are not destroyed. 
  
  Input
  
  The first line contains two integers n, m (1 ≤ n ≤ 50, 1 ≤ m ≤ 5 ⋅ 10^4), which are the number of bits in binary representation of each planets' designation and the number of queries, respectively.
  
  Each of the next m lines contains a query of two types:
  
  block l r — query for destruction of planets with numbers from l to r inclusively (0 ≤ l ≤ r < 2^n). It's guaranteed that no planet will be destroyed twice.
  
  ask a b — query for reachability between planets a and b (0 ≤ a, b < 2^n). It's guaranteed that planets a and b hasn't been destroyed yet.
  
  Output
  
  For each query of type ask you must output "1" in a new line, if it is possible to reach planet b from planet a and "0" otherwise (without quotation marks).
  
  Examples
  
  Input
  
  
  3 3
  ask 0 7
  block 3 6
  ask 0 7
  
  
  Output
  
  
  1
  0
  
  
  Input
  
  
  6 10
  block 12 26
  ask 44 63
  block 32 46
  ask 1 54
  block 27 30
  ask 10 31
  ask 11 31
  ask 49 31
  block 31 31
  ask 2 51
  
  
  Output
  
  
  1
  1
  0
  0
  1
  0
  
  Note
  
  The first example test can be visualized in the following way:
  
  <image>
  
  Response to a query ask 0 7 is positive.
  
  Next after query block 3 6 the graph will look the following way (destroyed vertices are highlighted):
  
  <image>
  
  Response to a query ask 0 7 is negative, since any path from vertex 0 to vertex 7 must go through one of the destroyed vertices.
solution: #include <bits/stdc++.h>
  using namespace std;
  using ll = long long;
  struct Query {
    char q;
    ll a;
    ll b;
  };
  int gf(vector<int>& f, int cur) {
    int root = cur;
    while (f[root] >= 0) {
      root = f[root];
    }
    int next = f[cur];
    while (next >= 0) {
      f[cur] = root;
      cur = next;
      next = f[cur];
    }
    return root;
  }
  void mf(vector<int>& f, int a, int b) {
    a = gf(f, a);
    b = gf(f, b);
    if (a == b) {
      return;
    }
    f[a] = b;
  }
  int is_pow2(ll n) { return (n & (n - 1)) == 0; }
  int main() {
    std::ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<Query> q(m);
    for (int i = 0; i < m; ++i) {
      string s;
      cin >> s >> q[i].a >> q[i].b;
      q[i].q = s[0];
    }
    set<ll> pts;
    pts.insert(0);
    pts.insert((1ll << (n)));
    for (int i = 0; i < m; ++i) {
      if (q[i].q == 'b') {
        ll a = q[i].a;
        ll b = q[i].b + 1;
        pts.insert(a);
        pts.insert(b);
      }
    }
    vector<ll> pre_vals(pts.begin(), pts.end());
    for (int i = 0; i + 1 < pre_vals.size(); ++i) {
      ll a = pre_vals[i];
      ll b = pre_vals[i + 1];
      while (a < b) {
        for (int i = n - 1; i >= 0; --i) {
          if ((a & ((1ll << (i)) - 1)) == 0 && a + (1ll << (i)) <= b) {
            a += (1ll << (i));
            pts.insert(a);
            break;
          }
        }
      }
    }
    vector<int> ret(m);
    vector<ll> vals(pts.begin(), pts.end());
    vector<int> f(vals.size(), -1);
    vector<int> ok(vals.size());
    vector<int> bad(vals.size());
    for (int i = m - 1; i >= 0; --i) {
      if (q[i].q == 'b') {
        ll a = q[i].a;
        ll b = q[i].b + 1;
        int idx = lower_bound(vals.begin(), vals.end(), a) - vals.begin();
        for (; idx < vals.size() && vals[idx] < b; ++idx) {
          bad[idx] = 1;
        }
      }
    }
    int total = 0;
    vector<int> logw(vals.size());
    for (int i = 0; i + 1 < vals.size(); ++i) {
      ll w = vals[i + 1] - vals[i];
      int b = 0;
      while (w != 1) {
        ++b;
        w >>= 1;
      }
      logw[i] = b;
    }
    auto recover = [&](int idx) {
      assert(!ok[idx]);
      ok[idx] = 1;
      ll cur = vals[idx];
      int l_idx = logw[idx];
      for (int bit = l_idx; bit < n; ++bit) {
        ll next = (cur ^ (1ll << (bit)));
        int i = upper_bound(vals.begin(), vals.end(), next) - vals.begin() - 1;
        for (; i + 1 < vals.size(); ++i) {
          int l_i = logw[i];
          ll diff = ((vals[idx] ^ vals[i]) >> max(l_idx, l_i));
          if (!is_pow2(diff)) {
            break;
          }
          if (ok[i]) {
            ++total;
            mf(f, idx, i);
          }
        }
      }
    };
    for (int i = 0; i + 1 < vals.size(); ++i) {
      if (!bad[i]) {
        recover(i);
      }
    }
    for (int i = m - 1; i >= 0; --i) {
      if (q[i].q == 'b') {
        ll a = q[i].a;
        ll b = q[i].b + 1;
        int idx = lower_bound(vals.begin(), vals.end(), a) - vals.begin();
        for (; idx < vals.size() && vals[idx] < b; ++idx) {
          recover(idx);
        }
      } else {
        ll a = q[i].a;
        ll b = q[i].b;
        int idx_a = upper_bound(vals.begin(), vals.end(), a) - vals.begin() - 1;
        int idx_b = upper_bound(vals.begin(), vals.end(), b) - vals.begin() - 1;
        assert(ok[idx_a]);
        assert(ok[idx_b]);
        ret[i] = (gf(f, idx_a) == gf(f, idx_b) ? 1 : 0);
      }
    }
    for (int i = 0; i < m; ++i) {
      if (q[i].q == 'a') {
        printf("%d\n", ret[i]);
      }
    }
    return 0;
  }
  