CodeForces - 1000C Covered Points Count

版权声明:欢迎加好友讨论~ QQ1336347798 https://blog.csdn.net/henu_xujiu/article/details/81452041

You are given n 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] calculate the number of points with integer coordinates such that the number of segments that cover these points equals k. A segment with endpoints li and ri covers point x if and only if li≤x≤ri

Input

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

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

Output

Print n space separated integers cnt1,cnt2,…,cntn, where cnti is equal to the number of points such that the number of segments that cover these points equals to i

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]

are covered by one segment, points [1,2] are covered by two segments and point [3]

is covered by three segments.

The picture describing the second example:

Points [1,4,5,6,7]

are covered by one segment, points [2,3] are covered by two segments and there are no points covered by three segments.

题目链接

 题意:看了半天才明白什么意思,就是说,给n个区间,每个区间之间的数字等级加一。求各等级的数量。

也就是被1.2.3.4....n个区间覆盖的点的个数。(总感觉怎么说都说不清楚,,,,)

举个例子吧,

input
3
0 3
1 3
3 8

输出 被1.2.3个区间覆盖的点的个数

output

6 2 1

d[1]={0,4,5,6,7,8}=6;

d[2]={1,2}=2;

d[3]={3}=1;

是不是清楚题意什么意思啦~

那怎么解决的呢, 用结构体储存这些起点和重点,起点的vis标记为1,重点标记为-1;

再将这些点进行排序(起点优先)

然后遍历这些点就行了详细解释看代码

还有要注意的就是数据范围ll

#include<stdio.h>
#include<string.h>  
#include<math.h>    
#include<map>   
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  
 
#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
struct node{
	ll x;
	int vis;
	node(ll _x=0,ll _vis=0):x(_x),vis(_vis){}
}d[400100];
ll ans[200100];
bool cmp(node a,node b)
{
	if(a.x==b.x)
		return a.vis > b.vis;//起点优先
	return a.x < b.x; 
}
int main()
{
	int n;
	cin>>n;
	int k=0;
	for(int i=0;i<n;i++)
	{
		ll a,b;
		cin>>a>>b;
		d[k++]=node(a,1);//从k=0开始存 
		//cout<<d[i].x <<endl;
		//cout<<d[i].vis <<endl;
		d[k++]=node(b+1,-1);
	}
	sort(d,d+k,cmp);
	int res=0;
	for(int i=0;i < k-1;i++)
	{
		res=res+d[i].vis;
		if(d[i+1].x!=d[i].x)
		{
			ans[res]=ans[res]+d[i+1].x-d[i].x;//下一个起点与此起点之间的点数 
		}	
		/*例如,3时res=1(+1+1+1-1-1)到下一个终点 8 有 5个(4.5.6.7.8)点,这时ans[res]+=5;*/
	 
		
	}
	for(int i=1;i<=n;i++)
	{
		cout<<ans[i]<<" ";
	}
	cout<<endl;
	return 0;
	 
	
}

关于

node(ll _x=0,ll _vis=0):x(_x),vis(_vis){}
cin>>a>>b;
		d[k++]=node(a,1);
		d[k++]=node(b+1,-1);

 看这里友情链接

猜你喜欢

转载自blog.csdn.net/henu_xujiu/article/details/81452041