(前缀和)【CF - 1000C】C. Covered Points Count

题目链接:http://codeforces.com/contest/1000/problem/C

题意:n条线段。依次输出被0..n条线段覆盖的点的个数。

将点记录并排序,从0点开始模拟,cnt记录当前有几条线段覆盖,碰到起点,说明又多了一条线段覆盖之后的点,所以cnt+1.碰到终点,说明有一条线段结束了,cnt-1.


/*
* @Author: Samson
* @Date:   2018-07-01 09:47:34
* @Last Modified by:   Samson
* @Last Modified time: 2018-07-01 10:42:32
*/
//   @URL : http://codeforces.com/contest/1000/problem/C
#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 5e5+10;

pair<LL,LL> p[MAXN];
LL ans[MAXN];

int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	int n,num = 0,cnt = 0;
	LL l,r;
	cin>>n;
	for(int i = 1; i <= n; ++i)
	{
		cin>>l>>r;
		p[++num].first = l,	p[num].second = 1; //线段起点,重叠部分+1
		p[++num].first = r+1,	p[num].second = -1; //线段终点,重叠部分-1
	}  
	sort(p+1,p+1+num);
	for(int i = 1; i <= num; ++i)
	{ 
		ans[cnt] += p[i].first-p[i-1].first;
		cnt += p[i].second;
	}
	for(int i = 1; i <= n; ++i)
	{
		cout<<ans[i]<< (i==n?'\n':' ');
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/suiguia/article/details/80872545
今日推荐