#include <bits/stdc++.h>   using namespace std;   struct Node {     int l, r;     Node(int l, int r) : l(l), r(r) {}     bool operator<(const Node &o) const {       return r - l == o.r - o.l ? r > o.r : r - l > o.r - o.l;     }   };   vector<int> event;   int arr[200000];   map<int, int> matching;   map<int, int> cloak_pos;   set<Node> heap;   int query[100000 + 10][3];   int query_pos[100000 + 10];   void modify(int pos, int delta) {     while (pos < event.size()) {       arr[pos] += delta;       pos += pos & -pos;     }   }   int sum(int pos) {     int ret = 0;     while (pos > 0) {       ret += arr[pos];       pos -= pos & -pos;     }     return ret;   }   int main() {     int n, q;     scanf( %d%d , &n, &q);     for (int i = 0; i < q; ++i) {       scanf( %d , &query[i][0]);       if (query[i][0] == 0) {         scanf( %d%d , &query[i][1], &query[i][2]);       }     }     matching[1] = n;     matching[n] = 1;     heap.insert(Node(1, n));     event.push_back(0);     for (int i = 0; i < q; ++i) {       if (query[i][0] > 0) {         int x = query[i][0];         if (cloak_pos.count(x)) {           int y = cloak_pos[x];           int l1, r1, l2, r2;           if (matching.count(y - 1)) {             l1 = matching[y - 1];             r1 = y - 1;             matching.erase(y - 1);             heap.erase(Node(l1, r1));           } else {             l1 = r1 = y;           }           if (matching.count(y + 1)) {             l2 = y + 1;             r2 = matching[y + 1];             matching.erase(y + 1);             heap.erase(Node(l2, r2));           } else {             l2 = r2 = y;           }           matching[l1] = r2;           matching[r2] = l1;           heap.insert(Node(l1, r2));           query_pos[i] = -y;           cloak_pos.erase(x);         } else {           Node cur = *heap.begin();           heap.erase(heap.begin());           int y = (cur.l + cur.r) / 2;           if ((cur.r - cur.l + 1) % 2 == 0) {             ++y;           }           matching.erase(cur.r);           if (cur.r != cur.l) {             matching.erase(cur.l);           }           event.push_back(y);           query_pos[i] = y;           if (y + 1 <= cur.r) {             matching[y + 1] = cur.r;             matching[cur.r] = y + 1;             heap.insert(Node(y + 1, cur.r));           }           if (cur.l <= y - 1) {             matching[y - 1] = cur.l;             matching[cur.l] = y - 1;             heap.insert(Node(cur.l, y - 1));           }           cloak_pos[x] = y;         }       }     }     sort(event.begin(), event.end());     event.erase(unique(event.begin(), event.end()), event.end());     for (int i = 0; i < q; ++i) {       if (query[i][0] > 0) {         if (query_pos[i] < 0) {           int y = -query_pos[i];           int t = lower_bound(event.begin(), event.end(), y) - event.begin();           modify(t, -1);         } else {           int y = query_pos[i];           int t = lower_bound(event.begin(), event.end(), y) - event.begin();           modify(t, +1);         }       } else {         int x =             lower_bound(event.begin(), event.end(), query[i][1]) - event.begin();         int y = upper_bound(event.begin(), event.end(), query[i][2]) -                 event.begin() - 1;         int ans = 0;         if (x <= y) {           ans = sum(y) - sum(x - 1);         }         printf( %d n , ans);       }     }     return 0;   }