题目链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=6119​​​
题意:中文题
解析:首先需要能合并的区间都合并了(按L排序后扫一遍),因为题目有明确说明,接着把每个区间之间的差值处理出来,然后尺取做一遍,维护一下答案即可,要注意,剩下的补签卡,可以直接签在已经维护好的那个区间的后面

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
typedef long long ll;
struct node
{
ll l,r;
bool operator < (const node &b)const
{
if(l==b.l)
return r<b.r;
return l<b.l;
}
}tmp[maxn],a[maxn];
ll d[maxn];
int main(void)
{
ll n,m;
while(~scanf("%I64d %I64d",&n,&m))
{
for(int i=0;i<n;i++)
scanf("%I64d %I64d",&tmp[i].l,&tmp[i].r);
sort(tmp,tmp+n);
int cnt = 1;
a[cnt] = tmp[0];
for(int i=1;i<n;i++)
{
if(tmp[i].l<=a[cnt].r+1)
a[cnt].r = max(tmp[i].r,a[cnt].r);
else
{
cnt++;
a[cnt] = tmp[i];
}
}
for(int i=1;i<cnt;i++)
d[i] = a[i+1].l-a[i].r-1;
int t1 = 0,l = 1;
ll sum = a[1].r-a[1].l+1+m;
for(int i=1;i<cnt;i++)
{
t1 += d[i];
while(t1>m)
{
t1 -= d[l];
l++;
}
sum = max(sum,a[i+1].r-a[l].l+1+m-t1);
}
printf("%I64d\n",sum);
}
return 0;
}