Covered Points Count(思维)

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;
}
这个问题是一个经典的区间覆盖问题,可以利用贪心算法来解决。贪心算法的思想是在每一步选择中都采取在当前状态下最优的选择,希望能够导致结果是全局最优的。 算法步骤如下: 1. 将所有点按坐标大小进行排序。 2. 从最小的点开始,每次选择一个长度为1的区间,使得这个区间包含当前最小的未被覆盖的点。 3. 更新覆盖范围并标记当前点为已覆盖。 4. 重复步骤2和3,直到所有点都被覆盖。 算法伪码如下: ``` function CoverIntervals(points): n = length(points) if n == 0: return 0, [] sort points by increasing order count = 1 end_position = points[0] + 1 covered_points = [points[0]] for i from 1 to n-1: if points[i] < end_position: # 如果当前点已经在覆盖范围内,继续寻找下一个点 continue else: # 如果当前点不在覆盖范围内,选择新的区间 count += 1 end_position = points[i] + 1 covered_points.append(end_position - 1) return count, covered_points ``` 证明算法的正确性: 每次选择的区间都是当前未覆盖点集合中最小点的一个最小覆盖区间。由于区间长度固定为1,所以每次选择都能覆盖一个最小未覆盖点。由于点是按照坐标排序的,所以每次选择新的区间时,它都会尽量覆盖更多的未覆盖点,保证不会遗漏任何点,并且用最少的区间覆盖所有点。 时间复杂度分析: - 排序操作的时间复杂度为O(n log n)。 - 遍历点的循环最多执行n次。 - 每次循环内部的操作是常数时间的。 因此,总的算法时间复杂度为O(n log n)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值