http://codeforces.com/contest/1000/problem/C
题意:n条线,数轴上,求被1,2,3,,,,n条线覆盖的点的个数
思路:类似于扫描线,不过是一维的,对于n个点的2n个端点储存,并由1,-1记录是起始点,还是线的终点,表示后面加一条线段,还是减少一条直线。再就是要注意当端点是同一个点时,排序起点优先与终点,因为,各条线都覆盖了这个点。最后一定注意long long!!!
代码:
#include<bits/stdc++.h>
using namespace std;
long long n,m,num[200005],p;
struct AA
{
long long x,ok;
bool operator<(const AA &aa)const{
if(x==aa.x) return ok>aa.ok;
return x<aa.x;
}
}pos[400005];
long long a,b,k;
int main()
{
scanf("%lld",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&a,&b);
pos[++k].ok=1;
pos[k].x=a;
pos[++k].ok=-1;
pos[k].x=b;
}
sort(pos+1,pos+k+1);
pos[0].x=0;
long long fre=0;
p=0;
for(int i=1;i<=k;i++)
{
if(pos[i].ok==1)
{
if(pos[i].x>fre)
num[p]+=pos[i].x-fre;
fre=pos[i].x;
}
else {
if(pos[i].x-fre+1>0)
num[p]+=pos[i].x-fre+1;
fre=pos[i].x+1;}
//cout<<i<<" "<<pos[i].x<<" "<<pos[i].ok<<" "<<p<<" "<<num[p]<<" "<<endl;
p+=pos[i].ok;
}
for(int i=1;i<n;i++)
{
printf("%lld ",num[i]);
}
printf("%lld\n",num[n]);
return 0;
}