Covered Area
Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3756 accepted submission (s): 1846
Problem description is a number of rectangles on the plane. The area of the area covered by these rectangles is obtained at least twice.
The first line of input data is a positive integer T (1 <= T <= 100), representing the number of test data. the first row of each test data is a positive integer N (1 <= n <= 1000), representing the number of rectangles, followed by N rows of data, each row contains four floating point numbers, it indicates the coordinates of the upper left corner and lower right corner of a rectangle on the plane. The upper and lower sides of the rectangle are parallel to the X axis, and the left and right sides are parallel to the Y axis. the coordinate ranges from 0 to 100000.
Note: This question contains a large amount of input data. We recommend that you use scanf to read data.
Output for each group of test data, calculate the area of the area covered by these rectangles at least twice. The result is retained with two decimal places.
Sample Input
251 1 4 21 3 3 72 1.5 5 4.53.5 1.25 7.5 46 3 10 730 0 1 11 0 2 12 0 3 1
Sample output
7.630.00
----------------------Split line------------
Question:
Give you n rectangles, and find the area and
Ideas:
The original area is covered once, and this question requires more than two times. At the beginning, I did not distinguish between one coverage and two overwrites !!!
Area and perimeterIntra-range or operation!!!
CNT [] indicates the number of overwrites.
For a node:
1: CNT []> = 2 indicates that it is completely overwritten, so the length is equal to the Interval Length.
2: The leaf node is 0.
3: CNT [] = 1 indicates that it is completely overwritten. However, if its left and right subintervals overwrite more than once, then this time is exactly two or more times, therefore, we need to get the length of the left and right subtree after overwriting more than once.
4: CNT [] = 0 does not indicate that the data is not overwritten, but not completely overwritten. Therefore, if the data is overwritten more than twice, the length is equal to the length of the left and right subtree that covers more than two times.
Have a good understanding of the interval or operation !!!
Note:
Discretization: Sorting + unique deduplication function, returns the iterator of the last element.
Point-based line segment -- right endpoint-1
Calculation length -- the right endpoint of A linestring's return point + 1
After writing this, I will submit more than three questions: hdu3642 get the Treasury
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=2222;using namespace std;int cnt[maxn<<2];double sum1[maxn<<2],sum2[maxn<<2];double X[maxn+10];struct Seg { double l,r,h; int s; Seg() {}; Seg(double a,double b,double c,double d):l(a),r(b),h(c),s(d) {}; bool operator<(const Seg&cmp)const { return h<cmp.h; }};Seg ss[maxn+10];void push_up1(int rt,int l,int r){ if(cnt[rt]) sum1[rt]=X[r+1]-X[l]; else if(l==r) sum1[rt]=0; else sum1[rt]=sum1[rt<<1]+sum1[rt<<1|1];}void push_up2(int rt,int l,int r){ if(cnt[rt]>=2) sum2[rt]=X[r+1]-X[l]; else if(l==r) sum2[rt]=0; else if(cnt[rt]==1) sum2[rt]=sum1[rt<<1]+sum1[rt<<1|1]; else sum2[rt]=sum2[rt<<1]+sum2[rt<<1|1];}void update(int L,int R,int c,int l,int r,int rt){ if(L<=l&&r<=R) { cnt[rt]+=c; push_up1(rt,l,r); push_up2(rt,l,r); return ; } int m=(l+r)>>1; if(L<=m) update(L,R,c,lson); if(m<R) update(L,R,c,rson); push_up1(rt,l,r); push_up2(rt,l,r);}void solve(int m){ sort(X,X+m); sort(ss,ss+m); int k=unique(X,X+m)-X; memset(cnt,0,sizeof(cnt)); memset(sum1,0,sizeof(sum1)); memset(sum2,0,sizeof(sum2)); double ret=0; for(int i=0; i<m-1; ++i) { int l=lower_bound(X,X+k,ss[i].l)-X; int r=lower_bound(X,X+k,ss[i].r)-X-1; if(l<=r) update(l,r,ss[i].s,0,k-1,1); ret+=sum2[1]*(ss[i+1].h-ss[i].h); } printf("%.2lf\n",ret);}int main(){ int T,n; cin>>T; while(T--) { double x1,y1,x2,y2; int m=0; scanf("%d",&n); while(n--) { scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); X[m]=x1; ss[m++]=Seg(x1,x2,y1,1); X[m]=x2; ss[m++]=Seg(x1,x2,y2,-1); } solve(m); } return 0;}
HUD 1255 -- covered area (line segment tree + area and multiple times + discretization)