Covered Points Count
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
.
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]
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次……n次的点分别有多少个?
分析:
将左端点和右端点存在数组中,左端点记为标记为1,右端点标记为-1,每当碰到一个左端点说明开始了一个新区间,重复覆盖次数会增加1,而每当碰到一个右端点,说明一个区间结束,区间数减1,重复覆盖数减1
有一点注意的地方,当碰到两个连续的左端点或者两个连续的右端点时,其间的点重复覆盖数相同的点应该是[l,l)或[r,r]即左闭右开区间的数是覆盖次数相同的,因为对于两个连续的左端点,右边的左端点说明开始了一个新的区间那么,这个点的覆盖值一定别前面的多1;同理对于两个连续的右端点,左侧的点覆盖值一定比其他的多1;而对于一左一右,区间内应该是相同的,即[l,r]左闭右闭区间,是包括端点的所有点
这样我们其实只需要将r即右端点标记为r+1即可
code:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 5e5+10;
pair<ll,ll>p[maxn];
ll ans[maxn];
int main(){
int n,num = 0,cnt = 0;
ll l,r;
scanf("%d",&n);
for(int i = 1; i <= n; i++){
scanf("%lld%lld",&l,&r);
p[++num].first = l;
p[num].second = 1;
p[++num].first = r + 1;
p[num].second = -1;
}
sort(p+1,p+1+num);//注意pair默认按照first值从小到大排序
for(int i = 1; i <= num; i++){
ans[cnt] += p[i].first - p[i-1].first;//pair[0]没有赋值默认前后值均为0
cnt += p[i].second;
}
for(int i = 1; i <= n; i++){
printf("%lld%c",ans[i], i == n ? '\n' : ' ');
}
return 0;
}