欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

luogu3558(最长k可重区间集问题)

最编程 2024-08-01 11:34:28
...
/**
*┏┓  ┏┓
*  ┏┛┗━━━━━━━┛┗━━━┓
*  ┃ ┃  
*  ┃ ━  ┃
*  ┃ > < ┃
*  ┃ ┃
*  ┃... ⌒ ...  ┃
*  ┃ ┃
*  ┗━┓ ┏━┛
*  ┃ ┃ Code is far away from bug with the animal protecting
*  ┃ ┃ 神兽保佑,代码无bug
*  ┃ ┃ 
*  ┃ ┃
*  ┃ ┃
*  ┃ ┃ 
*  ┃ ┗━━━┓
*  ┃ ┣┓
*  ┃ ┏┛
*  ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#include<stdlib.h>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 1005
#define nm 50005
#define pi 3.1415926535897931
const int inf=1e9+7;
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar() ;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}



struct edge{int t,v,w;edge*next,*rev;}e[nm],*h[NM],*o=e,*p[NM];
void _add(int x,int y,int w,int v){o->t=y;o->w=w;o->v=v;o->next=h[x];h[x]=o++;}
void add(int x,int y,int w,int v){_add(x,y,w,v);_add(y,x,0,-v);h[x]->rev=h[y];h[y]->rev=h[x];}
int n,m,d[NM],b[NM],tot,ans,w[NM];
bool v[NM];
queue<int>q;
struct tmp{int l,r,v;}a[NM];





int spfa(){
mem(v);inc(i,0,n)d[i]=inf;mem(w);
v[n+1]++;q.push(n+1);w[n+1]=inf;
while(!q.empty()){
int t=q.front();q.pop();v[t]=false;
link(t)if(j->w&&d[j->t]>d[t]+j->v){
d[j->t]=d[t]+j->v;w[j->t]=min(w[t],j->w);p[j->t]=j;
if(!v[j->t])v[j->t]++,q.push(j->t);
}
}
return w[n];
}


int main(){
n=read();m=read();
inc(i,1,n)b[++tot]=a[i].l=read(),b[++tot]=a[i].r=read(),a[i].v=a[i].r-a[i].l;
sort(b+1,b+1+tot);tot=unique(b+1,b+1+tot)-b-1;
inc(i,1,n)a[i].l=lower_bound(b+1,b+1+tot,a[i].l)-b,a[i].r=lower_bound(b+1,b+1+tot,a[i].r)-b,
add(a[i].l,a[i].r,1,-a[i].v);
inc(i,1,tot)add(i-1,i,m,0);
n=tot;
add(n+1,0,m,0);
while(spfa()){
ans-=d[n]*w[n];
for(int x=n;p[x];x=p[x]->rev->t)p[x]->w-=w[n],p[x]->rev->w+=w[n];
}
return 0*printf("%d\n",ans);
}