codeforces1000c csdn-博客

codeforces1000c csdn-博客

You are given nn segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.

Your task is the following: for every k∈[1…n]k∈[1…n], calculate the number of points with integer coordinates such that the number of segments that cover these points equals kk. A segment with endpoints lili and riri covers point xx if and only if li≤x≤rili≤x≤ri.

Input
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of segments.

The next nn lines contain segments. The ii-th line contains a pair of integers li,rili,ri (0≤li≤ri≤10180≤li≤ri≤1018) — the endpoints of the ii-th segment.

Output
Print nn space separated integers cnt1,cnt2,…,cntncnt1,cnt2,…,cntn, where cnticnti is equal to the number of points such that the number of segments that cover these points equals to ii.

Examples
Input
3
0 3
1 3
3 8
Output
6 2 1
Input
3
1 3
2 4
5 7
Output
5 2 0
Note
The picture describing the first example:

这里写图片描述

Points with coordinates [0,4,5,6,7,8][0,4,5,6,7,8] are covered by one segment, points [1,2][1,2] are covered by two segments and point [3][3] is covered by three segments.

The picture describing the second example:

这里写图片描述

Points [1,4,5,6,7][1,4,5,6,7] are covered by one segment, points [2,3][2,3] are covered by two segments and there are no points covered by three segments.

  • 题意:给定一个区间线段,求出这个那些点被i个线段覆盖的数量,按照顺序输出

  • 解题思路:这个题我写了两种方法。

  • 第一种法 :用的是每个区间比较,把右端点查到一个优先队列中,就用这个练代码。

  • 第二种法 :还可以直接对每个点进行标记个正负1,然后排序后依次,从左到右依次添加。

//第一种方法:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#define debug puts("-----")
#define pi (acos(-1.0))
#define pb push_back
typedef long long ll;
using namespace std;
const int inf = 2e5+100;
struct node
{
	ll l,r;
	node(ll l,ll r):l(l),r(r){}
	bool operator<(const node & a)const
	{
		if(l!=a.l)return l<a.l;
		return r<a.r;
	}
};
vector<node>p;
ll ans[inf];
int n;
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		ll a,b;cin>>a>>b;
		p.pb(node(a,b) );
	}
	sort(p.begin(),p.end());
	priority_queue<ll,vector<ll>,greater<ll> > q;
	ll l=p[0].l;q.push(p[0].r);
	for(int i=1;i<n;i++)
	{
		ll m=p[i].l;
		while(!q.empty()&&q.top()<m){
			ans[q.size()]+=q.top()-l+1;
			l=q.top()+1;
			q.pop();
		}
		if(q.empty())
		{
			l=m;q.push(p[i].r);
		}else
		{
			ans[q.size()]+=m-l;
			l=m;
			q.push(p[i].r);
		}
	}
	while(!q.empty())
	{
		ans[q.size()]+=q.top()-l+1;
		l=q.top()+1;
		q.pop();
	}
	for(int i=1;i<=n;i++)
		cout<<ans[i]<<(i==n?'\n':' ');
	return 0;
}
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#define debug puts("-----")
#define pi (acos(-1.0))
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
const int inf = 4e5+100;
pair<ll,int> p[inf];
int n,m;
ll ans[inf];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		ll a, b;cin>>a>>b;
		p[m++]=mp(a,1);
		p[m++]=mp(b+1,-1);
	}
	sort(p,p+m);
	int d=0;
	for(int i=0;i<m-1;i++)
	{
		d+=p[i].second;
		if(p[i].first!=p[i+1].first)
		ans[d]+=p[i+1].first-p[i].first;
	}
	for(int i=1;i<=n;i++)
	cout<<ans[i]<<" ";
	cout<<endl;	
	return 0;
}

ps:从中可以看出第二种方法明显更简单,所以写的时候可以用第二种。不过第一中可以练习一下代码能力。
有喜欢的可以给个订阅,我也会好好努力的。O(∩_∩)O~

posted @ 2018-07-02 09:53 i-Curve 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值