HDU1828 Picture 线段树+扫描线模板题

Posted buerdepepeqi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 




















以上是关于HDU1828 Picture 线段树+扫描线模板题的主要内容,如果未能解决你的问题,请参考以下文章

HDU1828 Picture 线段树+扫描线模板题

HDU 1828 Picture(线段树扫描线求周长)

hdu1828 Picture (线段树:扫描线周长)

51nod 1206 && hdu 1828 Picture (扫描线+离散化+线段树 矩阵周长并)

[HDOJ1828]Picture(扫描线,线段树,矩形并周长)

HDOJ1828&&POJ1177Picture(线段树,扫描线)