码迷,mamicode.com
首页 > 其他好文 > 详细

西安段素扫描线

时间:2018-06-20 19:01:58      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:space   struct   name   IV   ase   can   min   hang   amp   

先放代码

晚上在注释(我要去van了)

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int manx=5010;
struct Edge
{
    int x1,x2;
    int y;
    int f;
    void change()
    {
        if(x1>x2)
            swap(x1,x2);
        return ;
    }
};
struct node
{
    int f;
    int ha;
    int len;
    int fl,fr;
    int l,r;
};
int te,tx,k;
Edge line[manx<<1];
int base[manx<<1];
int datx[manx<<1];
node t[manx<<5];
void add(int a,int b,int c,int d)
{
    if(b>d) swap(b,d);
    /*line[++te].x1=a,line[te].x2=c;
    line[te].y=b;line[te].f=1;
    line[te].change();
    line[++te].x1=a,line[te].x2=c;
    line[te].y=d;line[te].f=-1;
    line[te].change();*/
    int x1=min(a,c),x2=max(a,c);
    line[++te].x1=x1;
    line[te].x2=x2;
    line[te].y=b;
    line[te].f=1;
    line[++te].x1=x1;
    line[te].x2=x2;
    line[te].y=d;
    line[te].f=-1;
}
bool compare(const Edge &a,const Edge &b)
{
    return a.y==b.y ? a.f > b.f : a.y < b.y;
}
void make_base()
{
    int now=0x7fffffff;
    for(int i=1;i<=tx;i++)
        if(now!=datx[i])
        {
            now=datx[i];
            base[++k]=now;
        }
    return ;
}
void build(int root,int l,int r)
{
    t[root].f=0;t[root].len=0;
    t[root].fl=t[root].fr=0;
    t[root].l=base[l];t[root].r=base[r+1];
    if(l==r)    return ;
    int mid=(l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    return ;
}
int check(int val)
{
    int l=1,r=k;
    int mid;
    while(l<r)
    {
        mid=(l+r)>>1;
        if(base[mid]<val)   l=mid+1;
        else    r=mid;
    }
    return l;
}
void push_up(int root,int l,int r)
{
    if(t[root].ha)
    {
        t[root].len=t[root].r-t[root].l;
        t[root].fl=t[root].fr=1;
        t[root].f=1;
        return ;
    }
    else
    if(l==r)
    {
        t[root].fl=t[root].fr=0;
        t[root].len=0;
        t[root].ha=0;
        t[root].f=0;
        return ;
    }
    else
    {
        t[root].len=t[root<<1].len+t[root<<1|1].len;
        t[root].fl=t[root<<1].fl;
        t[root].fr=t[root<<1|1].fr;
        t[root].f=t[root<<1].f+t[root<<1|1].f-(t[root<<1].fr&t[root<<1|1].fl);
        return ;
    }
}
void updata(int root,int l,int r,int al,int ar,int k)
{
    if(l>ar||r<al)  return ;
    if(l>=al&&r<=ar)
    {
        t[root].ha+=k;
        push_up(root,l,r);
        return ;
    }
    int mid=(l+r)>>1;
    updata(root<<1,l,mid,al,ar,k);
    updata(root<<1|1,mid+1,r,al,ar,k);
    push_up(root,l,r);
}
int main()
{
    //freopen("testdata.in","r",stdin);
    int n;
    scanf("%d",&n);
    int a,b,c,d;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        add(a,b,c,d);
        datx[++tx]=a,datx[++tx]=c;
    }
    sort(line+1,line+1+te,compare);
    sort(datx+1,datx+1+tx);
    make_base();
    build(1,1,k-1);
    int ans=0; 
    int last=0;
    for(int i=1;i<=te;i++)
    {
        int pos1=check(line[i].x1),pos2=check(line[i].x2);
        updata(1,1,k-1,pos1,pos2-1,line[i].f);
        int pas=t[1].len;
        ans+=t[1].f*2*(line[i+1].y-line[i].y);
        ans+=abs(last-pas);
        last=pas; 
    }
    printf("%d",ans);
}

西安段素扫描线

标签:space   struct   name   IV   ase   can   min   hang   amp   

原文地址:https://www.cnblogs.com/Lance1ot/p/9204592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!