#include<iostream> #include<stdio.h> #include<string> #include<string.h> #include<algorithm> usingnamespace std; structnode { int l, r; node() {} node(int x, int y) { l = x; r = y; } booloperator <(const node x)const { if (l == x.l) return r < x.r; return l < x.l; } }a[100000 + 5]; intmain() { int n, m; while (scanf("%d%d", &n, &m) != EOF) { int i; for (i = 0; i < n; i++) { scanf("%d%d", &a[i].l, &a[i].r); } sort(a, a + n); int sum = 0; int bu = m; int ans = 0; int j = 0; int le, ri; le = a[0].l; ri = a[0].r; sum = ri - le + 1; ans = sum + m; for (i = 1; i < n; i++) { if (a[i].l <= ri)//挨着的 { ri = max(ri, a[i].r); a[i].r = ri; sum = ri - le + 1; } else { if (a[i].l - ri - 1 > m)//相隔甚远 { le = a[i].l; ri = a[i].r; sum = ri - le + 1; j = i; bu = m; } else//可以接上 { while (j<i&&a[i].l - ri - 1>bu) { le = a[j + 1].l; bu += max(le - a[j].r - 1, 0); j++; } bu -= a[i].l - ri - 1; ri = a[i].r; sum = ri - le + 1; } } ans = max(ans, sum + bu); } cout << ans << endl; } }