HDU 1828 Picture

Picture

Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on  HDU. Original ID: 1828
64-bit integer IO format: %I64d      Java class name: Main
 
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

Source

 
解题:线段树扫描线
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int maxn = 100000;
 7 struct node{
 8     int lt,rt,cover,len;
 9 }tree[maxn<<2];
10 struct line{
11     int x,y,h,del;
12     line(int a = 0,int b = 0,int c = 0,int d = 0){
13         x = a;
14         y = b;
15         h = c;
16         del = d;
17     }
18     bool operator<(const line& t)const{
19         return h == t.h?del > t.del:h < t.h;//特别注意此处
20     }
21 }nx[maxn],ny[maxn];
22 void build(int lt,int rt,int v){
23     tree[v].lt = lt;
24     tree[v].rt = rt;
25     tree[v].cover = tree[v].len = 0;
26     if(lt + 1 == rt) return;
27     int mid = (lt + rt)>>1;
28     build(lt,mid,v<<1);
29     build(mid,rt,v<<1|1);
30 }
31 void modify(int v){
32     if(tree[v].cover) tree[v].len = tree[v].rt - tree[v].lt;
33     else if(tree[v].lt + 1 == tree[v].rt) tree[v].len = 0;
34     else tree[v].len = tree[v<<1].len + tree[v<<1|1].len;
35 }
36 void update(int lt,int rt,int v,int del){
37     if(tree[v].lt >= lt && tree[v].rt <= rt){
38         tree[v].cover += del;
39         modify(v);
40         return;
41     }
42     int mid = (tree[v].lt + tree[v].rt)>>1;
43     if(lt < mid) update(lt,rt,v<<1,del);
44     if(rt > mid) update(lt,rt,v<<1|1,del);
45     modify(v);
46 }
47 int main(){
48     int n,tot,x1,x2,y1,y2;
49     while(~scanf("%d",&n)){
50         build(-20000,20000,1);
51         for(int i = tot = 0; i < n; ++i){
52             scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
53             nx[tot] = line(x1,x2,y1,1);
54             nx[tot+1] = line(x1,x2,y2,-1);
55             ny[tot] = line(y1,y2,x1,1);
56             ny[tot+1] = line(y1,y2,x2,-1);
57             tot += 2;
58         }
59         sort(nx,nx+tot);
60         sort(ny,ny+tot);
61         LL ans = 0,pre = 0;
62         for(int i = 0; i < tot; ++i){
63             update(nx[i].x,nx[i].y,1,nx[i].del);
64             ans += abs(tree[1].len - pre);
65             pre = tree[1].len;
66         }
67         for(int i = pre = 0; i < tot; ++i){
68             update(ny[i].x,ny[i].y,1,ny[i].del);
69             ans += abs(tree[1].len - pre);
70             pre = tree[1].len;
71         }
72         printf("%I64d\n",ans);
73     }
74     return 0;
75 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4366992.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值