#include <bits/stdc++.h>   using namespace std;   int tree[(1 << 20)];   void add(int x, int val) {     for (int i = x; i < (1 << 20); i = ((i) | (i + 1))) tree[i] += val;   }   int sum(int x) {     int ans = 0;     for (int i = x; i > 0; i = ((i) & (i - 1))) ans += tree[i - 1];     return ans;   }   int N, Q;   map<int, int> idtohook;   map<int, int> appearance;   priority_queue<pair<int, int> > segments;   map<int, int> nextL, nextR;   vector<pair<int, pair<int, int> > > query;   map<int, int> zip;   vector<int> ans;   int input(void) {     int ans;     scanf( %d , &ans);     return ans;   }   void count_query(int low, int high) {     query.push_back(make_pair(0, make_pair(low, high)));     zip[low] = zip[high] = 0;   }   void enter_query(int id) {     int hook = -1;     while (1) {       pair<int, int> p = segments.top();       segments.pop();       int L = p.second;       int R = L + p.first;       if (nextR[L] != R || nextL[R] != L) continue;       hook = (L + R + 1) / 2;       nextL[hook] = L;       nextL[R] = hook;       nextR[L] = hook;       nextR[hook] = R;       segments.push(make_pair(hook - L, L));       segments.push(make_pair(R - hook, hook));       break;     }     query.push_back(make_pair(1, make_pair(hook, 1)));     idtohook[id] = hook;     zip[hook] = 0;   }   void leave_query(int id) {     int hook = idtohook[id];     int L = nextL[hook], R = nextR[hook];     nextL[hook] = nextR[hook] = -1;     nextL[R] = L;     nextR[L] = R;     segments.push(make_pair(R - L, L));     query.push_back(make_pair(1, make_pair(hook, -1)));   }   void solve(void) {     int i;     i = 0;     for (__typeof((zip).begin()) itr = (zip).begin(); itr != (zip).end(); itr++) {       (itr->second) = i;       i++;     }     for ((i) = 0; (i) < (int)(query.size()); (i)++) {       int type = query[i].first;       if (type == 0) {         int low = zip[query[i].second.first];         int high = zip[query[i].second.second];         int tmp = sum(high + 1);         if (low > 0) tmp -= sum(low);         ans.push_back(tmp);       } else {         int pos = zip[query[i].second.first];         add(pos, query[i].second.second);       }     }   }   int main(void) {     int i;     N = input();     Q = input();     segments.push(make_pair(N + 1, 0));     nextR[0] = N + 1;     nextL[N + 1] = 0;     zip[0] = zip[N + 1] = 0;     for ((i) = 0; (i) < (int)(Q); (i)++) {       int x = input();       if (x == 0) {         int low = input(), high = input();         count_query(low, high);       } else {         appearance[x]++;         int tmp = appearance[x];         if (tmp % 2 == 1) {           enter_query(x);         } else {           leave_query(x);         }       }     }     solve();     for ((i) = 0; (i) < (int)(ans.size()); (i)++) printf( %d n , ans[i]);     return 0;   }   