Picture 1828

http://acm.hdu.edu.cn/showproblem.php?pid=1828

求矩形的周长

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #define lson l,mid,i<<1
 5 #define rson mid+1,r,i<<1|1
 6 using namespace std;
 7 const int Ni = 40100;
 8 struct node{
 9     int l,r,h,s;
10     node(){}
11     node(int a,int b,int c,int d):l(a),r(b),h(c),s(d){}
12     bool operator < (const node &a) const
13     {
14         if(h==a.h) return s > a.s;
15        return h < a.h;
16     }
17 }arr[Ni];
18 int sum[Ni*3];
19 int num[Ni*3];
20 int cnt[Ni*3];
21 int lbd[Ni*3];
22 int rbd[Ni*3];
23 void build(int l,int r,int i)
24 {
25     sum[i]=num[i]=cnt[i]=0;
26     lbd[i]=rbd[i]=0;
27     if(l==r) return;
28     int mid=(l+r)>>1;
29     build(lson);
30     build(rson);
31 }
32 void pushup(int l,int r,int i)
33 {
34     if (cnt[i]) {
35         lbd[i] = rbd[i] = 1;
36         sum[i] = r - l + 1;
37         num[i] = 2;
38     } else if (l == r) {
39         sum[i] = num[i] = lbd[i] = rbd[i] = 0;
40     } else {
41         lbd[i] = lbd[i<<1];
42         rbd[i] = rbd[i<<1|1];
43         sum[i] = sum[i<<1] + sum[i<<1|1];
44         num[i] = num[i<<1] + num[i<<1|1];
45         if (lbd[i<<1|1] && rbd[i<<1]) num[i] -= 2;
46     }
47 }
48 void update(int ql,int qr,int s,int l,int r,int i=1)
49 {
50     if(ql<=l&&r<qr)
51     {
52         cnt[i]+=s;
53         pushup(l,r,i);
54         return;
55     }
56     int mid=(l+r)>>1;
57     if(ql<=mid) update(ql,qr,s,lson);
58     if(qr>mid+1) update(ql,qr,s,rson);
59     pushup(l,r,i);
60 }
61 int main()
62 {
63     int n,m,i;
64     while(cin>>n)
65     {
66         m=0;
67         int l=100000,r=-100000;
68         for(i=0;i<n;i++)
69         {
70             int a,b,c,d;
71             scanf("%d%d%d%d",&a,&b,&c,&d);
72             l=min(l,a);
73             r=max(r,c);
74             arr[m++]=node(a,c,b,1);
75             arr[m++]=node(a,c,d,-1);
76         }
77         sort(arr,arr+m);
78         build(l,r,1);
79         int ret=0,last=0;
80         for(i=0;i<m;i++)
81         {
82             update(arr[i].l,arr[i].r,arr[i].s,l,r);
83             ret+=num[1]*(arr[i+1].h-arr[i].h);
84             ret+=abs(sum[1]-last);
85             last=sum[1];
86         }
87         printf("%d\n",ret);
88     }
89     return 0;
90 }

 

posted @ 2012-08-07 10:09  qijinbiao1  阅读(209)  评论(0编辑  收藏  举报