hdu 1255 覆盖的面积 线段树求面积的交 我感觉有点难啊~~第一次

#include <cstdio>#include <algorithm>using namespace std ;double y[2010] ;struct Line{double x,y_up,y_down ;int mark;}line[2010];struct node{double x;double y_up,y_down;double cover;bool isLeaf ;}st[400100];void build(int l , int r , int pos){st[pos].x = -1 ;st[pos].y_down = y[l] ;st[pos].y_up = y[r] ;st[pos].cover = 0 ;st[pos].isLeaf = false ;if(l+1 == r){st[pos].isLeaf = true ;return ;}int mid = (l+r)>>1 ;build(l,mid,pos<<1) ;build(mid,r,pos<<1|1) ;}double insert(double x , double y_down , double y_up , int mark , int pos){if(st[pos].y_down>=y_up || st[pos].y_up<=y_down){return 0 ;}if(st[pos].isLeaf){if(st[pos].cover>1){double tempx = st[pos].x ;double area = (x-tempx)*(st[pos].y_up-st[pos].y_down) ;st[pos].x = x ;st[pos].cover += mark ;return area ;}else{st[pos].cover += mark ;st[pos].x = x ;return 0 ;}}return insert(x,y_down,y_up,mark,pos<<1)+insert(x,y_down,y_up,mark,pos<<1|1) ;}bool cmp(const Line &a , const Line &b){return a.x<b.x ;}int main(){int t ;scanf("%d",&t) ;while(t–){int n ;scanf("%d",&n) ;int index = 0 ;for(int i = 0 ; i < n ; ++i){double x1 , x2 , y1 , y2 ;scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2) ;line[index].x = x1 ;line[index].y_down = y1 ;y[index] = y1 ;line[index].y_up = y2 ;line[index++].mark = 1 ;line[index].x = x2 ;line[index].y_down = y1 ;line[index].y_up = y2 ;y[index] = y2 ;line[index++].mark = -1 ;}sort(line,line+index,cmp);sort(y,y+index) ;build(0,index-1,1) ;double s = 0.0 ;for(int i = 0 ; i < index ; ++i){s += insert(line[i].x,line[i].y_down,line[i].y_up,line[i].mark,1) ;}printf("%.2lf\n",s) ;}return 0 ;}与君共勉

,找一个让心里安静和干净的地方,

hdu 1255 覆盖的面积 线段树求面积的交 我感觉有点难啊~~第一次

相关文章:

你感兴趣的文章:

标签云: