【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)

离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了。

因为更新[3,6]时,它只更新到[3,3],[6,6]。

要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段树节点表示到x[l]到x[r+1]的区间。

这样tree[l,r]=tree[l,m]+tree[m+1,r]=[x[l],x[m+1]]+[x[m+1],x[r+1]]

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define ll long long
#define N 2005
using namespace std;
struct P{ll s,e,h,f;}p[N];
struct Tree{ll sum,c;}t[N<<5];
int x[N],n,m,ans;
int cmp(const P &a,const P &b){
    return a.h<b.h;
}
void init(){
    n=m=ans=0;
    memset(p,0,sizeof p);
    memset(t,0,sizeof t);
    x[0]=-1;
}
void pushUp(ll rt,ll l,ll r){
    if(t[rt].c)t[rt].sum=x[r+1]-x[l];
    else if(l==r)t[rt].sum=0;
    else t[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;
}
void update(ll s,ll e,ll rt,ll l,ll r,ll v){
    if(s<=l&&r<=e) t[rt].c+=v;
    else {
        if(l>e||r<s)return;
        ll m=l+r>>1;
        update(s,e,rt<<1,l,m,v);
        update(s,e,rt<<1|1,m+1,r,v);
    }
    pushUp(rt,l,r);
}
int main()
{
    int a,b,c,d;
    init();
    while(cin>>a>>b>>c>>d){
            if(a<0)break;
        while(a>=0){
            p[++n].s=a,p[n].e=c,p[n].h=b,p[n].f=1;x[n]=a;
            p[++n].s=a,p[n].e=c,p[n].h=d,p[n].f=-1;x[n]=c;
            cin>>a>>b>>c>>d;
        }
        sort(x+1,x+1+n);
        for(int i=1;i<=n;i++)
            if(x[i]!=x[i-1])x[++m]=x[i];

        sort(p+1,p+1+n,cmp);
        for(int i=1;i<n;i++){
            int l=lower_bound(x,x+m,p[i].s)-x;
            int r=lower_bound(x,x+m,p[i].e)-x-1;

            update(l,r,1,1,m,p[i].f);
            ans+=t[1].sum*(p[i+1].h-p[i].h);
        }
        cout<<ans<<endl;
        init();
    }
    return 0;
}

  

时间: 2024-08-19 06:53:40

【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)的相关文章

POJ 1389 Area of Simple Polygons(面积合并,线段树+离散化)

Area of Simple Polygons Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3257   Accepted: 1678 Description There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segment

POJ 1389 Area of Simple Polygons 扫描线+线段树面积并

---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 同1151.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <stdio.h> #include

POJ 1389 Area of Simple Polygons

Area of Simple Polygons Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 138964-bit integer IO format: %lld      Java class name: Main There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sid

POJ 1389 Area of Simple Polygons(扫描线)

Area of Simple Polygons Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3278   Accepted: 1694 Description There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segment

HDU 3265 Posters(线段树扫描线&#183;矩形框面积并)

题意  把一些矩形海报挖去一部分小矩形贴在指定位置  问最后海报覆盖的面积 一个矩形框可以分割成4个独立的小矩形  然后就能用扫描线求面积并了 #include <cstdio> #include <algorithm> using namespace std; const int N = 100005, M = N << 2; typedef long long ll; struct SLine { int x, y1, y2, flag; SLine() {}; S

poj 3277 City Horizon (线段树 扫描线 矩形面积并)

题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不会出错, 所有的区间都能列出来,只是在查找的时候费点事. 给的矩形相当于在同一水平线上的,也就是y1坐标相当于为0,其他的就和 poj 1151 Atlantis 差不多了. 我写的思路是按照矩形面积并的思路写的: 但是还有另一种方法也是挺简单的,就是把给的矩形按照高从小到大排序,然后依次插入线段树

HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

Problem A : Counting Squares From:HDU, 1264 Problem Description Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in t

hdu 1255 覆盖的面积 线段树扫描线求重叠面积

稀里糊涂打完了没想到1A了,心情还是很舒畅的,c和c++的四舍五入还是四舍六入遇积进位遇偶舍,我感觉很混乱啊,这道题我输出的答案是7.62,也就是遇偶舍了,可是我就手动处理一下进位问题,发现0.005 系统自动进位0.01了,尼玛啊,我一下子就混乱了,不是遇偶舍么,0也是偶数啊,怎么就进位了呢.最后我就不手动处理进位问题了,直接0.2lf%,虽然我输出的结果是7.62,但是提交也过了 这道题和poj1151,hdu1542差不多,扫描线详细讲解http://blog.csdn.net/young

POJ 2482 Stars in Your Window 线段树扫描线

Stars in Your Window Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remember, vividly, on the beautiful Zhuhai Campus, 4 years ago, from the moment I saw you smile, as you were walkin

HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11551    Accepted Submission(s): 4906 Problem Description There are several ancient Greek texts that contain descriptions of the fabled