题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6119
合并有重叠的段后尺取。
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <bits/stdc++.h> 18 using namespace std; 19 #define fr first 20 #define sc second 21 #define cl clear 22 #define BUG puts("here!!!") 23 #define W(a) while(a--) 24 #define pb(a) push_back(a) 25 #define Rint(a) scanf("%d", &a) 26 #define Rll(a) scanf("%I64d", &a) 27 #define Rs(a) scanf("%s", a) 28 #define Cin(a) cin >> a 29 #define FRead() freopen("in", "r", stdin) 30 #define FWrite() freopen("out", "w", stdout) 31 #define Rep(i, len) for(int i = 0; i < (len); i++) 32 #define For(i, a, len) for(int i = (a); i < (len); i++) 33 #define Cls(a) memset((a), 0, sizeof(a)) 34 #define Clr(a, x) memset((a), (x), sizeof(a)) 35 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 36 #define lrt rt << 1 37 #define rrt rt << 1 | 1 38 #define pi 3.14159265359 39 #define RT return 40 #define lowbit(x) x & (-x) 41 #define onenum(x) __builtin_popcount(x) 42 typedef long long LL; 43 typedef long double LD; 44 typedef unsigned long long ULL; 45 typedef pair<int, int> pii; 46 typedef pair<string, int> psi; 47 typedef pair<LL, LL> pll; 48 typedef map<string, int> msi; 49 typedef vector<int> vi; 50 typedef vector<LL> vl; 51 typedef vector<vl> vvl; 52 typedef vector<bool> vb; 53 54 const int maxn = 100100; 55 int n; 56 LL m; 57 pll seg[maxn]; 58 59 signed main() { 60 // freopen("in", "r", stdin); 61 while(~Rint(n)) { 62 Rll(m); 63 For(i, 1, n+1) { 64 Rll(seg[i].first); 65 Rll(seg[i].second); 66 } 67 sort(seg+1, seg+n+1); 68 int tn = n; 69 n = 0; 70 For(i, 1, tn+1) { 71 if(i != 1 && seg[i].first <= seg[n].second) { 72 seg[n].second = max(seg[n].second, seg[i].second); 73 } 74 else seg[++n] = seg[i]; 75 } 76 int lo = 1; 77 LL cur = 0, ret = 0; 78 For(i, 1, n+1) { 79 if(i != 1) { 80 cur += seg[i].first - seg[i-1].second - 1; 81 } 82 while(i != 1 && cur > m) { 83 cur -= seg[lo+1].first - seg[lo].second - 1; 84 lo++; 85 } 86 ret = max(ret, seg[i].second - seg[lo].first + 1 + m - cur); 87 } 88 printf("%I64d ", ret); 89 } 90 return 0; 91 }