POJ最近做过的原题。
1 /* 2 ID: cuizhe 3 LANG: C++ 4 TASK: picture 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <string> 9 #include <cmath> 10 #include <algorithm> 11 using namespace std; 12 #define maxn 10000 13 #define lson l , m, rt<<1 14 #define rson m+1, r,rt<<1|1 15 int que[maxn*4]; 16 int sum[maxn*4]; 17 int cnt[maxn*4]; 18 bool lbd[maxn*4] , rbd[maxn*4]; 19 int numseg[maxn*4]; 20 struct node 21 { 22 int lx,rx,y; 23 int s; 24 node() {} 25 node(int a,int b,int c,int d):lx(a),rx(b),y(c),s(d) {} 26 bool operator < (const node &S) const 27 { 28 if(y == S.y) return s > S.s; 29 return y < S.y; 30 } 31 } mat[maxn]; 32 int bin(int x,int n) 33 { 34 int str,end,mid; 35 str = 0,end = n; 36 while(str <= end) 37 { 38 mid = (str+end)/2; 39 if(que[mid] == x) 40 return mid; 41 else if(que[mid] > x) 42 end = mid - 1; 43 else 44 str = mid + 1; 45 } 46 return mid; 47 } 48 void pushup(int rt,int l,int r) 49 { 50 if(cnt[rt]) 51 { 52 lbd[rt] = rbd[rt] = 1; 53 sum[rt] = que[r+1] - que[l]; 54 numseg[rt] = 2; 55 } 56 else if (l == r) 57 { 58 sum[rt] = numseg[rt] = lbd[rt] = rbd[rt] = 0; 59 } 60 else 61 { 62 lbd[rt] = lbd[rt<<1]; 63 rbd[rt] = rbd[rt<<1|1]; 64 sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 65 numseg[rt] = numseg[rt<<1] + numseg[rt<<1|1]; 66 if (lbd[rt<<1|1] && rbd[rt<<1]) numseg[rt] -= 2; 67 } 68 } 69 void update(int L,int R,int c,int l,int r,int rt) 70 { 71 if(L <= l&&r <= R) 72 { 73 cnt[rt] += c; 74 pushup(rt,l,r); 75 return ; 76 } 77 int m = (l+r)>>1; 78 if(L <= m) 79 update(L,R,c,lson); 80 if(R > m) 81 update(L,R,c,rson); 82 pushup(rt,l,r); 83 } 84 int main() 85 { 86 int n,num,i,l,r; 87 int a,b,c,d; 88 freopen("picture.in","r",stdin); 89 freopen("picture.out","w",stdout); 90 while(scanf("%d",&n)!=EOF) 91 { 92 if(!n) break; 93 num = 0; 94 for(i = 0; i < n; i ++) 95 { 96 scanf("%d%d%d%d",&a,&b,&c,&d); 97 que[num] = a; 98 mat[num ++] = node(a,c,b,1); 99 que[num] = c; 100 mat[num ++] = node(a,c,d,-1); 101 } 102 sort(que,que+num); 103 sort(mat,mat+num); 104 int k = 1; 105 for(i = 1; i < num; i ++) 106 { 107 if(que[i] != que[i-1]) 108 que[k++] = que[i]; 109 } 110 int ans = 0,last = 0; 111 memset(cnt,0,sizeof(cnt)); 112 memset(sum,0,sizeof(sum)); 113 memset(lbd,0,sizeof(lbd)); 114 memset(rbd,0,sizeof(rbd)); 115 for(i = 0; i < num; i ++) 116 { 117 l = bin(mat[i].lx,k-1); 118 r = bin(mat[i].rx,k-1)-1; 119 if(l <= r) 120 update(l,r,mat[i].s,0,k-1,1); 121 ans += numseg[1] * (mat[i+1].y - mat[i].y); 122 ans += abs(sum[1] - last); 123 last = sum[1]; 124 } 125 printf("%d ",ans); 126 } 127 return 0; 128 }