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;
}