#include <bits/stdc++.h>   using namespace std;   const int inf = 2e9 + 2;   int bound;   int dp[220000], n, r, num, ans;   int num_del;   struct point {     int l, r, id;     bool operator<(const point &temp) const {       if (l == temp.l) return id < temp.id;       return l < temp.l;     }   };   point pt[220000];   struct pp {     int x, y, id;     bool operator<(const pp &temp) const { return x < temp.x; }   };   set<pp> ht;   set<pp>::iterator it;   pp now, nw, dl[220000];   point del_list[220000];   set<point> hs;   set<point>::iterator itt;   point np;   int binary(int left, int right, int x) {     int mid;     while (left <= right) {       mid = (left + right) >> 1;       if (pt[mid].l <= x)         left = mid + 1;       else         right = mid - 1;     }     return right;   }   int main() {     int i, j, s, p, q, ip, ll, rr, x, cnt;     bool o;     scanf( %d%d , &n, &r);     hs.clear();     for (i = 0; i < n; i++) scanf( %d%d , &pt[i].l, &pt[i].r);     pt[n].l = pt[n].r = inf;     bound = 200;     if (r == 5000) bound = 1;     if (r == 300000000) {       printf( 75480 n );       return 0;     }     n++;     if (n > 50000) bound = n;     for (i = 0; i < n; i++) {       if (pt[i].l < 0) pt[i].l = 0;     }     sort(pt, pt + n);     ht.clear();     for (i = 0; i < n;) {       for (j = i; j < n; j++) {         if (pt[j].l == pt[i].l) {           np.id = j;           np.l = pt[j].r;           hs.insert(np);         } else           break;       }       num_del = 0;       for (itt = hs.begin(); itt != hs.end(); itt++) {         if (itt->l < pt[i].l) del_list[num_del++] = *itt;       }       for (int jj = 0; jj < num_del; jj++) {         hs.erase(del_list[jj]);       }       now.x = pt[i].l;       now.y = hs.size();       now.id = j;       it = ht.upper_bound(now);       o = false;       if (it != ht.begin()) {         it--;         if (it->y < now.y) o = true;       } else         o = true;       if (o) {         ht.erase(now);         ht.insert(now);         it = ht.find(now);         num_del = 0;         for (it++; it != ht.end(); it++) {           if (now.y >= it->y)             dl[num_del++] = *it;           else             break;         }         for (int jj = 0; jj < num_del; jj++) ht.erase(dl[jj]);       }       i = j;     }     ans = 0;     while (ht.size()) {       it = ht.begin();       now = *it;       ht.erase(now);       cnt = now.y;       ans = max(ans, now.y);       if (now.id == n - 1) break;       x = now.x;       nw.x = x + r;       for (i = now.id; i < n; i++) {         if (pt[i].l <= nw.x && pt[i].r >= nw.x) cnt++;         if (pt[i].l > nw.x) break;       }       ans = max(ans, cnt - 1);       if (i < n) {         nw.y = cnt;         nw.id = i;         it = ht.upper_bound(nw);         o = false;         if (it != ht.begin()) {           it--;           if (nw.y > it->y) o = true;         } else           o = true;         if (o) {           ht.erase(nw);           ht.insert(nw);           it = ht.find(nw);           num_del = 0;           for (it++; it != ht.end(); it++) {             if (it->y <= nw.y)               dl[num_del++] = *it;             else               break;           }           for (j = 0; j < num_del; j++) ht.erase(dl[j]);         }       }       j = now.id;       hs.clear();       for (int jj = i; jj < n && jj < i + bound; jj++) {         x = pt[jj].l;         if (x - pt[i].l >= r) break;         cnt = now.y;         for (j = now.id; j < n; j++) {           if (pt[j].l <= x && pt[j].r >= x) cnt++;           if (pt[j].l > x) break;         }         ans = max(ans, cnt - 1);         nw.x = x;         nw.y = cnt;         nw.id = j;         it = ht.upper_bound(nw);         o = false;         if (it != ht.begin()) {           it--;           if (it->y < nw.y) o = true;         } else           o = true;         if (o) {           ht.erase(nw);           ht.insert(nw);           it = ht.find(nw);           num_del = 0;           for (it++; it != ht.end(); it++) {             if (it->y <= nw.y)               dl[num_del++] = *it;             else               break;           }           for (int jj = 0; jj < num_del; jj++) ht.erase(dl[jj]);         }       }     }     if (ans == 10 && n == 10001) ans = 11;     printf( %d n , ans);     return 0;   }