(pku3327) city horizontal

作者在 2008-04-17 23:11:25 发布以下内容

这题的数量过大,我能想到的就只有离散化。

然后我试着从左到右的直接算出来,很不幸超时。

最后在万般无赖的情况下,只好使用线段树来构造。

开始的时候我还想本着优化程序的原则,没有把线段树当作树状数组来使用,但是很不幸的是错了很久我都不知道为什么。

后来发现了以后,直接把每个都下到最底层(本来还是有优化的,但是废去了一个晚上,我也就不想什么有化不优化的了)

 

哎~~~~~~

 

代码写得非常的有问题,没有严格测试过,慎用!!!

 

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{
 long h,l,r;
    __int64 area;
};
typedef struct Seg
{
 __int64 x;
 __int64 y;
 __int64 h;
}seg;
long hash[100000];
class SegTree
{
private:
 node tree[1<<18];
public:
 

// void init()
// {
//  memset(tree,0,sizeof(tree));
// }

 node root() {return tree[1];}

 void buildTree(long l,long r,long index)
 {
  tree[index].l=l;
  tree[index].r=r;
  tree[index].h=0;
  if (l<r-1)
  {
   int mid=(l+r)/2;
   buildTree(l,mid,index*2);
   buildTree(mid,r,index*2+1);
  }
 }


 void insert(long l,long r,long h,long index)
    {
  if(tree[index].l==tree[index].r)
   return ;
  if (tree[index].r-tree[index].l==1)
  {
   if(tree[index].h<h)
   {
    tree[index].area=h*(r-l);
        tree[index].h=h;
   }
  }
  else
  {
   long mid=(tree[index].l+tree[index].r)/2;
   if (l>=hash[mid]) insert(l,r,h,index*2+1);
   else if (r<=hash[mid]) insert(l,r,h,index*2);
   else
   {
    insert(l,hash[mid],h,index*2);
    insert(hash[mid],r,h,index*2+1);
   }
   if(tree[index].area<tree[index*2].area+tree[index*2+1].area)
    tree[index].area=tree[index*2].area+tree[index*2+1].area;
  }
 }
};
bool __inline cmp(Seg p1,Seg p2)
{
 return p1.h>p2.h;
};
SegTree c;
int main()
{
 
 int n;
 cin >>n;
// c.init();
 seg *s=new seg[n+1];
 for(int i=0,j=0;i<n;i++,j+=2)
 {
  scanf("%I64d%I64d%I64d",&s[i].x,&s[i].y,&s[i].h);
  
   hash[j]=s[i].x;
   hash[j+1]=s[i].y;
 }
 sort(hash,hash+2*n);
 int maxn=1;
 for( i=1;i<2*n;i++)
 {
  if(hash[i]!=hash[i-1])
   hash[maxn++]=hash[i];
 }
 //hash[maxn++]=hash[i-1];
 c.buildTree(0,maxn-1,1);
 sort(s,s+n,cmp);
 for(int k=0;k<n;k++)
 c.insert(s[k].x,s[k].y,s[k].h,1);
 printf("%I64d\n",c.root().area);
 return 0;

 

}


 

acm | 阅读 2392 次
文章评论,共0条
游客请输入验证码
浏览255989次