poj1151&hdu1542Atlantis(线段树+离散化+线扫描 VS 二维线段树)

->>题目戳这里     戳这里也行->_<-

题目大意:二维平面内给n个矩形,求面积并。

题目分析:线段树线段树~注意给定的坐标点是浮点数,所以要离散化。

这题用线段树2种做法:

1。线段树+离散化+线扫描。

此法相对简单,对x轴坐标离散化建立一颗线段树,对所以矩形按矩形的相对高度排序,先插相对位置比较低的矩形,这样在y轴方向上可以方便的求出y方向上的并,因为排序后y方向上只要2种情况,后插的矩形和前面插的矩形无交集或者有交集,这样只要在y方向上维护一个当前最大高度即可,每次插入一个矩形时将这个要插入的矩形高度范围和当前最大高度比较即可得到位置关系,方便的求出y轴上的并长,最后求面积即可。

详情请见代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10005;
struct node
{
    double sum,curh;
}tree[N<<2];
double hash[N];
struct nd
{
    double x1,x2,y1,y2;
}rect[200];

int cmp(struct nd a,struct nd b)
{
    if(a.y1 != b.y1)
        return a.y1 < b.y1;
    else
    {
        if(a.y2 != b.y2)
            return a.y2 < b.y2;
        else
            return a.x1 < b.x1;
    }
}

void build(int num,int s,int e)
{
    tree[num].sum = tree[num].curh = 0;
    if(s == e)
        return;
    int mid = (s + e)>>1;
    build(num<<1,s,mid);
    build(num<<1|1,mid + 1,e);
}

int gethash(double x,int len)
{
    int l = 1;
    int mid,r = len;
    while(l <= r)
    {
        mid = (l + r)>>1;
        if(hash[mid] == x)
            return mid;
        else
        {
            if(hash[mid] > x)
                r = mid - 1;
            else
                l = mid + 1;
        }
    }
    return -1;
}

void insert(int num,int s,int e,int pos,double y1,double y2)
{
    if(s == e)
    {
        if(y1 > tree[num].curh)
        {
            tree[num].sum = tree[num].sum + (y2 - y1) * (hash[s + 1] - hash[s]);
        }
        else
        {
            tree[num].sum = tree[num].sum + (y2 - tree[num].curh) * (hash[s + 1] - hash[s]);
        }
        tree[num].curh = y2;
        return;
    }
    int mid = (s + e)>>1;
    if(pos <= mid)
        insert(num<<1,s,mid,pos,y1,y2);
    else
        insert(num<<1|1,mid + 1,e,pos,y1,y2);
    tree[num].sum = tree[num<<1].sum + tree[num<<1|1].sum;
}

int main()
{
    int n,cas = 0;
    int i;
    int m;
    while(scanf("%d",&n),n)
    {
        m = 1;
        for(i = 1;i <= n;i ++)
        {
            scanf("%lf%lf%lf%lf",&rect[i].x1,&rect[i].y1,&rect[i].x2,&rect[i].y2);
            hash[m ++] = rect[i].x1;
            hash[m ++] = rect[i].x2;
        }
        sort(hash + 1,hash + m);
        int mm = 2;
        for(i = 2;i < m;i ++)//对x轴坐标离散化
        {
            if(hash[i] != hash[i - 1])
                hash[mm ++] = hash[i];
        }
        mm --;
        build(1,1,mm);
        sort(rect + 1,rect + n + 1,cmp);
        for(i = 1;i <= n;i ++)//插入矩形
        {
            int l = gethash(rect[i].x1,mm);
            int r = gethash(rect[i].x2,mm);
            for(int j = l;j < r;j ++)
                insert(1,1,mm,j,rect[i].y1,rect[i].y2);
        }
        printf("Test case #%d\n",++cas);
        printf("Total explored area: %.2lf\n\n",tree[1].sum);
    }
    return 0;
}
//0MS	324K
//212K	16MS

2。对x轴和y轴同时离散化,建立二维线段树,直接插入每个矩形即可,详情请见代码:

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 205;
double hashx[N];
double hashy[N];
int nx,ny;
struct nd
{
    double x1,x2,y1,y2;
}rect[N];
struct node
{
    int array[N<<2];
    void build(int num,int s,int e)
    {
        /*array[num] = 0;
        if(s == e)
            return;
        int mid = (s + e)>>1;
        build(num<<1,s,mid);
        build(num<<1|1,mid + 1,e);*/
        memset(array,0,sizeof(array));
    }
    void insert(int num,int s,int e,int d,int u)
    {
        if(array[num] == 1)
            return;
        if(s == e)//d && e == u)
        {
            array[num] = 1;
            return;
        }
        int mid = (s + e)>>1;
        if(u <= mid)
            insert(num<<1,s,mid,d,u);
        else
        {
            if(d > mid)
                insert(num<<1|1,mid + 1,e,d,u);
            else
            {
                insert(num<<1,s,mid,d,mid);
                insert(num<<1|1,mid + 1,e,mid + 1,u);
            }
        }
        if(array[num<<1] == 1 && array[num<<1|1] == 1)
            array[num] = 1;
        else
            array[num] = 0;
    }
    double query(int num,int s,int e)
    {
        if(array[num] == 1)
            return hashy[e + 1] - hashy[s];
        if(s == e)//不是到了叶子点直接返回啊啊啊啊
        {
            if(array[num] == 1)
                return hashy[e + 1] - hashy[s];//做标记了才返回,此处曾经跪过。。。
            else
                return 0;
        }
        int mid = (s + e)>>1;
        return query(num<<1,s,mid) + query(num<<1|1,mid + 1,e);
    }
}tree[N<<2];

void build(int num,int s,int e)
{
    if(s == e)
    {
        tree[num].build(1,1,ny);
        return;
    }
    int mid = (s + e)>>1;
    build(num<<1,s,mid);
    build(num<<1|1,mid + 1,e);
}

void insert(int num,int s,int e,int l,int r,int d,int u)
{
    if(s == e)
    {
        tree[num].insert(1,1,ny,d,u);
        return;
    }
    int mid = (s + e)>>1;
    if(r <= mid)
        insert(num<<1,s,mid,l,r,d,u);
    else
    {
        if(l > mid)
            insert(num<<1|1,mid + 1,e,l,r,d,u);
        else
        {
            insert(num<<1,s,mid,l,mid,d,u);
            insert(num<<1|1,mid + 1,e,mid + 1,r,d,u);
        }
    }
}

double query(int num,int s,int e)
{
    if(s == e)
    {
        return tree[num].query(1,1,ny) * (hashx[e + 1] - hashx[e]);
    }
    int mid = (s + e)>>1;
    return query(num<<1,s,mid) + query(num<<1|1,mid + 1,e);
}

int getx(double x)
{
    int l = 1;
    int r = nx;
    int mid;
    while(l <= r)
    {
        mid = (l + r)>>1;
        if(hashx[mid] == x)
            return mid;
        else
        {
            if(hashx[mid] > x)
                r = mid - 1;
            else
                l = mid + 1;
        }
    }
    return -1;
}

int gety(double x)
{
    int l = 1;
    int r = ny;
    int mid;
    while(l <= r)
    {
        mid = (l + r)>>1;
        if(hashy[mid] == x)
            return mid;
        else
        {
            if(hashy[mid] > x)
                r = mid - 1;
            else
                l = mid + 1;
        }
    }
    return -1;
}

int main()
{
    int i,cas = 0;
    int n;
    while(scanf("%d",&n),n)
    {
        int xx = 1;
        int yy = 1;
        for(i = 1;i <= n;i ++)
        {
            scanf("%lf%lf%lf%lf",&rect[i].x1,&rect[i].y1,&rect[i].x2,&rect[i].y2);
            hashx[xx ++] = rect[i].x1;
            hashx[xx ++] = rect[i].x2;
            hashy[yy ++] = rect[i].y1;
            hashy[yy ++] = rect[i].y2;
        }
        sort(hashx + 1,hashx + xx);
        sort(hashy + 1,hashy + yy);
        nx = ny = 2;
        for(i = 2;i < xx;i ++)//离散化x轴y轴坐标
        {
            if(hashx[i] != hashx[i - 1])
                hashx[nx ++] = hashx[i];
            if(hashy[i] != hashy[i - 1])
                hashy[ny ++] = hashy[i];
        }
        nx --;
        ny --;
        build(1,1,nx);
        for(i = 1;i <= n;i ++)//插入矩形
        {
            int l = getx(rect[i].x1);
            int r = getx(rect[i].x2) - 1;//这里减1是因为把线段缩成了一个点
            int d = gety(rect[i].y1);
            int u = gety(rect[i].y2) - 1;//同上
            insert(1,1,nx,l,r,d,u);
        }

        printf("Test case #%d\n",++cas);
        printf("Total explored area: %.2lf\n\n",query(1,1,nx));
    }
}
//hdu0MS	1652K
//poj1616K	16MS


同时在hdu和poj上提交了一下对比发现,还是扫描线简单啊,无论是代码量还是时空复杂度。好吧,无聊了才写二维线段树的。。。

今天才发现,第一份代码算是乱搞的,竟然0Ms过,数据太水了,现在贴一份正统的扫描线算法:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10005;
double hash[N + N];
struct node
{
    double x,y1,y2;
    int flag;
}line[N + N];
struct nd
{
    int cover;
    double len;
}tree[N<<4];

int cmp(struct node a,struct node b)
{
    return a.x < b.x;
}

int gety(double x,int len)
{
    int l = 1;
    int r = len;
    int mid;
    while(l <= r)
    {
        mid = (l + r)>>1;
        if(hash[mid] == x)
            return mid;
        else
        {
            if(hash[mid] > x)
                r = mid - 1;
            else
                l = mid + 1;
        }
    }
    return -1;
}

void build(int num,int s,int e)
{
    tree[num].cover = tree[num].len = 0;
    int mid = (s + e)>>1;
    if(s == e)
        return;
    build(num<<1,s,mid);
    build(num<<1|1,mid + 1,e);
}

void insert(int num,int s,int e,int l,int r,int add)
{
    if(s == e)
    {
        tree[num].cover += add;
        if(tree[num].cover)
            tree[num].len = hash[e + 1] - hash[e];
        else
            tree[num].len = 0;
        return;
    }
    int mid = (s + e)>>1;
    if(r <= mid)
        insert(num<<1,s,mid,l,r,add);
    else
    {
        if(l > mid)
            insert(num<<1|1,mid + 1,e,l,r,add);
        else
        {
            insert(num<<1,s,mid,l,mid,add);
            insert(num<<1|1,mid + 1,e,mid + 1,r,add);
        }
    }
    tree[num].len = (tree[num<<1].len + tree[num<<1|1].len);
}

int main()
{
    double x1,x2,y1,y2;
    int n,i;
    int cas = 0;
    while(scanf("%d",&n),n)
    {
        for(i = 1;i < n + n;i += 2)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[i].x = x1;
            line[i].y1 = y1;
            line[i].y2 = y2;
            line[i].flag = 1;
            line[i + 1].x = x2;
            line[i + 1].y1 = y1;
            line[i + 1].y2 = y2;
            line[i + 1].flag = -1;
            hash[i] = y1;
            hash[i + 1] = y2;
        }
        int m = 2;
        sort(hash + 1,hash + n + n + 1);
        for(i = 2;i <= n + n;i ++)
            if(hash[i] != hash[i - 1])
                hash[m ++] = hash[i];
        m --;
        build(1,1,m);
        sort(line + 1,line + n + n + 1,cmp);
        int l,r;
        double ans = 0;
        l = gety(line[1].y1,m);
        r = gety(line[1].y2,m) - 1;
        insert(1,1,m,l,r,line[1].flag);
        for(i = 2;i <= n + n;i ++)
        {
            ans += (line[i].x - line[i - 1].x) * tree[1].len;
            l = gety(line[i].y1,m);
            r = gety(line[i].y2,m) - 1;
            insert(1,1,m,l,r,line[i].flag);
        }
        printf("Test case #%d\n",++cas);
        printf("Total explored area: %.2lf\n\n",ans);
    }
    return 0;
}//0MS 340K
PS:这里更新的时候好像不能成段更新,因为矩形是边插边删的。

你可能感兴趣的:(线段树)