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 i64 = long long;
  struct DSU {
    std::vector<int> f, siz;
    DSU(int n) : f(n), siz(n, 1) { std::iota(f.begin(), f.end(), 0); }
    int leader(int x) {
      while (x != f[x]) x = f[x] = f[f[x]];
      return x;
    }
    bool same(int x, int y) { return leader(x) == leader(y); }
    bool merge(int x, int y) {
      x = leader(x);
      y = leader(y);
      if (x == y) return false;
      siz[x] += siz[y];
      f[y] = x;
      return true;
    }
    int size(int x) { return siz[leader(x)]; }
  };
  int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n, m;
    std::cin >> n >> m;
    std::vector<i64> v{0LL, 1LL << n};
    int op[m];
    i64 x[m], y[m];
    for (int i = 0; i < m; i++) {
      std::string t;
      std::cin >> t;
      if (t == "block") {
        op[i] = 0;
        std::cin >> x[i] >> y[i];
        y[i]++;
        v.push_back(x[i]);
        v.push_back(y[i]);
      } else {
        op[i] = 1;
        std::cin >> x[i] >> y[i];
      }
    }
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());
    int N = v.size() - 1;
    for (int i = 0; i < N; i++) {
      i64 l = v[i];
      i64 r = v[i + 1] - 1;
      int k = 0;
      while ((l >> k) != (r >> k)) {
        k++;
      }
      if (k > 0) {
        v.push_back(r & ~((1LL << (k - 1)) - 1));
      }
    }
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());
    N = v.size() - 1;
    DSU g(N);
    std::vector<int> adj[N];
    auto add = [&](int i, int j, int k) {
      i64 l = std::max(v[i], v[j] - (1LL << k));
      i64 r = std::min(v[i + 1], v[j + 1] - (1LL << k)) - 1;
      if ((~l >> k & 1) || (~r >> k & 1) || (l >> k) != (r >> k)) {
        adj[i].push_back(j);
        adj[j].push_back(i);
      }
    };
    for (int k = 0; k < n; k++) {
      i64 d = 1LL << k;
      for (int i = 0, j = 0; i < N; i++) {
        while (j < N && v[j + 1] <= v[i] + d) {
          j++;
        }
        while (j < N && v[j + 1] <= v[i + 1] + d) {
          add(i, j, k);
          j++;
        }
        if (j < N && v[j] < v[i + 1] + d) {
          add(i, j, k);
        }
      }
    }
    bool vis[N];
    std::fill(vis, vis + N, true);
    for (int i = 0; i < m; i++) {
      if (op[i] == 0) {
        int j = std::lower_bound(v.begin(), v.end(), x[i]) - v.begin();
        while (v[j] < y[i]) {
          vis[j++] = false;
        }
      }
    }
    for (int i = 0; i < N; i++) {
      if (vis[i]) {
        for (auto j : adj[i]) {
          if (vis[j]) {
            g.merge(i, j);
          }
        }
      }
    }
    int res[m];
    for (int i = m - 1; i >= 0; i--) {
      if (op[i] == 0) {
        int j = std::lower_bound(v.begin(), v.end(), x[i]) - v.begin();
        while (v[j] < y[i]) {
          vis[j] = true;
          for (auto k : adj[j]) {
            if (vis[k]) {
              g.merge(j, k);
            }
          }
          j++;
        }
      } else {
        int a = std::upper_bound(v.begin(), v.end(), x[i]) - v.begin() - 1;
        int b = std::upper_bound(v.begin(), v.end(), y[i]) - v.begin() - 1;
        res[i] = g.same(a, b);
      }
    }
    for (int i = 0; i < m; i++) {
      if (op[i] == 1) {
        std::cout << res[i] << "\n";
      }
    }
    return 0;
  }
  