#include <bits/stdc++.h>   using namespace std;   const int MAX = 100005;   template <typename T>   void readv(vector<T>& v, int n) {     for (int i = 0; i < n; ++i) {       T tmp;       cin >> tmp;       v.push_back(tmp);     }   }   struct cmp {     bool operator()(const pair<int, int>& i1, const pair<int, int>& i2) const {       int sign = (i1.second - i1.first) - (i2.second - i2.first);       if (sign != 0) return sign > 0;       return i1.first > i2.first;     }   };   set<pair<int, int>, cmp> intervals;   map<int, pair<int, int> > ileft, iright;   void insert_intvl(const pair<int, int>& intvl) {     intervals.insert(intvl);     ileft[intvl.second + 1] = intvl;     iright[intvl.first - 1] = intvl;   }   void erase_intvl(const pair<int, int>& intvl) {     intervals.erase(intvl);     ileft.erase(intvl.second + 1);     iright.erase(intvl.first - 1);   }   map<int, int> points;   class node {    public:     node() : left(0), right(0), size(0) {}     node *left, *right;     int size;   };   node root;   void add_node(node* nd, int x, int shift) {     nd->size++;     if (shift < 0) {       return;     }     if ((x & (1 << shift)) == 0) {       if (!nd->left) {         nd->left = new node;       }       add_node(nd->left, x, shift - 1);     } else {       if (!nd->right) {         nd->right = new node;       }       add_node(nd->right, x, shift - 1);     }   }   void remove_node(node* nd, int x, int shift) {     nd->size -= 1;     if (shift < 0) return;     if ((x & (1 << shift)) == 0) {       remove_node(nd->left, x, shift - 1);     } else {       remove_node(nd->right, x, shift - 1);     }   }   int query_tree(node* nd, int b, int e, int l, int r, int shift) {     if ((e < l) || (b > r)) {       return 0;     }     if ((b >= l) && (e <= r)) {       return nd->size;     }     int mid = (b + e) / 2;     int s1 = nd->left ? query_tree(nd->left, b, mid, l, r, shift - 1) : 0;     int s2 = nd->right ? query_tree(nd->right, mid + 1, e, l, r, shift - 1) : 0;     return s1 + s2;   }   const int BITS = 30;   void add_emp(int e, int p) {     add_node(&root, p, BITS - 1);     points[e] = p;   }   void remove_emp(int e) {     remove_node(&root, points[e], BITS - 1);     points.erase(e);   }   int query(int l, int r) {     return query_tree(&root, 0, (1 << BITS) - 1, l, r, BITS);   }   int main(int argc, char** argv) {     int n, q;     cin >> n >> q;     intervals.insert(make_pair(1, n));     for (int i = 0; i < q; ++i) {       int e;       cin >> e;       if (e == 0) {         int l, r;         cin >> l >> r;         cout << query(l, r) << endl;       } else {         if (points.count(e) == 0) {           pair<int, int> longest = *intervals.begin();           erase_intvl(longest);           int mid = (longest.first + longest.second + 1) / 2;           if (longest.first <= (mid - 1)) {             insert_intvl(make_pair(longest.first, mid - 1));           }           if ((mid + 1) <= longest.second) {             insert_intvl(make_pair(mid + 1, longest.second));           }           add_emp(e, mid);         } else {           int pe = points[e];           pair<int, int> intvl = make_pair(pe, pe);           if (ileft.count(pe) == 1) {             intvl.first = ileft[pe].first;             erase_intvl(ileft[pe]);           }           if (iright.count(pe) == 1) {             intvl.second = iright[pe].second;             erase_intvl(iright[pe]);           }           insert_intvl(intvl);           remove_emp(e);         }       }     }     return 0;   }   