Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4135 Accepted Submission(s): 2041
Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.
Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.
注意:本题的输入数据较多,推荐使用scanf读入数据.
Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
Sample Input
2 5 1 1 4 2 1 3 3 7 2 1.5 5 4.5 3.5 1.25 7.5 4 6 3 10 7 3 0 0 1 1 1 0 2 1 2 0 3 1
Sample Output
7.63 0.00
Author
Ignatius.L & weigang Lee
Recommend
Ignatius.L | We have carefully selected several similar problems for you: 1540 1828 1823 3016 2871
我对我的线段树很有自信,直接cin
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; double hs[2010]; struct Tree { int l,r,num; double val,len; }tree[2000<<2]; void create(int l,int r,int k) { tree[k].l=l; tree[k].r=r; tree[k].num=0; tree[k].val=tree[k].len=0; if(l+1==r) return; int m=l+r>>1; create(l,m,k<<1); create(m,r,k<<1|1); } void update(int l,int r,int k,int flag) { if(tree[k].l==l&&tree[k].r==r) { tree[k].num+=flag; if(tree[k].num==0) { tree[k].val=l+1==r?0:tree[k<<1].val+tree[k<<1|1].val; tree[k].len=l+1==r?0:tree[k<<1].len+tree[k<<1|1].len; } else { tree[k].val=hs[r]-hs[l]; if(tree[k].num==1) tree[k].len=l+1==r?0:tree[k<<1].val+tree[k<<1|1].val; else tree[k].len=hs[r]-hs[l]; } return; } int m=tree[k].l+tree[k].r>>1; if(r<=m) update(l,r,k<<1,flag); else if(l>=m) update(l,r,k<<1|1,flag); else { update(l,m,k<<1,flag); update(m,r,k<<1|1,flag); } if(tree[k].num==0) { tree[k].val=tree[k<<1].val+tree[k<<1|1].val; tree[k].len=tree[k<<1].len+tree[k<<1|1].len; } else if(tree[k].num==1) tree[k].len=tree[k<<1].val+tree[k<<1|1].val; } struct Seg { int flag; double l,r,y; bool operator <(Seg one)const { return y<one.y; } }seg[2010]; int main() { int T; cin>>T; while(T--) { int n; cin>>n; vector<double>box; for(int i=0;i<n;i++) { double x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; seg[i<<1].l=seg[i<<1|1].l=x1; seg[i<<1].r=seg[i<<1|1].r=x2; seg[i<<1].y=y1; seg[i<<1|1].y=y2; seg[i<<1].flag=1; seg[i<<1|1].flag=-1; box.push_back(x1); box.push_back(x2); } sort(box.begin(),box.end()); box.erase(unique(box.begin(),box.end()),box.end()); for(int i=0;i<box.size();i++) hs[i+1]=box[i]; n*=2; sort(seg,seg+n); double ans=0; create(1,box.size(),1); for(int i=0;i<n;i++) { int l=1+lower_bound(box.begin(),box.end(),seg[i].l)-box.begin(); int r=1+lower_bound(box.begin(),box.end(),seg[i].r)-box.begin(); if(i!=n-1) { update(l,r,1,seg[i].flag); ans+=tree[1].len*(seg[i+1].y-seg[i].y); } } printf("%.2f\n",ans); } }
时间: 2024-10-21 17:02:03