[CodeForces1000]C. Covered Points Count
区间前缀和的经典问题,离散化后求前缀和(值即为覆盖的条数),用离散化之前的值直接计算即可。
注意:
- 离散化后的值有
个,2 𝑛 sum
数组和b
数组要开n
的两倍 - 只需要对
L
和R+1
进行离散化就可以了,但记住取lower_bound
的时候也要取R+1
的排名,所以算前缀和的时候是--sum[y]
而不是--sum[y+1]
- 相邻两个端点的值不相同,在最后计算
ans
的时候只能算一边!
C. Covered Points Count
You are given
Your task is the following: for every
Input
The first line of the input contains one integer
The next
Output
Print
Examples
input
30 31 33 8
output
6 2 1
input
31 32 45 7
output
5 2 0
Note
The picture describing the first example:
Points with coordinates
The picture describing the second example:
Points
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct Node{ long long L,R;}a[200005];int sum[400005];long long b[400005],ans[200005];int main(void){ int i,n,x,y,num=0; scanf("%d",&n); for(i=1;i<=n;++i) { scanf("%lld%lld",&a[i].L,&a[i].R); b[++num]=a[i].L; b[++num]=a[i].R+1; } sort(b+1,b+num+1); num=unique(b+1,b+num+1)-(b+1); for(i=1;i<=n;++i) { x=lower_bound(b+1,b+num+1,a[i].L)-b; y=lower_bound(b+1,b+num+1,a[i].R+1)-b; ++sum[x];--sum[y]; } for(i=1;i<=num;++i)sum[i]+=sum[i-1]; for(i=1;i<num;++i) { ans[sum[i]]+=b[i+1]-b[i]; } for(i=1;i<=n;++i)printf("%lld ",ans[i]); return 0;}