poj1177

参考

http://wenku.baidu.com/view/1856cf84b9d528ea81c77918.html

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<cmath>
#define L(x) (x<<1)
#define R(x) (x<<1 |1)
#define MAX 5010
using namespace std;
int cy[2*MAX];
int m1,m2;
class seg
{
    public:
    int x,y1,y2,f;
    seg(){};
    seg(int a,int b,int c,int d):x(a),y1(b),y2(c),f(d)
    {}
    bool operator< (const seg b)const
    {
        return x<b.x;
    }
}line[MAX*2];
struct node
{
    int l,r,length,cover;
    int num;
    int cr,cl;
}a[2*MAX];
void build(int t,int l,int r)
{
    a[t].l=l;
    a[t].r=r;
    a[t].cover=0;
    a[t].length=0;
    a[t].cr=0;
    a[t].cl=0;
    a[t].num=0;
    if(r-l==1)return ;
    int mid=(l+r)/2;
    build(L(t),l,mid);
    build(R(t),mid,r);
}
int ll=0;
void uplen(int t)
{
    if(a[t].cover>0)
    {
      a[t].length=cy[a[t].r]-cy[a[t].l];
      a[t].num=1;
      a[t].cr=1;
      a[t].cl=1;
    }
    else
      if(a[t].r-a[t].l==1)
      {
          a[t].length=0;
          a[t].cr=0;
          a[t].cl=0;
          a[t].num=0;
      }
      else
      {
          a[t].cl=a[L(t)].cl;
          a[t].cr=a[R(t)].cr;
          a[t].num=a[L(t)].num+a[R(t)].num-a[L(t)].cr*a[R(t)].cl;
          a[t].length=a[L(t)].length+a[R(t)].length;
      }
}
void update(int t ,seg s)
{
    if(cy[a[t].l]>=s.y1&&cy[a[t].r]<=s.y2)
    {
        a[t].cover+=s.f;
        uplen(t);
        return; 
    }
    if(a[t].r-a[t].l==1)return ;
    int mid=(a[t].l+a[t].r)/2;
    if(s.y1<=cy[mid])
      update(L(t),s);
    if(s.y2>=cy[mid])
      update(R(t),s);
    uplen(t);
} 
void addlen(int x1,int x2,int y1,int y2)
{
    
        line[m1++]=seg(x1,y1,y2,1);
        line[m1++]=seg(x2,y1,y2,-1);
        cy[m2++]=y1;
        cy[m2++]=y2;
}
int main()
{
    int x1,x2,y1,y2;
    int l1,l2;
    int n,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        m1=0;
        m2=0;
        ll=0;
        l1=0;
        l2=0;
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            addlen(x1,x2,y1,y2);
        }
        sort(line,line+m1);
        sort(cy,cy+m2);
        m2=unique(cy,cy+m2)-cy;
        build(1,0,m2-1);
        for(i=0;i<m1;i++)
        {
            update(1,line[i]);
            ll+=abs(a[1].length-l1);
            if(i<m1-1)
            {
                ll+=2*a[1].num*(line[i+1].x-line[i].x);
            }
            l1=a[1].length;
        }
        printf("%d\n",ll);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值