HDU 3265 Posters

Posters

Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3265
64-bit integer IO format: %I64d      Java class name: Main
 
Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters. 

However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window. 

Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out.

To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the posters and the edges of the holes on the posters are all parallel with the coordinate axes. 
 

Input

The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right corner. It is guaranteed that 0<=xi, yi<=50000(i=1…4) and x1<=x3<x4<=x2, y1<=y3<y4<=y2.

The input ends with a line of single zero.
 

Output

For each test case, output a single line with the total area of window covered by posters.
 

Sample Input

2
0 0 10 10 1 1 9 9
2 2 8 8 3 3 7 7
0

Sample Output

56

Source

 
解题:线段树扫描线求矩形面积的并,注意溢出,注意矩形分割。。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 100000;
18 struct node{
19     int lt,rt,cnt,len;
20 };
21 struct Line{
22     int lx,rx,h,delta;
23     Line(int x = 0,int y = 0,int z = 0,int d = 0){
24         lx = x;
25         rx = y;
26         h = z;
27         delta = d;
28     }
29     bool operator<(const Line &b) const{
30         return h < b.h;
31     }
32 };
33 node tree[maxn<<2];
34 Line line[maxn<<2];
35 int tot;
36 void build(int lt,int rt,int v){
37     tree[v].lt = lt;
38     tree[v].rt = rt;
39     tree[v].cnt = tree[v].len = 0;
40     if(lt + 1 == rt) return;
41     int mid = (lt + rt)>>1;
42     build(lt,mid,v<<1);
43     build(mid,rt,v<<1|1);
44 }
45 void pushup(int v){
46     if(tree[v].cnt) tree[v].len = tree[v].rt - tree[v].lt;
47     else if(tree[v].lt + 1 == tree[v].rt) tree[v].len = 0;
48     else tree[v].len = tree[v<<1].len + tree[v<<1|1].len;
49 }
50 void update(int lt,int rt,int v,int delta){
51     if(tree[v].lt >= lt && tree[v].rt <= rt){
52         tree[v].cnt += delta;
53         pushup(v);
54         return;
55     }
56     if(lt < tree[v<<1].rt) update(lt,rt,v<<1,delta);
57     if(rt > tree[v<<1|1].lt) update(lt,rt,v<<1|1,delta);
58     pushup(v);
59 }
60 int main() {
61     int n,x1,y1,x2,y2,x3,y3,x4,y4;
62     while(scanf("%d",&n),n){
63         build(0,50010,1);
64         for(int i = tot = 0; i < n; ++i){
65             scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
66             scanf("%d %d %d %d",&x3,&y3,&x4,&y4);
67             line[tot++] = Line(x1,x4,y4,1);
68             line[tot++] = Line(x1,x4,y2,-1);
69             line[tot++] = Line(x4,x2,y3,1);
70             line[tot++] = Line(x4,x2,y2,-1);
71             line[tot++] = Line(x3,x2,y1,1);
72             line[tot++] = Line(x3,x2,y3,-1);
73             line[tot++] = Line(x1,x3,y1,1);
74             line[tot++] = Line(x1,x3,y4,-1);
75         }
76         sort(line,line+tot);
77         LL sum = 0;
78         for(int i = 0; i+1 < tot; ++i){
79             update(line[i].lx,line[i].rx,1,line[i].delta);
80             sum += (LL)tree[1].len*(line[i+1].h - line[i].h);//这个地方有点坑啊。。注意溢出。。
81         }
82         cout<<sum<<endl;
83     }
84     return 0;
85 }
View Code

 

posted @ 2014-11-24 13:55  狂徒归来  阅读(181)  评论(0编辑  收藏  举报