0 0 4 4 1 1 5 2 1 1 2 5 -1 -1 -1 -1 0 0 2 2 1 1 3 3 2 2 4 4 -1 -1 -1 -1 -1 -1 -1 -1
18 10
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 50010; 6 struct node { 7 int lt,rt,cover,sum; 8 } tree[maxn<<2]; 9 struct Line { 10 int x1,x2,y,up; 11 Line(int a = 0,int b = 0,int c = 0,int d = 0) { 12 x1 = a; 13 x2 = b; 14 y = c; 15 up = d; 16 } 17 bool operator<(const Line &t)const { 18 if(y == t.y) return up > t.up; 19 return y < t.y; 20 } 21 } line[maxn]; 22 void build(int L,int R,int v) { 23 tree[v].cover = tree[v].sum = 0; 24 tree[v].lt = L; 25 tree[v].rt = R; 26 if(L+1 == R) return; 27 int mid = (L + R)>>1; 28 build(L,mid,v<<1); 29 build(mid,R,v<<1|1); 30 } 31 void pushup(int v) { 32 if(tree[v].cover) { 33 tree[v].sum = tree[v].rt - tree[v].lt; 34 return; 35 } else if(tree[v].lt + 1 == tree[v].rt) { 36 tree[v].sum = 0; 37 return; 38 } 39 tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum; 40 } 41 void update(int lt,int rt,int val,int v) { 42 if(lt <= tree[v].lt && rt >= tree[v].rt) { 43 tree[v].cover += val; 44 pushup(v); 45 return; 46 } 47 if(lt < tree[v<<1].rt) update(lt,rt,val,v<<1); 48 if(rt > tree[v<<1|1].lt) update(lt,rt,val,v<<1|1); 49 pushup(v); 50 } 51 int main() { 52 int x1,y1,x2,y2; 53 while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2)) { 54 if(x1 == -1 && y1 == -1 && x2 == -1 && y2 == -1) break; 55 int tot = 0; 56 line[tot++] = Line(x1,x2,y1,1); 57 line[tot++] = Line(x1,x2,y2,-1); 58 while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2)) { 59 if(x1 == -1 && y1 == -1 && x2 == -1 && y2 == -1) break; 60 line[tot++] = Line(x1,x2,y1,1); 61 line[tot++] = Line(x1,x2,y2,-1); 62 } 63 sort(line,line+tot); 64 int ret = 0,pre = 0; 65 build(0,50000,1); 66 for(int i = 0; i < tot; ++i) { 67 ret += tree[1].sum*(line[i].y - pre); 68 pre = line[i].y; 69 update(line[i].x1,line[i].x2,line[i].up,1); 70 } 71 printf("%d\n",ret); 72 } 73 return 0; 74 }