Covered Points Count CF1000C 思维 前缀和 贪心

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.

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

30 31 33 8

output

Copy

6 2 1 

input

Copy

31 32 45 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条线段的开始点和结束点,问被1-n条线段覆盖的点的个数

记录每个点的位置,以及他是开始点还是结束点,放在一个数组里。然后按从小到大排序,用一个cnt记录下现在覆盖了几条线段,接着直接遍历,加上每段点数,遇到开始点cnt+1,遇到结束点cnt-1

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 5e5 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
struct node {
    ll first, second;
};
node a[maxn];
ll ans[maxn];
bool cmp( node p, node q ) {
    return p.first < q.first;
}
int main() {
    ll n;
    while( cin >> n ) {
        memset( ans, 0, sizeof(ans) );
        for( ll i = 1; i <= n; i ++ ) {
            ll l, r;
            cin >> l >> r;
            a[i*2-1].first = l, a[i*2].first = r+1;
            a[i*2-1].second = 1, a[i*2].second = -1;
        }
        ll cnt = 0;
        sort( a + 1, a + 2*n + 1, cmp );
        for( ll i = 1; i <= 2*n; i ++ ) {
            ans[cnt] += a[i].first - a[i-1].first;
            cnt += a[i].second;
        }
        for( ll i = 1; i <= n; i ++ ) {
            if( i != n ) {
                cout << ans[i] << " ";
            } else {
                cout << ans[i] << endl;
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/l609929321/p/9310855.html

时间: 2024-10-16 16:57:38

Covered Points Count CF1000C 思维 前缀和 贪心的相关文章

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 segment

Codeforces 1000C Covered Points Count

C. Covered Points Count题目大意:有n条线段,问有多少个点被i条线段覆盖(i=1~n).很常见的线段覆盖套路题QAQ.坐标排序后把左端点当做+1,右端点当做-1,扫一遍统计答案即可.但是记得开ll,数组大小开双倍. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <queue> 6 #

Educational Codeforces Round 46 C - Covered Points Count

C - Covered Points Count emmm 好像是先离散化一下 注意 R需要+1 这样可以确定端点 emmm 扫描线?瞎搞一下? #include<bits/stdc++.h> using namespace std; #define maxn 4000005 #define LL long long LL a[maxn],b[maxn],ll[maxn],rr[maxn],c[maxn]; LL x[maxn],y[maxn]; vector<LL >q; int

cf1000 C. Covered Points Count

#include<bits/stdc++.h> using namespace std; typedef long long ll; map<ll ,int > mp; ll cnt[200010]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { ll l,r; scanf("%lld%lld",&l,&r); mp[l]++; mp[r+

EDU 50 E. Covered Points 利用克莱姆法则计算线段交点

E. Covered Points 利用克莱姆法则计算线段交点.n^2枚举,最后把个数开方,从ans中减去. ans加上每个线段的定点数, 定点数用gcs(△x , △y)+1计算. #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include

Codeforces 578B Or Game (前缀和 + 贪心)

Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] 题目链接:B. "Or" Game You are given \(n\) numbers \(a_1,?a_2,?...,?a_n\). You can perform at most \(k\) operations. For each operation you can multiply one of the numbers by \(x\). We want to make

Spotlights【思维+前缀和优化】

https://blog.csdn.net/mengxiang000000/article/details/53291883   原博客地址 http://codeforces.com/group/1EzrFFyOc0/contest/738/problem/B  题目链接 题目大意: 给你一个N*M的空间,其中0表示没有人,1表示有人,对应一个好位子以及方向的定义为: ①首先这个位子不能有人. ②其次对应这个位子安排一个照明方向,这个方向上必须有人才行. 让你求一共有多少个这样满足的放置方案.

cf1058E 思维 前缀处理

题目大意:给定一个长度为 (n≤3×105)的数列ai(1≤ai≤1018)交换一个数的任意二进制位,问你可以选出多少 区间经过操作后异或和是 0 链接:http://codeforces.com/contest/1058/problem/E 思路:由于二进制随意交换,那么它本身值不必考虑,只需要保存它有多少二进制为1的个数就好了. 充分必要条件: 区间中二进制1的个数是偶数 区间中二进制位最多的一个数的二进制个数小于等于和的一半 对于条件一  所以前面的为偶数的前提下  , 根据(0,l)的奇

AtCoder Beginner Contest 124 D - Handstand(思维+前缀和)

D - Handstand Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement NN people are arranged in a row from left to right. You are given a string SS of length NN consisting of 0 and 1, and a positive integer KK. The ii-th per