Description
Input
Output
Sample Input
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00题意:给出左上角和右下角坐标,叫你把矩形覆盖的面积求出来。AC代码:#include<iostream> #include<algorithm> #include<cstring> #include<string> #include<cstdio> using namespace std; #define T 10005 #define lson (rt<<1) #define rson (rt<<1|1) typedef long long ll; int n; //离散化数组 double pos[T]; //保存y在x区间的边 struct line { double y,x_up,x_down; int flag; bool operator<(const line& a)const{ return y<a.y; } line(double _1,double _2,double _3,int _4): y(_1),x_up(_2),x_down(_3),flag(_4){} line(){} }p[T]; //线段树数组 struct node { int L,R; double len; int mid; int flag; }tree[T]; //向上更新 void pushup(int rt) { if(tree[rt].flag){//完全覆盖区间 tree[rt].len = pos[tree[rt].R-1]-pos[tree[rt].L-1]; } else if(tree[rt].L+1==tree[rt].R){//叶子节点 tree[rt].len = 0; } else//跨越几个区间合并值 { tree[rt].len = tree[lson].len+tree[rson].len; } } //建树 void build(int rt,int L,int R) { tree[rt].L = L; tree[rt].R = R; tree[rt].flag = 0; tree[rt].len = 0; tree[rt].mid = (L+R)>>1; if(L+1!=R){ build(lson,L,tree[rt].mid); build(rson,tree[rt].mid,R); } } //更新 void update(int rt,int L,int R,int w) { if(L<=tree[rt].L&&tree[rt].R<=R){ tree[rt].flag+=w; pushup(rt); return; } if(R<=tree[rt].mid) update(lson,L,R,w); else if(L>=tree[rt].mid) update(rson,L,R,w); else{ update(lson,L,tree[rt].mid,w); update(rson,tree[rt].mid,R,w); } pushup(rt); } int main() { #ifdef zsc freopen("input.txt","r",stdin); #endif int cas=0; int i,c; double x1,x2,y1,y2; while(scanf("%d",&n),n) { c = 0; for(i=0;i<n;++i){ scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); pos[c] = x1; p[c++] = line(y1,x1,x2,1); pos[c] = x2; p[c++] = line(y2,x1,x2,-1); } sort(pos,pos+c); int d = unique(pos,pos+c)-pos; build(1,1,d); sort(p,p+c); double ans=0; printf("Test case #%d\n",++cas); for(i=0;i<c-1;++i){ //因为离散化了,所以要找x的下标 int a = lower_bound(pos,pos+d,p[i].x_up)-pos+1; int b = lower_bound(pos,pos+d,p[i].x_down)-pos+1; //更新x区间(a,b)的值 update(1,a,b,p[i].flag); ans+=tree[1].len*(p[i+1].y-p[i].y); } printf("Total explored area: %.2lf\n",ans); printf("\n"); } return 0; }