zoukankan      html  css  js  c++  java
  • HDU 1828 Picture(线段树:扫描线 面积并)

    题解+扫描线讲解:https://blog.csdn.net/u013480600/article/details/22548393

    #include<bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<queue>
    #include<set>
    #include<map>
    #include<vector>
    
    #define inf 0x3f3f3f3f
    #define mem(a,b) memset(a,b,sizeof(a))
    #define ll long long
    #define sd(x) scanf("%d",&(x))
    #define sl(x) scanf("%lld",&(x))
    #define slf(x) scanf("%lf",&(x))
    #define scs(s) scanf("%s",s)
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    #define per(i,a,b) for(int i=a;i>=b;i--)
    #define lowbit(x) x&(-x)
    #define ls now<<1
    #define rs now<<1|1
    #define lson l,mid,ls
    #define rson mid+1,r,rs
    #define All L,R
    
    using namespace std;
    
    const int maxn=2222;
    
    struct node{
        double l,r,h;
        int d;
        bool operator <(const node cmp) const{
            return h<cmp.h;
        }
    }e[maxn];
    
    double Hash[maxn],tree[maxn*4];
    int lazy[maxn*4];
    
    void built(int l,int r,int now)
    {
        int mid=(l+r)>>1;
        lazy[now]=tree[now]=0;
        if(l==r)
            return ;
        built(lson);
        built(rson);
    }
    
    void pushdown(int now,int l,int r)
    {
        int mid=(l+r)>>1;
        if(lazy[now]!=-1)
        {
            lazy[ls]=lazy[rs]=lazy[now];
            tree[ls]=lazy[ls]?Hash[mid+1]-Hash[l]:0;
            tree[rs]=lazy[rs]?Hash[r+1]-Hash[mid+1]:0;
        }
    
    }
    
    void pushup(int now,int l,int r)
    {
        if(lazy[ls]==-1||lazy[rs]==-1||(lazy[rs]!=lazy[ls]))
            lazy[now]=-1;
        else
            lazy[now]=lazy[ls];
        tree[now]=tree[ls]+tree[rs];
    }
    
    void update(int l,int r,int now,int L,int R,int c)
    {
        int mid=(l+r)>>1;
        if(L<=l&&r<=R)
        {
            if(lazy[now]!=-1)
            {
                lazy[now]+=c;
                tree[now]=lazy[now]?Hash[r+1]-Hash[l]:0;
                return ;
            }
        }
        pushdown(now,l,r);
        if(L<=mid) update(lson,All,c);
        if(R>mid) update(rson,All,c);
        pushup(now,l,r);
    }
    
    int bin(double key,int n,double d[])
    {
        int l=1,r=n;
        while(r>=l)
        {
            int m=(r+l)/2;
            if(d[m]==key)
                return m;
            else if(d[m]>key)
                r=m-1;
            else
                l=m+1;
        }
    }
    
    int main(){
        int t,tot=0;
        while(sd(t)&&t)
        {
            int n=0,m=0;
            double x1,x2,y1,y2;
            rep(i,1,t)
            {
                slf(x1),slf(y1),slf(x2),slf(y2);
                Hash[++n]=x1;
                Hash[++n]=x2;
    
                e[++m]=(node){x1,x2,y1,1};
                e[++m]=(node){x1,x2,y2,-1};
            }
            sort(e+1,e+1+m);
            sort(Hash+1,Hash+1+n);
            n=unique(Hash+1,Hash+1+n)-Hash-1;
            built(1,n-1,1);
            double ans=0;
            rep(i,1,m-1)
            {
                int l=lower_bound(Hash+1,Hash+1+n,e[i].l)-Hash;
                int r=lower_bound(Hash+1,Hash+1+n,e[i].r)-Hash-1;
                if(l<=r) update(1,n-1,1,l,r,e[i].d);
                ans+=tree[1]*(e[i+1].h-e[i].h);
            }
            printf("Test case #%d
    Total explored area: %.2lf
    
    ",++tot,ans);
        }
        return 0;
    }
    
  • 相关阅读:
    webform单选、复选
    webform下拉列表、列表框
    webform文本框 、显示文字、按钮、跳转页面、页面传值
    sol函数初级查询,去重、分组、排序
    sql基础
    递归
    函数有多个返回值
    Hibernate (开放源代码的对象关系映射框架)介绍
    extjs介绍
    easyui介绍
  • 原文地址:https://www.cnblogs.com/minun/p/11295885.html
Copyright © 2011-2022 走看看