XXFSDDX HDU6119 区间合并模板(交叉区间去重)

原来要这样写啊(

#include<bits/stdc++.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<iostream>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
using namespace std;
#define ll long long
#define pb push_back
#define FOR(a) for(int i=1;i<=a;i++)
const int inf=0x3f3f3f3f;
const int maxn=1e5+9; 
const int mod=1e9+7;

struct node{
	int l,r;
}arr[maxn];
node brr[maxn];

int cmp(node a,node b){
	if(a.l!=b.l)return a.l<b.l;
	return a.r<b.r;
}

int main(){
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		FOR(n){scanf("%d%d",&arr[i].l,&arr[i].r);}
		sort(arr+1,arr+1+n,cmp);
		int cnt=1;
		brr[1]=arr[1];
		for(int i=2;i<=n;i++){
			if(arr[i].l<=brr[cnt].r)brr[cnt].r=max(arr[i].r,brr[cnt].r);
			else brr[++cnt]=arr[i];
		}
		int l=1,r=1,cur=0;
		int ans=0;
		for(r=1;r<=cnt;r++){
			if(r!=1){
				cur+=brr[r].l-brr[r-1].r-1;
				while(cur>m){
					cur-=brr[l+1].l-brr[l].r-1;
					l++;
				}
			}
			ans=max(ans,brr[r].r-brr[l].l+1+m-cur);
		}
		printf("%d\n",ans);
	}
}


posted @ 2017-08-13 20:43  Drenight  阅读(223)  评论(0编辑  收藏  举报