CodeForces - 1000 C. Covered Points Count

C. Covered Points Count

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

扫描二维码关注公众号,回复: 2807113 查看本文章

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

Copy

3
0 3
1 3
3 8

output

Copy

6 2 1 

input

Copy

3
1 3
2 4
5 7

output

Copy

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.

题解:n个线段,问被这n个线段覆盖k次(k∈[1..n])的点的个数。 

把每个线段的起点终点分别存起来,起点标记为1,终点标记为-1,然后按照起点终点的位置排序。

如样例1:

排序标记后,a点的标记是1,c点的标记也是1,证明他们都是起点,一开始我们设一个计数变量cnt,我们发现点被覆盖的次数就是计数变量原来的值加上当前点的标记,这就是为什么要给起点终点标记为1,-1了。然后到达一个起点或终点的时候,只需要后边的点的编号减去前边的点编号就是我们要的被覆盖的点的个数了。 

但是有个地方就变得很难处理了,我们发现3,4,5这三个点重合了,按照原来的方式求的就不对了。怎么办呢。 
只要把终点往后移一个就ok了。 

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 2e5 + 100;

struct node {
   ll l;
   int is;
   bool operator <(node &a) const  {
      if(l != a.l) return l < a.l;
      return is > a.is;
   }
};

vector<node> data;
ll ans[maxn];

int main()
{
    int n;
    scanf("%d", &n);
    ll s, e;
    for(int i = 0; i < n; i++) {
        scanf("%lld %lld", &s, &e);
        data.push_back((node) {s, 1});
        data.push_back((node) {e + 1, -1});
    }

    sort(data.begin(), data.end());

    int len = data.size();
    int cnt = 1;
    for(int i = 1; i < len; i++) {
        ans[cnt] += data[i].l - data[i - 1].l;
        cnt += data[i].is;
    }

    for(int i = 1; i <= n; i++) {
         printf("%lld%c", ans[i], i == n ? '\n' : ' ');
    }
}

猜你喜欢

转载自blog.csdn.net/henu111/article/details/81450661
今日推荐