zoukankan      html  css  js  c++  java
  • HDU 3265 Posters 线段树 扫描线

    把一张海报剩余的部分分割成四个小矩形,然后就是简单的矩形面积并了。

    //#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!='
    ')return ch;
        }
        return EOF;
    }
    const int maxn=201000;
    struct Seg
    {
        int l,r,x;
        int c;
        Seg(){}
        Seg(int a,int  b,int c,int d):l(a),r(b),x(c),c(d){}
        bool operator < (const Seg& another) const
        {
            return x<another.x;
        }
    };
    Seg seg[maxn<<1];
    int flag[maxn<<3];
    int sum[maxn<<3];
    int join(int& m,int x1,int y1,int x2,int y2)
    {
        seg[m++]=Seg(y1,y2,x1,1);
        seg[m++]=Seg(y1,y2,x2,-1);
        return 0;
    }
    int PushUp(int idx,int l,int r)
    {
        if(flag[idx])
        {
            sum[idx]=r+1-l;
        }else if(l!=r)
            sum[idx]=sum[idx<<1]+sum[idx<<1|1];
        else sum[idx]=0;
        return 0;
    }
    int build(int idx,int l,int r)
    {
        memset(flag,0,sizeof(flag));
        memset(sum,0,sizeof(sum));
        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;
        }
        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;
        while(scanf("%d",&n)!=EOF&&n)
        {
            int m=0;
            for(int i=0;i<n;i++)
            {
                int x1,x2,x3,x4,y1,y2,y3,y4;
                scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
                if(x3!=x1)join(m,x1,y1,x3,y4);
                if(y4!=y2)join(m,x1,y4,x4,y2);
                if(x4!=x2)join(m,x4,y3,x2,y2);
                if(y3!=y1)join(m,x3,y1,x2,y3);
            }
            sort(seg,seg+m);
            ll res=0;
            build(1,0,50000);
            for(int i=0;i<m;i++)
            {
                if(i!=0)res+=(ll)(seg[i].x-seg[i-1].x)*sum[1];
                update(1,0,50000,seg[i].l,seg[i].r-1,seg[i].c);
            }
            printf("%I64d
    ",res);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    使用控制结构——循环语句——基本循环
    oracle字符类型 char,varchar2,varchar,clob,nvarchar,nclob
    使用控制结构——条件分支语句——多重条件分支
    hduoj 1518square
    运算符重载实现复数的加减乘除
    使用游标——参数游标
    开发PL/SQL子程序——触发器——编译触发器,删除触发器,显示触发器
    NYOJ58最少步数
    使用控制结构——条件分支语句——简单条件
    开发PL/SQL子程序——触发器——使用触发器注意事项
  • 原文地址:https://www.cnblogs.com/BMan/p/3318906.html
Copyright © 2011-2022 走看看