Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17146 | Accepted: 4668 |
Description
Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings.
The entire horizon is represented by a number line with N (1 ≤ N ≤ 40,000) buildings. Building i's silhouette has a base that spans locations Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.
Input
Lines 2.. N+1: Input line i+1 describes building i with three space-separated integers: Ai, Bi, and Hi
Output
Sample Input
4 2 5 1 9 10 4 6 8 2 4 6 3
Sample Output
16
Hint
Source
数组开小wa了,几次,n=4000,离散化8000,线段树最少3倍啊
ac代码
Problem: 3277 User: kxh1995
Memory: 7748K Time: 735MS
Language: C++ Result: Accepted
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct s
{
__int64 x1,x2,y;
int flag;
}a[380080];
__int64 hash[380050],sum[380050];
int col[380050];
int cmp(s a,s b)
{
return a.y<b.y;
}
__int64 bseach(__int64 key,__int64 n)
{
__int64 l=1,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(hash[mid]==key)
return mid;
if(hash[mid]<key)
l=mid+1;
else
r=mid-1;
}
return l;
}
void pushup(__int64 tr,__int64 l,__int64 r)
{
if(col[tr])
sum[tr]=hash[r+1]-hash[l];
else
if(l==r)
sum[tr]=0;
else
sum[tr]=sum[tr<<1]+sum[tr<<1|1];
}
void update(__int64 L,__int64 R,__int64 l,__int64 r,__int64 tr,__int64 flag)
{
if(L<=l&&r<=R)
{
col[tr]+=flag;
pushup(tr,l,r);
return;
}
int mid=(l+r)>>1;
if(L<=mid)
update(L,R,l,mid,tr<<1,flag);
if(R>mid)
update(L,R,mid+1,r,tr<<1|1,flag);
pushup(tr,l,r);
}
int main()
{
__int64 n;
while(scanf("%I64d",&n)!=EOF)
{
memset(col,0,sizeof(col));
memset(sum,0,sizeof(sum));
__int64 x1,x2,y;
__int64 i,k=1;
for(i=1;i<=n;i++)
{
scanf("%I64d%I64d%I64d",&x1,&x2,&y);
a[k].x1=x1;
a[k].x2=x2;
a[k].y=1;
a[k].flag=1;
hash[k++]=x1;
a[k].x1=x1;
a[k].x2=x2;
a[k].y=y+1;
a[k].flag=-1;
hash[k++]=x2;
}
sort(a+1,a+k,cmp);
sort(hash+1,hash+k);
__int64 ans=0;
for(i=1;i<k-1;i++)
{
__int64 x,y;
x=bseach(a[i].x1,k-1);
y=bseach(a[i].x2,k-1)-1;
if(x<=y)
update(x,y,1,k-1,1,a[i].flag);
ans+=sum[1]*(a[i+1].y-a[i].y);
}
printf("%I64d\n",ans);
}
}