夏夜、

心若平似镜、何题不AC。

HDU 1542 Atlantis 线段树 扫描线

开始学扫描线http://www.2cto.com/kf/201307/231714.html 终于找到一个有图的了,智商有限没图看不懂。

自己的代码:

a.因为每个矩形的左右两条边是一一对应的,所以不必下放标记。

b.这里线段树的一个结点并非是线段的一个端点,而是该端点和下一个端点间的线段,所以题目中r+1,r-1的地方可以自己好好的琢磨一下

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb(a) push_back(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("d:\\in.txt","r",stdin);
   // freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}
const int maxn=110;
struct Seg
{
    double a,b,x;
    int c;
    Seg(){}
    Seg(double a,double b,double c,int d):a(a),b(b),x(c),c(d){}
    bool operator < (const Seg& an) const
    {
        return x<an.x;
    }
}seg[maxn<<1];
double sum[maxn<<3];
double Y[maxn<<1];
int flag[maxn<<3];
int build(int idx,int l,int r)
{
    memset(sum,0,sizeof(sum));
    memset(flag,0,sizeof(flag));
    return 0;
}
int PushUp(int idx,int l,int r)
{
    if(flag[idx])
        sum[idx]=Y[r+1]-Y[l];
    else if(l!=r)
        sum[idx]=sum[idx<<1]+sum[idx<<1|1];
    else sum[idx]=0;
    return 0;
}
int update(int idx,int l,int r,int tl,int tr,int v)
{
    if(tl<=l&&tr>=r)
    {
        flag[idx]+=v;
        PushUp(idx,l,r);
        return 0;
    }
 //   PushDown(idx,l,r);
    int mid=(r+l)>>1;
    if(tl<=mid)update(lson,tl,tr,v);
    if(tr>mid)update(rson,tl,tr,v);
    PushUp(idx,l,r);
    return 0;
}
int main()
{
    int n;
    int ca=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        int m=0;
        for(int i=0;i<n;i++)
        {
            double a,b,c,d;
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            seg[m]=Seg(b,d,a,1);
            Y[m++]=b;
            seg[m]=Seg(b,d,c,-1);
            Y[m++]=d;
        }
        sort(Y,Y+m);
        sort(seg,seg+m);
        int k=1;
        for(int i=1;i<m;i++)
            if(Y[i]!=Y[i-1])Y[k++]=Y[i];
        double res=0;
        build(1,0,k);
        for(int i=0;i<m;i++)
        {
            if(i>0)res+=sum[1]*(seg[i].x-seg[i-1].x);
            int l=lower_bound(Y,Y+k,seg[i].a)-Y;
            int r=lower_bound(Y,Y+k,seg[i].b)-Y;
            update(1,0,k,l,r-1,seg[i].c);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",++ca,res);
    }
    return 0;
}
View Code

posted on 2013-09-11 14:59  BMan、  阅读(235)  评论(0编辑  收藏  举报

导航