首页 > 其他 > 详细

HDU 1828 Picture

时间:2015-03-25 23:21:29      阅读:225      评论:0      收藏:0      [点我收藏+]

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

 

HDU 1828 Picture

原文:http://www.cnblogs.com/crackpotisback/p/4366992.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!