码迷,mamicode.com
首页 > 其他好文 > 详细

Rectangles Area Sum

时间:2015-11-22 17:26:51      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 #include<string.h>
 5 #include<algorithm>
 6 using namespace std; 
 7 #define re(i,n) for(int i=0;i<n;i++) 
 8 const int maxn=1e4+7;
 9 const int mod=1e9+7;
10 typedef long long ll;
11 int x[maxn*2],y[maxn*2],xsz,ysz;
12 struct Node{
13     int fy,ty;
14     int x;
15     int type;
16 }a[maxn*2];
17 bool cmp(const Node&m,const Node&n){
18     return m.x<n.x;
19 }
20 struct Data{
21     int cnt,len;
22 }da[maxn*16];
23 int fy,ty,type; 
24 void insert(int id,int f,int t){
25     //printf("inserting node %d ,from %d,to %d,fy %d ,ty %d,type %d\n",id,y[f],y[t],fy,ty,type);
26     if(fy<=y[f]&&ty>=y[t]){
27         da[id].cnt+=type;
28         if(da[id].cnt==0){
29             if(f==t)da[id].len=0;
30             else da[id].len=da[id<<1].len+da[id<<1|1].len;
31         }else da[id].len=y[t]-y[f];
32         return;
33     }
34     int mid=(f+t)>>1;
35     if(ty>y[mid])insert(id<<1|1,mid,t);
36     if(fy<y[mid])insert(id<<1,f,mid);
37     if(da[id].cnt==0)da[id].len=da[id<<1].len+da[id<<1|1].len;
38     else da[id].len=y[t]-y[f];
39 }
40 int main(){  
41     int n;
42     while(cin>>n&&n){
43         re(i,n){
44             scanf("%d%d%d%d",&x[i*2],&y[i*2],&x[i*2+1],&y[i*2+1]);
45             a[i*2].fy=a[i*2+1].fy=min(y[i*2],y[i*2+1]);
46             a[i*2].ty=a[i*2+1].ty=max(y[i*2],y[i*2+1]);
47             a[i*2].x=min(x[i*2],x[i*2+1]);
48             a[i*2+1].x=max(x[i*2],x[i*2+1]);
49             a[i*2].type=1;
50             a[i*2+1].type=-1;
51         }
52         sort(y,y+n*2),ysz=unique(y,y+n*2)-y;
53         sort(a,a+n*2,cmp);
54         ll ans=0;  
55         memset(da,0,sizeof(da));
56         fy=a[0].fy,ty=a[0].ty,type=a[0].type;
57         insert(1,0,ysz-1);
58         //cout<<"The first time "<<da[1].cnt<<endl;
59         //cout<<"ans is "<<ans<<endl;
60         for(int i=1;i<n*2;i++){
61             ans+=(ll)(a[i].x-a[i-1].x)*da[1].len;
62             ans%=mod;
63             fy=a[i].fy,ty=a[i].ty,type=a[i].type;
64             //printf("i=%d\t ans=%lld\t ",i,ans);
65             insert(1,0,ysz-1);
66         }
67         cout<<ans<<endl;
68     }  
69     return 0;
70 }

 

Rectangles Area Sum

标签:

原文地址:http://www.cnblogs.com/weidiao/p/4986194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!