传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6119
题意:给你n个人区间 可能重合包含 给你m次机会每次可以填补一个点 求最大连续长度
思路:数据量较大 但是把区间处理后发现 符合双指针的连续性
注意:处理连续重合区间
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL>P;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 100;
P p[maxn];
LL sum[maxn];
LL n, m;
int main()
{
while(scanf("%lld%lld", &n, &m) != EOF)
{
for(int i = 0; i < n; i++)
{
LL l, r;
scanf("%lld%lld", &l, &r), p[i] = P(l, r);
}
sort(p , p + n);
int cnt = 0;
for(int i = 0; i < n; i++)
{
LL l = i, r = i + 1, last = p[i].second;
while(r < n && p[r].first <= last)
{
last = max(last, p[r].second),r++;
}
r--;
p[cnt++] = P(p[l].first, last);
i = r;
}
sum[0]=0;
for(int i=1;i<cnt;i++) sum[i]=sum[i-1]+p[i].first-p[i-1].second-1;
LL ans=m;int r=0;
for(int l=0;r<cnt && l < cnt;l++)
{
while(sum[r]- sum[l] <= m && r < cnt) r++;
LL x=m-(sum[r-1] - sum[l]);
ans=max(ans,p[r-1].second-p[l].first+1+x);
}
printf("%lld\n",ans);
}
return 0;
}