poj-1177.Picture(线段树+扫描线求矩形边长)

Picture题目传送门
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 15393 Accepted: 8000
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.
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
Source

IOI 1998
题目思路:
用线段树维护每一个高度上的矩阵的长度,和该长度上所有的竖线的总对数(想想为什么是对数)。用本次的矩阵长度减去上次的长度就是本层高度的横线长度。对高度进行依次扫描即可。

代码如下:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5+7;
const int maxn2= 6010;
const int inf =0x3f3f3f3f;
int n,m,w[maxn],cnt,an[maxn];
struct node{
    int x1,x2,h,val;
}q[maxn<<2];
struct node2{
    int cnt,lf,rf;//标记这个节点的左右两个端点是否被覆盖(0表示没有,1表示有)
    int seg;//这个区间有多少竖线段
    int c;
}s[maxn<<4];
int cmp(node a,node b){
    return a.h<b.h;
}
void build_tree(int ro,int l,int r){
    s[ro]={0,0,0,0,0,};
    if(l==r) return ;
    int mid=(l+r)>>1;
    build_tree(ro<<1,l,mid);
    build_tree(ro<<1|1,mid+1,r);
}
void push_up(int ro,int l,int r){
    if(s[ro].c){
        s[ro].cnt=w[r+1]-w[l];
        s[ro].seg=1;
        s[ro].rf=s[ro].lf=1;
    }else if(r==l) {
        s[ro].cnt=0;s[ro].rf=s[ro].lf=0;
        s[ro].seg=0;
    }else{
        s[ro].cnt=s[ro<<1].cnt+s[ro<<1|1].cnt;
        s[ro].lf=s[ro<<1].lf;s[ro].rf=s[ro<<1|1].rf;
        s[ro].seg=s[ro<<1].seg+s[ro<<1|1].seg-(s[ro<<1].rf&s[ro<<1|1].lf);
    }
}
void update(int ro,int l,int r,int nl,int nr,int val){
    if(l==nl&&r==nr){
        s[ro].c+=val; push_up(ro,l,r);return ;
    }
    int mid=(l+r)>>1;
    if(nr<=mid) update(ro<<1,l,mid,nl,nr,val);
    else if(nl>mid) update(ro<<1|1,mid+1,r,nl,nr,val);
    else
    {
        update(ro<<1,l,mid,nl,mid,val);
        update(ro<<1|1,mid+1,r,mid+1,nr,val);
    }
    push_up(ro,l,r);
}
int main()
{
    while(cin>>n){
        cnt=1;
        for(int i=1;i<=n;i++){
            int x1,x2,y1,y2;scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            q[cnt]={x1,x2,y1,1};w[cnt++]=x1;
            q[cnt]={x1,x2,y2,-1}; w[cnt++]=x2;
        }
        sort(q+1,q+cnt,cmp);sort(w+1,w+cnt);
        int len=unique(w+1,w+cnt)-w;
        int ans=0,last=0;build_tree(1,1,len);
        for(int i=1;i<cnt;i++){
            int l=lower_bound(w+1,w+len,q[i].x1)-w;
            int r=lower_bound(w+1,w+len,q[i].x2)-w-1;
            update(1,1,len,l,r,q[i].val);
            ans+=abs(last-s[1].cnt);
            last=s[1].cnt;
            if(i!=cnt-1) ans+=2*s[1].seg*(q[i+1].h-q[i].h);
        }
        cout<<ans<<endl;
    }
    return 0;
}

/*
    2
    10 10 20 20
    15 15 25 25
*/
发布了35 篇原创文章 · 获赞 25 · 访问量 808

猜你喜欢

转载自blog.csdn.net/qq_43816599/article/details/103994597