码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷 3740 [HAOI2014]贴海报

时间:2018-03-11 20:59:37      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:class   names   etc   n+1   blog   register   algorithm   问题   define   

技术分享图片

【题解】

  线段覆盖问题。线段树或者并查集都可以。不离散化居然能过?

  

#include<cstdio>
#include<algorithm>
#define N 10000010
#define rg register
using namespace std;
int n,m,ans,l[N],r[N],fa[N];
inline int read(){
	int k=0,f=1; char c=getchar();
	while(c<‘0‘||c>‘9‘)c==‘-‘&&(f=-1),c=getchar();
	while(‘0‘<=c&&c<=‘9‘)k=k*10+c-‘0‘,c=getchar();
	return k*f;
}
inline int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
	n=read(); m=read();
	for(rg int i=1;i<=m;i++){
		l[i]=read(); r[i]=read();
	}
	for(rg int i=1;i<=n+1;i++) fa[i]=i;
	for(rg int i=m;i;i--){
		int p=find(l[i]); bool fg=0;
		while(p<=r[i]) fa[p]=find(p+1),p=fa[p],fg=1;
		ans+=fg;
	}
	return printf("%d\n",ans),0;
}

  

洛谷 3740 [HAOI2014]贴海报

标签:class   names   etc   n+1   blog   register   algorithm   问题   define   

原文地址:https://www.cnblogs.com/DriverLao/p/8545409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!