zoukankan      html  css  js  c++  java
  • HDU1828 Picture 线段树+扫描线模板题

    Picture

    Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6332    Accepted Submission(s): 2951


    Problem Description
    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter. 

    Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1. 



    The corresponding boundary is the whole set of line segments drawn in Figure 2. 



    The vertices of all rectangles have integer coordinates.
     
    Input
    Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate. 

    0 <= number of rectangles < 5000 
    All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

    Please process to the end of file.
     
    Output
    Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
     
    Sample Input
    7 -15 0 5 10 -5 8 20 25 15 -4 24 14 0 -6 16 4 2 15 10 22 30 10 36 20 34 0 40 16
     
    Sample Output
    228

    代码如下

    #include <map>
    #include <set>
    #include <cmath>
    #include <ctime>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cctype>
    #include <bitset>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #define fuck(x) cout<<"["<<x<<"]";
    #define FIN freopen("input.txt","r",stdin);
    #define FOUT freopen("output.txt","w+",stdout);
    #pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    const int maxn = 1e5+5;
    struct node1{
        int l,r;
        int cover;
        int len;
        int lbt,rbt;
        int segnum;
    }st[maxn<<2];
    struct node2{
        int x,y1,y2;
        int flag;
    }edge[maxn];
    int Y[maxn];
    void build(int root,int l,int r){
        st[root].l=l;
        st[root].r=r;
        st[root].cover=st[root].len=0;
        st[root].lbt=st[root].rbt=0;
        st[root].segnum=0;
        if(r-l==1) return;
        int mid=(l+r+1)/2;
        build(root<<1,l,mid);
        build(root<<1|1,mid,r);
    }
    
    void update(int root){
        if(st[root].cover>0){
            st[root].len=Y[st[root].r]-Y[st[root].l];
            st[root].lbt=st[root].rbt=1;
            st[root].segnum=1;
        }else if(st[root].r-st[root].l==1){
            st[root].len=0;
            st[root].lbt=st[root].rbt=0;
            st[root].segnum=0;
        }else{
            st[root].len=st[root<<1].len+st[root<<1|1].len;
             st[root].lbt=st[root<<1].lbt;
             st[root].rbt=st[root<<1|1].rbt;
             st[root].segnum=st[root<<1].segnum+st[root<<1|1].segnum-st[root<<1].rbt*st[root<<1|1].lbt;
        }
    }
    void update(int root,int val,int y1,int y2){
        if(y1<=Y[st[root].l]&&Y[st[root].r]<=y2){
            st[root].cover+=val;
        }else{
            int mid=(st[root].l+st[root].r+1)/2;
            if(y1<Y[mid]) update(root<<1,val,y1,y2);
            if(y2>Y[mid]) update(root<<1|1,val,y1,y2);
        }
        update(root);
    }
    bool cmp(node2 a,node2 b){
        if(a.x==b.x) return a.y1<b.y1;
        return a.x<b.x;
    }
    int main(){
    #ifndef ONLINE_JUDGE
        FIN
    #endif
    
        int n;
        while(scanf("%d",&n) !=EOF){
            if(n==0) printf("0
    ");
            int x1,x2,y1,y2;
            for(int i=0;i<n;i++){
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                edge[i<<1].x=x1;
                edge[i<<1].y1=y1;
                edge[i<<1].y2=y2;
                edge[i<<1].flag=1;
                Y[i<<1]=y1;
                edge[i<<1|1].x=x2;
                edge[i<<1|1].y1=y1;
                edge[i<<1|1].y2=y2;
                edge[i<<1|1].flag=-1;
                Y[i<<1|1]=y2;
            }
            sort(Y,Y+2*n);
            sort(edge,edge+2*n,cmp);
            int cnt=0;
            for(int i=0;i<2*n;i++){
                if(Y[i]!=Y[i+1]) Y[cnt++]=Y[i];
            }
            Y[cnt++]=Y[2*n-1];
            build(1,0,cnt-1);
            int ans=0;
            int lastlen=0;
            for(int i=0;i<2*n-1;i++){
                update(1,edge[i].flag,edge[i].y1,edge[i].y2);
                ans+=2*st[1].segnum*(edge[i+1].x-edge[i].x);
                ans+=abs(st[1].len-lastlen);
                lastlen=st[1].len;
            }
            ans+=(edge[2*n-1].y2-edge[2*n-1].y1);
            printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    多角度分析平台即服务?PaaS的类型和用例
    2021.3.10 Android导出Excel表
    2021.3.9 个人作业阶段2
    2021.3.8 Android图像视图1
    2021.3.7 Android图像视图
    2021.3.6 Android页面刷新
    2021.3.5 个人作业1+家庭记账本(8)
    2021.3.4 家庭记账本(7)
    2021.3.3 Android项目打包+家庭记账本(6)
    2021.3.2 开课博客+家庭记账本(5)
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9374499.html
Copyright © 2011-2022 走看看