codeforces 612 D. The Union of k-Segments(类扫描线)

http://codeforces.com/contest/612/problem/D
题意:给出n条线段,问每个点被覆盖大于k次的最少区间

思路:把所有点做标记放入数组后排序,使用一个cnt变量表示该点被多少线段覆盖,遇到一个起点就cnt++,终点就cnt–,如果遇到起点cnt变成了k,说明他是所求线段的一个起点,把他扔进答案,如果遇到了一个终点,原来是k,说明他要结束了,把他放进答案

#include<bits/stdc++.h>
#define fi first
#define se second
#define log2(a) log(n)/log(2)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
#define tim printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
using namespace std;
 
typedef long long ll;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<P, ll> LP;
const ll inf = 1e18;
const int N = 2e6 + 10;
const ll mod = 1e9+7;
const int base = 131;
const double pi = acos ( -1 );
const double eps = 1e-8;
inline ll mul(ll x,ll y) { return (x*y-(ll)((long double)x*y/mod)*mod+mod)%mod;}
inline ll ksm(ll a,ll b) {ll ans=1;while(b){if(b&1)ans=mul(ans,a);a=mul(a,a),b>>=1;}return ans;}
 
#define a(i,j) a[(i-1)*m+(j)]
#define b(i,j) b[(i-1)*m+(j)]
 
unordered_map<ll, ll> mp;
 
 
ll n, m,x,y,z,cx,k,a[N],b[N],c[N];
ll ans,t,flag;
ll num[N],vis[N];
vector<ll> v;
ll sum,cnt;
ll res[N];
P p[N];
 
int main()
{
 
 
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
 
	cin>>n>>k;
	for(int i=1;i<=2*n;i+=2)
	{
		cin>>p[i].fi;
		p[i].se=-1;
		cin>>p[i+1].fi;
		p[i+1].se=1;
	}
	sort(p+1,p+2*n+1);
 
	for(int i=1;i<=2*n;i++)
	{
		if(p[i].se==-1)
		{
			cnt++;
			if(cnt==k) num[++t]=p[i].fi;
		}
		else
		{
			if(cnt==k) num[++t]=p[i].fi;
			cnt--;
		}
	}
	cout<<t/2<<endl;
	for(int i=1;i<=t;i+=2)
	{
		cout<<num[i]<<" "<<num[i+1]<<endl;
	}
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值