标签:des style blog http ar io color os sp
传送门:HDU_1542
2 10 10 20 20 15 15 25 25.5 0
Test case #1 Total explored area: 180.00
题意:求矩形并。
思路:线段树扫描线。
线段树维护左右区间大小,矩形下边标记为1,上边标记为-1,从下往上扫描。
参考:lv
代码:
/************************************************ * Author: Ac_sorry * File: * Create Date: * Motto: One heart One life * CSDN: http://blog.csdn.net/code_or_code *************************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<cstdlib> #include<vector> #include<string> #include<map> #include<utility> #include<queue> #include<stack> #define INF 0x3f3f3f3f #define MOD 1000000007 #define seed_ 131 #define eps 1e-8 #define mem(a,b) memset(a,b,sizeof a) #define flag(i) tree[i].flag #define ls(i) tree[i].ls #define rs(i) tree[i].rs using namespace std; typedef long long LL; const int N=220; struct Edge{ double lx,rx,y; int flag; void init(double a,double b,double c,int d) { lx=a;rx=b;y=c;flag=d; } bool operator <(const Edge e) const { return y<e.y; } }edge[N<<2]; struct Tree{ int ls,rs; double len; int flag; }tree[N<<2]; double X[N<<2]; void pushup(int p) { if(flag(p)) tree[p].len=X[rs(p)]-X[ls(p)]; else if(rs(p)==ls(p)+1) tree[p].len=0; else tree[p].len=tree[p<<1].len+tree[p<<1|1].len; } void build(int p,int l,int r) { tree[p].len=0; flag(p)=0; ls(p)=l; rs(p)=r; if(l+1==r) return; int m=(l+r)>>1; build(p<<1,l,m); build(p<<1|1,m,r); } void update(int p,int l,int r,int v) { //cout<<"---\n"; if(ls(p)>r||rs(p)<l) return; if(l<=ls(p)&&rs(p)<=r) { flag(p)+=v; pushup(p); return; } //int m=(ls(p)+rs(p))>>1; //if(l<=m) update(p<<1,l,r,v); //if(r>m) update(p<<1|1,l,r,v); pushup(p); } int main() { //int T;scanf("%d",&T); int AC=0; int n; while(scanf("%d",&n)==1,n) { double x1,y1,x2,y2; for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); edge[i].init(x1,x2,y1,-1); edge[n+i].init(x1,x2,y2,1); X[i]=x1;X[i+n]=x2; } n<<=1; sort(edge+1,edge+1+n); sort(X+1,X+1+n); int cnt=unique(X+1,X+1+n)-(X+1); build(1,1,cnt); double ans=0; printf("Test case #%d\n",++AC); for(int i=1;i<=n;i++) { if(i!=1) ans+=(edge[i].y-edge[i-1].y)*tree[1].len; int mp_l,mp_r; mp_l=lower_bound(X+1,X+1+cnt,edge[i].lx)-X; mp_r=lower_bound(X+1,X+1+cnt,edge[i].rx)-X; //cout<<cnt<<mp_l<<"--"<<mp_r<<endl; update(1,mp_l,mp_r,edge[i].flag); } printf("Total explored area: %.2f\n\n",ans); } return 0; }
标签:des style blog http ar io color os sp
原文地址:http://blog.csdn.net/code_or_code/article/details/41971471