Solution report
Question Portal
Question:
Calculate the area.
Ideas:
No.
Only the length of a line segment whose coverage is greater than 2 should be maintained in the line segment tree.
It won't be updated. I read what others have written and understood.
Sum1 and sum2 are used to indicate the length of the interval with 1 overwrite and the interval with 2 overwrite respectively.
When updating, you must update sum1 and sum2;
If the range is overwritten
Sum1 indicates the actual Interval Length. If it is overwritten once, sum2 indicates the sum1 and sum1 of the left and right Subtrees. If it is overwritten twice, it indicates the actual Interval Length.
If not overwritten, the sum of left and right Subtrees is equal.
#include <algorithm>#include <iostream>#include <cstring>#include <cstdio>using namespace std;struct Seg { double lx,rx,h; int v; friend bool operator < (Seg a,Seg b) { return a.h<b.h; }} seg[2010];double _hash[2010],sum1[100000],sum2[100000];int lz[100000];void push_down(int rt,int l,int r) { if(lz[rt]) { lz[rt<<1]+=lz[rt]; lz[rt<<1|1]+=lz[rt]; lz[rt]=0; }}void push_up(int rt,int l,int r) { if(lz[rt]) { sum1[rt]=_hash[r+1]-_hash[l]; if(lz[rt]>=2) sum2[rt]=_hash[r+1]-_hash[l]; else if(lz[rt]==1) sum2[rt]=sum1[rt<<1]+sum1[rt<<1|1]; } else sum1[rt]=sum1[rt<<1]+sum1[rt<<1|1], sum2[rt]=sum2[rt<<1]+sum2[rt<<1|1];}void update(int rt,int l,int r,int ql,int qr,int v) { if(ql>r||qr<l)return ; if(ql<=l&&r<=qr) { lz[rt]+=v; push_up(rt,l,r); return ; } int mid=(l+r)>>1; update(rt<<1,l,mid,ql,qr,v); update(rt<<1|1,mid+1,r,ql,qr,v); push_up(rt,l,r);}int main() { int t,n,i,j,ql,qr; double lx,ly,rx,ry; scanf("%d",&t); while(t--) { double ans=0; scanf("%d",&n); memset(lz,0,sizeof(lz)); memset(sum1,0,sizeof(sum1)); memset(sum2,0,sizeof(sum2)); for(i=0; i<n; i++) { scanf("%lf%lf%lf%lf",&lx,&ly,&rx,&ry); _hash[i]=lx; _hash[i+n]=rx; seg[i].lx=lx,seg[i].rx=rx,seg[i].h=ly,seg[i].v=1; seg[i+n].lx=lx,seg[i+n].rx=rx,seg[i+n].h=ry,seg[i+n].v=-1; } sort(_hash,_hash+n*2); sort(seg,seg+n*2); int m=unique(_hash,_hash+n*2)-_hash; for(i=0; i<n*2-1; i++) { ql=lower_bound(_hash,_hash+m,seg[i].lx)-_hash; qr=lower_bound(_hash,_hash+m,seg[i].rx)-_hash-1; if(i)ans+=sum2[1]*(seg[i].h-seg[i-1].h); update(1,0,m-1,ql,qr,seg[i].v); } printf("%.2lf\n",ans+0.000001); } return 0;}
Covered Area
Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3658 accepted submission (s): 1787
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
Authorignatius. L & Weigang Lee
Recommendignatius. L