标签:atl memory nal tle 次数 scanf ant must fir
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18287 Accepted Submission(s): 7417
离散化+线段树+扫描线
#include <bits/stdc++.h> #define maxn 10005 struct Line { double x,y_down,y_up; int flag; bool operator <(const Line &a) { return x<a.x; } }line[maxn]; struct Tree { double y_down,y_up; double x; int cover;//用以表示加进线段树的线段次数 bool flag; }tree[maxn*100]; int n; double xx1,yy1,xx2,yy2; int cnt=0; double y[2*maxn]; void build(int l,int r,int rt) { tree[rt].x=-1; tree[rt].cover=0; tree[rt].y_down=y[l]; tree[rt].y_up=y[r]; tree[rt].flag=false; if(l+1==r) { tree[rt].flag=true; return; } int mid=(l+r)>>1; build(l,mid,rt*2); build(mid,r,rt*2+1); } double insert_tree(int rt,double x,double l,double r,int flag)//flag表示是左边还是右边 { if(r<=tree[rt].y_down||l>=tree[rt].y_up) { return 0; } if(tree[rt].flag) { if(tree[rt].cover>0) { double temp_x=tree[rt].x; double ans=(x-temp_x)*(tree[rt].y_up-tree[rt].y_down); tree[rt].x=x; tree[rt].cover+=flag; return ans; } else { tree[rt].cover+=flag; tree[rt].x=x; return 0; } } double ans1,ans2; ans1=insert_tree(rt*2,x,l,r,flag); ans2=insert_tree(rt*2+1,x,l,r,flag); return ans1+ans2; } int main() { int n,i; int cas=0; while(std::cin>>n&&n) { cnt=1; for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&xx1,&yy1,&xx2,&yy2); y[cnt]=yy1; line[cnt].x=xx1; line[cnt].y_down=yy1; line[cnt].y_up=yy2; line[cnt].flag=1; cnt++; y[cnt]=yy2; line[cnt].x=xx2; line[cnt].y_down=yy1; line[cnt].y_up=yy2; line[cnt].flag=-1; cnt++; } std::sort(y+1,y+cnt); std::sort(line+1,line+cnt); build(1,cnt-1,1); double ans=0; for(int i=1;i<cnt;i++) { //std::cout<<"gg"<<std::endl; ans+=insert_tree(1,line[i].x,line[i].y_down,line[i].y_up,line[i].flag); } printf("Test case #%d\nTotal explored area: %.2f\n\n", ++cas, ans); } return 0; }
标签:atl memory nal tle 次数 scanf ant must fir
原文地址:https://www.cnblogs.com/zyf3855923/p/9451266.html