CF1000C Covered Points Count (差分)

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.

题解:给你n个线段求覆盖点的个数。

对于 [L,R] 可以拆成 <L,+1> <R+1,-1> 将这些点排序 模拟即可。now表示现在的点数。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=220000;
struct node{ll p;int k; }a[MAXN<<1] ;
ll ans[MAXN];
int n,len;
bool cmp(node i,node j){return i.p==j.p? i.k<j.k : i.p<j.p ;} 
int main(){
//	freopen("C.in","r",stdin);
	scanf("%d",&n);len=0;
	for(int i=1;i<=n;i++){
		ll x,y;scanf("%lld%lld",&x,&y);
		a[++len].p=x;a[len].k=1;
		a[++len].p=y+1;a[len].k=-1; 
	}
	sort(a+1,a+len+1,cmp);
	int now=0;ll last=0;
	for(int i=1;i<=len;i++){
		ll x=a[i].p;		
		ans[now]+=x-last;now+=a[i].k;last=a[i].p;
	}
	for(int i=1;i<n;i++) printf("%lld ",ans[i]);printf("%lld\n",ans[n]);
	
	return 0;
}

 

 

转载于:https://www.cnblogs.com/Exception2017/p/10252104.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值