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 #define ll long long
 7 #define out(a) printf("%lld ",a)
 8 using namespace std;
 9 int n,tot;
10 ll ans[200050];
11 ll l,r,now=0;
12 ll read()
13 {
14     ll s=0,t=1; char c;
15     while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();}
16     while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();}
17     return s*t;
18 } 
19 struct dist
20 {
21     ll num,h;
22 }a[400050];
23 bool cmp(dist a,dist b)
24 {
25     return a.num==b.num?a.h<b.h:a.num<b.num;
26 }
27 int main()
28 {
29     n=read(); now=0;
30     for (int i=1;i<=n;i++) {
31       l=read(),r=read();
32       a[++tot].num=l,a[tot].h=1;
33       a[++tot].num=r+1,a[tot].h=-1;
34     }
35     sort(a+1,a+tot+1,cmp); 
36     for (int i=1;i<=tot;i++) {
37       ans[now]+=a[i].num-a[i-1].num;
38       now+=a[i].h;
39     }
40     for (int i=1;i<=n;i++)
41       out(ans[i]);
42     return 0;
43 }
View Code

猜你喜欢

转载自www.cnblogs.com/Kaleidoscope233/p/9277277.html