标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 21734 | Accepted: 8179 |
Description
Input
Output
Sample Input
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00
Source
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> using namespace std; const int N=205; #define m (l+r)/2 #define lson o<<1,l,m #define rson o<<1|1,m+1,r #define lc o<<1 #define rc o<<1|1 inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } int n,cnt=0; double x1,y1,x2,y2,mp[N]; struct seg{ double l,r,h; int f;//1 or -1 seg(double a=0,double b=0,double c=0,int d=0):l(a),r(b),h(c),f(d){} bool operator <(const seg &r)const{return h<r.h;} }a[N]; struct node{ double sum; int cov; }t[N<<2]; inline int Bin(double v){ int l=1,r=cnt; while(l<=r){ int mid=(l+r)>>1; if(mp[mid]==v) return mid; else if(v<mp[mid]) r=mid-1; else l=mid+1; } return -1; } inline void pushUp(int o,int l,int r){ if(t[o].cov) t[o].sum=mp[r+1]-mp[l]; else if(l==r) t[o].sum=0; else t[o].sum=t[lc].sum+t[rc].sum; } void update(int o,int l,int r,int ql,int qr,int v){ if(ql<=l&&r<=qr){ t[o].cov+=v; pushUp(o,l,r); }else{ if(ql<=m) update(lson,ql,qr,v); if(m<qr) update(rson,ql,qr,v); pushUp(o,l,r); } } int cas=0; int main(int argc, const char * argv[]) { while((n=read())){ double ans=0; for(int i=1;i<=n;i++){ scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); a[i*2-1]=seg(x1,x2,y1,1); a[i*2]=seg(x1,x2,y2,-1); mp[2*i-1]=x1; mp[2*i]=x2; } sort(mp+1,mp+1+2*n); sort(a+1,a+1+2*n); cnt=0;mp[++cnt]=mp[1]; for(int i=2;i<=2*n;i++) if(mp[i]!=mp[i-1]) mp[++cnt]=mp[i]; memset(t,0,sizeof(t)); for(int i=1;i<=2*n-1;i++){//最后一个不用 int ql=Bin(a[i].l),qr=Bin(a[i].r)-1; if(ql<=qr) update(1,1,cnt,ql,qr,a[i].f); ans+=t[1].sum*(a[i+1].h-a[i].h); } printf("Test case #%d\n",++cas); printf("Total explored area: %.2f\n\n",ans); } return 0; }
POJ1151Atlantis 矩形面积并[线段树 离散化 扫描线]
标签:
原文地址:http://www.cnblogs.com/candy99/p/5958587.html