先不说,因为还不是很懂的说~~~~~~
#include<iostream> #include<cstdlib> #define N 105 using namespace std; struct elem{ double x; double s; double t; bool flag; }; struct node{ int l; int r; int cover; double m; }; elem cor[2*N]; node tree[8*N]; double index[2*N]; int len; int n; double res; int cmp(const void *a,const void *b) { if((*(elem *)a).x > (*(elem *)b).x) return 1; else if((*(elem *)a).x == (*(elem *)b).x) return 0; else return -1; } int cmp1(const void *a,const void *b) { if(*(double *)a > *(double *)b) return 1; else if(*(double *)a == *(double *)b) return 0; else return -1; } void init() { int i; int k; double x1,y1,x2,y2; for(i = 0;i < n;i++){ cin >>x1 >>y1 >>x2 >>y2; k = i<<1; cor[k].x = x1;cor[k].s = y1; cor[k].t = y2; cor[k].flag = true; cor[k+1].x = x2; cor[k+1].s = y1; cor[k+1].t = y2; cor[k+1].flag = false; index[k] = y1; index[k+1] = y2; } qsort(cor,2*n,sizeof(cor[0]),cmp); qsort(index,2*n,sizeof(index[0]),cmp1); len = 0; for(i = 1;i < 2*n;i++){ if(index[i] != index[i-1]) index[++len] = index[i]; } //for(i = 0;i <= len;i++) // cout <<index[i] <<' '; //cout <<endl; return ; } int find(double x) { int base = 0; int top = len; int mid;//cout <<x <<' '; while(base <= top){ mid = (base+top)/2; if(x > index[mid]) base = mid+1; else if(x < index[mid]) top = mid-1; else return mid; } return -1; } void buid_tree(int k,int left,int right) { int mid; int L,R; tree[k].l = left; tree[k].r = right; tree[k].cover = 0; tree[k].m = 0; if(tree[k].r-tree[k].l == 1) return ; mid = (tree[k].l+tree[k].r)/2; L = k<<1; R = L+1; buid_tree(L,left,mid); buid_tree(R,mid,right); return ; } void init_m(int k) { int L,R; if(tree[k].cover > 0){ tree[k].m = index[tree[k].r]-index[tree[k].l]; return ; } L = k<<1; R = L+1; if(tree[k].r-tree[k].l > 1) tree[k].m = tree[L].m+tree[R].m; else tree[k].m = 0; return ; } void tree_insert(int k,int left,int right) { int mid; int L,R; if(left <= tree[k].l && right >= tree[k].r){ tree[k].cover++; init_m(k); return ; } mid = (tree[k].l+tree[k].r)/2; L = k<<1; R = L+1; if(left < mid) tree_insert(L,left,right); if(right > mid) tree_insert(R,left,right); init_m(k);//cout <<tree[k].m <<' '; return ; } void tree_delete(int k,int left,int right) { int mid; int L,R; if(left <= tree[k].l && right >= tree[k].r){ tree[k].cover--; init_m(k); return ; } mid = (tree[k].l+tree[k].r)/2; L = k<<1; R = L+1; if(left < mid) tree_delete(L,left,right); if(right > mid) tree_delete(R,left,right); init_m(k); return; } void slove() { int i; res = 0; for(i = 0;i < (2*n-1);i++){ if(cor[i].flag) tree_insert(1,find(cor[i].s),find(cor[i].t)); else tree_delete(1,find(cor[i].s),find(cor[i].t)); res += tree[1].m*(cor[i+1].x-cor[i].x); } } int main() { int walk = 0; while(cin >>n && n){ init(); buid_tree(1,0,len); slove(); printf("Test case #%d/n",++walk); printf("Total explored area: %.2lf/n/n",res); } return 0; }