标签:
题目链接:
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+10; int n; double rec[2*N],xa,xb,ya,yb; struct Tree { int l,r,cover; double sum; }; Tree tree[4*N]; struct Line { double l,r,h; int flag; }; Line line[4*N]; int cmp(Line x,Line y) { return x.h<y.h; } void build(int node,int L,int R) { tree[node].sum=0; tree[node].cover=0; tree[node].l=L; tree[node].r=R; if(L>=R)return ; int mid=(L+R)>>1; build(2*node,L,mid); build(2*node+1,mid+1,R); } void Pushup(int node) { if(tree[node].cover) { tree[node].sum=rec[tree[node].r+1]-rec[tree[node].l]; } else { if(tree[node].l!=tree[node].r) tree[node].sum=tree[2*node].sum+tree[2*node+1].sum; else tree[node].sum=0; } } void update(int node,int L,int R,int x) { if(L<=tree[node].l&&R>=tree[node].r) { tree[node].cover+=x; Pushup(node); return ; } int mid=(tree[node].l+tree[node].r)>>1; if(R<=mid)update(2*node,L,R,x); else if(L>mid)update(2*node+1,L,R,x); else { update(2*node,L,mid,x); update(2*node+1,mid+1,R,x); } Pushup(node); } map<double,int>mp; int main() { int Case=1; while(1) { scanf("%d",&n); if(n==0)break; printf("Test case #%d\n",Case++); int cnt=1; for(int i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&xa,&ya,&xb,&yb); rec[cnt]=line[cnt].l=xa; line[cnt].r=xb; line[cnt].flag=1; line[cnt++].h=ya; line[cnt].l=xa; rec[cnt]=line[cnt].r=xb; line[cnt].h=yb; line[cnt++].flag=-1; } sort(line+1,line+cnt,cmp); sort(rec+1,rec+cnt); int num=2; for(int i=2;i<cnt;i++) { if(rec[i]!=rec[i-1])rec[num++]=rec[i]; } for(int i=1;i<num;i++) { mp[rec[i]]=i; } build(1,1,num-1); double ans=0; for(int i=1;i<cnt-1;i++) { int fx=mp[line[i].l]; int fy=mp[line[i].r]; update(1,fx,fy-1,line[i].flag); ans+=tree[1].sum*(line[i+1].h-line[i].h); } printf("Total explored area: %.2lf\n",ans); printf("\n"); } return 0; }
hdu-1542 Atlantis(离散化+线段树+扫描线算法)
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5366207.html