Solution report
Question Portal
Question:
Calculates the rectangle and area.
Ideas:
Discrete + line segment tree + scanned line.
#include <algorithm>#include <iostream>#include <cstring>#include <cstdio>using namespace std;struct Seg { int v; double h,lx,rx; friend bool operator < (Seg a,Seg b) { return a.h<b.h; }} seg[4000];struct R { double lx,ly,rx,ry;} re[1100];int lz[401000];double _hash[101000],sum[401000];void push_up(int rt,int l,int r) { if(lz[rt])sum[rt]=_hash[r+1]-_hash[l]; else if(l==r)sum[rt]=0; else sum[rt]=sum[rt*2]+sum[rt*2+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); //sum[rt]+=_hash[r+1]-_hash[l]; return ; } int mid=(l+r)/2; update(rt*2,l,mid,ql,qr,v); update(rt*2+1,mid+1,r,ql,qr,v); push_up(rt,l,r);}int main() { int n,i,j,k=1; while(~scanf("%d",&n)) { if(!n)break; int cnt=0; memset(lz,0,sizeof(lz)); memset(sum,0,sizeof(sum)); double ans=0; for(i=0; i<n; i++) { scanf("%lf%lf%lf%lf",&re[i].lx,&re[i].ly,&re[i].rx,&re[i].ry); _hash[i]=re[i].lx; _hash[i+n]=re[i].rx; seg[cnt].h=re[i].ly; seg[cnt].lx=re[i].lx; seg[cnt].rx=re[i].rx; seg[cnt++].v=1; seg[cnt].h=re[i].ry; seg[cnt].lx=re[i].lx; seg[cnt].rx=re[i].rx; seg[cnt++].v=-1; } sort(seg,seg+cnt); sort(_hash,_hash+n*2); int m=unique(_hash,_hash+n*2)-_hash; for(i=0; i<cnt-1; i++) { int l=lower_bound(_hash,_hash+m,seg[i].lx)-_hash; int r=lower_bound(_hash,_hash+m,seg[i].rx)-_hash-1; update(1,0,m-1,l,r,seg[i].v); ans+=sum[1]*(seg[i+1].h-seg[i].h); } printf("Test case #%d\nTotal explored area: ",k++); printf("%.2lf\n",ans); printf("\n"); } return 0;}
Atlanta
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6942 accepted submission (s): 3046
Problem descriptionthere are several generated ent Greek texts that contain descriptions of the fabled island Atlanta. some of these texts even include maps of parts of the island. but unfortunately, these maps describe different regions of Atlanta. your friend Bill has to know the total area for which maps exist. you (unwisely) volunteered to write a program that calculates this quantity.
Inputthe input file consists of several test cases. each test case starts with a line containing a single integer N (1 <= n <= 100) of available maps. the N following lines describe one map each. each of these lines contains four numbers x1; Y1; x2; Y2 (0 <= X1 <X2 <= 100000; 0 <= Y1 <Y2 <= 100000), not necessarily integers. the values (x1; Y1) and (X2; Y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don't process it.
Outputfor each test case, your program shocould output one section. the first line of each section must be "Test Case # K", where k is the number of the test case (starting with 1 ). the second one must be "total occupied ed area: a", where A is the total occupied ed area (I. e. the area of the Union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
Sample Input
210 10 20 2015 15 25 25.50
Sample output
Test case #1Total explored area: 180.00
Sourcemid-Central European Regional Contest 2000