Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://poj.org/problem?id=1151Description
Input
The input file is terminated by a line containing a single 0. Don't process it.
Output
Output a blank line after each test case.
Sample Input
10 10 20 20
15 15 25 25.5
0
Sample Output
Total explored area: 180.00
HINT
题意
给你N个矩形,求矩形相交的面积题解:
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 20001 #define mod 10007 #define eps 1e-9 //const int inf=0x7fffffff; //无限大 const int inf=0x3f3f3f3f; /* int buf[10]; inline void write(int i) { int p = 0;if(i == 0) p++; else while(i) {buf[p++] = i % 10;i /= 10;} for(int j = p-1; j >=0; j--) putchar('0' + buf[j]); printf("\n"); } */ //************************************************************************************** inline ll read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } struct Line { int flag; double x,y1,y2; Line(double a,double b,double c,int d) {x=a;y1=b;y2=c;flag=d;} bool operator<(const Line &b)const {return x<b.x;} }; struct node { int l,r,flag; double ll,rr,len; void fun(int val) { flag+=val; if(flag==0) len=0; else len=rr-ll; } }; vector<double> y; vector<Line> line; map<double,int>H; node tree[maxn*4]; void build(int l,int r,int x) { tree[x].l=l,tree[x].r=r; tree[x].flag=0,tree[x].len=0; tree[x].ll=y[l],tree[x].rr=y[r]; if(l+1!=r) { int mid=(l+r)>>1; build(l,mid,x<<1); build(mid,r,x<<1|1); } } void updata(int st,int ed,int x,int val) { int l=tree[x].l,r=tree[x].r; if(l+1==r)tree[x].fun(val); else { int mid=(l+r)>>1; if(st<mid)updata(st,ed,x<<1,val); if(ed>mid)updata(st,ed,x<<1|1,val); tree[x].len=tree[x<<1].len+tree[x<<1|1].len; } } int main() { int n,cas=1; while(scanf("%d",&n)!=EOF) { if(n==0) break; line.clear(),y.clear(),H.clear(); memset(tree,0,sizeof(tree)); double x1,y1,x2,y2; for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line.push_back((Line){x1,y1,y2,1}); line.push_back((Line){x2,y1,y2,-1}); y.push_back(y1),y.push_back(y2); } sort(line.begin(),line.end()); sort(y.begin(),y.end()); y.erase(unique(y.begin(),y.end()),y.end()); for(int i=0;i<(int)y.size();i++) H[y[i]]=i; build(0,y.size()-1,1); double ans=0; for(int i=0;i<line.size();i++) { if(i!=0)ans+=(line[i].x-line[i-1].x)*tree[1].len; updata(H[line[i].y1],H[line[i].y2],1,line[i].flag); } printf("Test case #%d\nTotal explored area: %.2f\n\n",cas++,ans); } }