poj--1177(线段树+离散化+扫描线)

题目:http://poj.org/problem?id=1177

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

参考:https://www.cnblogs.com/shuaiwhu/archive/2012/04/22/2464876.html

代码:

//自己写的re
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;

const int maxn = 40005;

struct node
{
    int st,ed,m,lbd,rbd;
    int sequence_line,coun;
}tree[maxn];

void build(int st,int ed,int p)
{
    tree[p].st = st;
    tree[p].ed = ed;
    tree[p].m = tree[p].lbd = tree[p].rbd = 0;
    tree[p].sequence_line = tree[p].coun = 0;
    if(ed - st > 1)
    {
        int m = ( st + ed ) / 2;
        build(st,m,2 * p + 1);
        build(m,ed,2 * p + 2);
    }
}

inline void update(int p)
{
    if(tree[p].coun > 0)
    {
        tree[p].m = tree[p].ed - tree[p].st;
        tree[p].lbd = tree[p].rbd = 1;
        tree[p].sequence_line = 1;
        return;
    }
    if(tree[p].ed - tree[p].st == 1)
    {
        tree[p].m = 0;
        tree[p].lbd = tree[p].rbd = 0;
        tree[p].sequence_line = 0;
    }
    else {
        int left = 2 * p + 1,right = 2 * p + 2;
        tree[p].m = tree[left].m + tree[right].m;
        tree[p].sequence_line = tree[left].sequence_line + tree[right].sequence_line - (tree[left].rbd & tree[right].lbd);
        tree[p].lbd = tree[left].lbd;
        tree[p].rbd = tree[right].rbd;
    }
}

void Insert(int st,int ed,int p)
{
    if(st <= tree[p].st && ed >= tree[p].ed)
    {
        tree[p].coun ++;
        update(p);
        return;
    }
    int m = (tree[p].ed + tree[p].st) >> 1;
    if(st < m)Insert(st,ed,2 * p + 1);
    if(ed > m)Insert(st,ed,2 * p + 2);
    update(p);
}

void Delete(int st,int ed,int p)
{
    if(st <= tree[p].st && ed >= tree[p].ed)
    {
        tree[p].coun --;
        update(p);
        return;
    }
    int m = (tree[p].ed + tree[p].st) >> 1;
    if(st < m) Delete(st,ed,2 * p + 1);
    if(ed > m) Delete(st,ed,2 * p + 2);
    update(p);
}

struct line{
    int x,y1,y2;//y1 < y2
    bool d;//d = true 表示该线段位矩形的左边,d = false 表示该线段是矩形的右边
}a[10003];

bool cmp(line t1,line t2)
{
    return t1.x < t2.x;
}

void Cal_c(int n)
{
    int t2,sum = 0;
    t2 = 0;
    a[n] = a[n - 1];
    for(int i = 0;i < n;i ++)
    {
        if(a[i].d == 1)Insert(a[i].y1,a[i].y2,0);
        else Delete(a[i].y1,a[i].y2,0);
        sum += tree[0].sequence_line * (a[i + 1].x - a[i].x) * 2;
        sum += abs(tree[0].m - t2);
        t2 = tree[0].m;
    }
    printf("%d\n",sum);
}

int main()
{
    int j,n,x1,x2,y1,y2,suby,upy;
    while(cin>>n)
    {
        j = 0;
        suby = 10000;
        upy = -10000;
        for(int i = 0;i < n;i ++)
        {
            cin>>x1>>y1>>x2>>y2;
            a[j].x = x1;
            a[j].y1 = y1;
            a[j].y2 = y2;
            a[j].d = 1;
            j ++;
            a[j].x = x2;
            a[j].y1 = y1;
            a[j].y2 = y2;
            a[j].d = 0;
            j ++;
            if(suby > y1) suby = y1;
            if(upy < y2) upy = y2;
        }
        sort(a,a + j,cmp);
        build(suby,upy,0);
        Cal_c(j);
    }
    return 0;
}

//标程
#include<iostream>
#include<algorithm>
#include <cstdio>
using namespace std;
struct node{
int st,ed,m,lbd,rbd;
int sequence_line,count;
}ST[40005];
void build(int st, int ed, int v){ //建树,区间为 [st,ed]
        ST[v].st = st; ST[v].ed = ed;
        ST[v].m = ST[v].lbd = ST[v].rbd = 0;
        ST[v].sequence_line = ST[v].count = 0;
        if(ed - st > 1){
            int mid = (st+ed)/2;
            build(st, mid, 2*v+1);
            build(mid, ed, 2*v+2);
        }
}
inline void UpData(int v){ //更新结点区间的测度
    if(ST[v].count > 0){
        ST[v].m = ST[v].ed - ST[v].st;
        ST[v].lbd = ST[v].rbd = 1;
        ST[v].sequence_line = 1;
        return;
    }
    if(ST[v].ed - ST[v].st == 1){
        ST[v].m = 0;
        ST[v].lbd = ST[v].rbd = 0;
        ST[v].sequence_line = 0;
    }
    else {
        int left = 2*v+1, right = 2*v+2;
        ST[v].m = ST[left].m + ST[right].m;
        ST[v].sequence_line = ST[left].sequence_line +
        ST[right].sequence_line - (ST[left].rbd & ST[right].lbd);
        ST[v].lbd = ST[left].lbd;
        ST[v].rbd = ST[right].rbd;
    }
}
void insert(int st, int ed, int v){
    if(st <= ST[v].st && ed >= ST[v].ed){
        ST[v].count++;
        UpData(v);
        return ;
    }
    int mid = (ST[v].st + ST[v].ed)/2;
    if(st < mid)insert(st, ed, 2*v+1);
    if(ed > mid)insert(st, ed, 2*v+2);
    UpData(v);
}
void Delete(int st, int ed, int v){
    if(st <= ST[v].st && ed >= ST[v].ed){
    ST[v].count--;
    UpData(v);
    return;
    }
    int mid = (ST[v].st + ST[v].ed)/2;
    if(st < mid)Delete(st, ed, 2*v+1);
    if(ed > mid)Delete(st, ed, 2*v+2);
    UpData(v);
}
struct line{
    int x,y1,y2;//y1 < y2
    bool d; //d = true 表示该线段为矩形左边, d = false表示该线段为矩形的右边

}a[10003];
bool cmp(line t1, line t2){ //为线段排序的函数,方便从左向右的扫描
return t1.x < t2.x;
}
void cal_C(int n);
int main()
{
    int n,x1,x2,y1,y2,i,j,suby, upy;
    while(scanf("%d",&n) != EOF){
        j = 0;
        suby = 10000; upy = -10000;
        for(i = 0; i < n; i++){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            a[j].x = x1; a[j].y1 = y1; a[j].y2 = y2;
            a[j].d = 1;
            j++;
            a[j].x = x2; a[j].y1 = y1; a[j].y2 = y2;
            a[j].d = 0;
            j++;
            if(suby > y1)suby = y1;
            if(upy < y2)upy = y2;
        }
        sort(a, a+j, cmp);
        build(suby,upy,0);
        cal_C(j);
    }
    return 0;
}
void cal_C(int n){
    int i,j,k,t2,sum=0;
    t2 = 0;
    a[n] = a[n-1];
    for(i = 0; i < n; i++){
        if(a[i].d == 1) insert(a[i].y1, a[i].y2, 0);
        else Delete(a[i].y1, a[i].y2, 0);
        sum += ST[0].sequence_line * (a[i+1].x-a[i].x)* 2;
        sum += abs(ST[0].m - t2);
        t2 = ST[0].m;
    }
    printf("%d\n",sum);
}

 不知道为什么就re了,路过大佬帮小生看看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值