HDU1542 rectangular area scan segment tree and

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=1542

The meaning of problems: n-dimensional planar rectangle parallel to the coordinate axis, to calculate the area of ​​the rectangle and

There is talk of a good blog: https://blog.csdn.net/u013480600/article/details/22548393

By segment tree is a leaf node to maintain a bunch of floating-point numbers are not directly maintenance required to maintain discrete.

We should note that the segment tree, each leaf node (control interval [L, L]) does not refer to X [L] coordinates, but rather refers to the interval [X [L], X [L + 1]]. In another segment tree nodes control interval [L, R], also refers to the x-axis intervals of L to a range of R intervals, i.e. X [L] to the X coordinate range [R + 1].

In short, the maintenance of a range.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mid (l+r)/2
using namespace std;
typedef long long ll;
const int maxn=1000+10;
double dat[4*maxn];
int lazy[maxn];
double b[maxn];
struct edge{
    double l,r,h;
    int cnt;
    friend bool operator < (edge x,edge y){
        return x.h<y.h;
    }
}e[maxn];
void pushup(int a,int l,int r){
    if(lazy[a])    dat[a]=b[r+1]-b[l];
    else    dat[a]=dat[a<<1]+dat[a<<1|1];
}
void update(int a,int l,int r,int s,int t,int k){
    if(s>r || t<l)    return;
    if(s<=l && r<=t){
        lazy[a]+=k;
        pushup(a,l,r);
        return;
    }
    if(s<=mid)    update(a<<1,l,mid,s,t,k);
    if(t>mid)    update(a<<1|1,mid+1,r,s,t,k);
    pushup(a,l,r);
}
int main(){
    int n;
    int T=0;
    while(~scanf("%d",&n)){
        memset (which, 0 , sizeof (this));
        memset(lazy,0,sizeof(lazy));
        memset(b,0,sizeof(b));
        memset(e,0,sizeof(e));
        if(n==0)    return 0;
        T++;
        int p=0,num=0;
        for(int i=1;i<=n;i++){
            double x1,x2,y1,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            e[++p].l=x1;
            e[p].r=x2;
            e[p].h=y1;
            and [p] .cnt = 1 ;
            e[++p].l=x1;
            e[p].r=x2;
            e[p].h=y2; 
            and [p] .cnt = - 1 ;
            b[++num]=x1;
            B [ ++ whether] = x2;
        }
        sort (e + 1 and + 1 + p);
        sort(b+1,b+1+num);
        int m=unique(b+1,b+1+num)-b-1;
        double ans=0;
        for(int i=1;i<p;i++){
            int l=lower_bound(b+1,b+m,e[i].l)-b;
            int r=lower_bound(b+1,b+m,e[i].r)-b;
            Update ( . 1 , . 1 , M- . 1 , L, R- . 1 , E [I] .cnt); // remember r-1 is the key section is maintained, a total of m-1 intervals 
            ans + = dat [ . 1 ] * (E [I + . 1 ] .h- E [I] .h);
        }
        printf("Test case #%d\n",T);
        printf("Total explored area: %.2lf\n\n",ans);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qingjiuling/p/11330889.html