zoukankan      html  css  js  c++  java
  • B


    #include <stdio.h> #include <iostream> #include <algorithm> using namespace std; struct node { int l,r; int c; double cnt,lf,rf; }seg[603]; double y[201]; struct Line { double x,y1,y2; int f; }line[201]; bool cmp(Line a,Line b) { return a.x<b.x; } void Build(int t,int l,int r) { seg[t].l = l; seg[t].r = r; seg[t].cnt = seg[t].c = 0; seg[t].lf = y[l]; seg[t].rf = y[r]; if(l + 1 == r) return ; int mid = (l + r)/2; Build(2*t,l,mid); Build(2*t+1,mid,r); } void calen(int t) { if(seg[t].c>0) { seg[t].cnt = seg[t].rf-seg[t].lf; return ; } if(seg[t].l+1 == seg[t].r)seg[t].cnt = 0; else seg[t].cnt = seg[2 *t].cnt+seg[2 *t+1].cnt; } void update(int t,Line e) { if(e.y1 == seg[t].lf && e.y2 == seg[t].rf) { seg[t].c+=e.f; calen(t); return ; } if(e.y2<=seg[2 *t].rf)update(2 *t,e); else if(e.y1>=seg[2 * t+1].lf)update(2 *t+1,e); else { Line tmp = e; tmp.y2 = seg[2 *t].rf; update(2 * t,tmp); tmp = e; tmp.y1 = seg[t<<1|1].lf; update(2 * t + 1,tmp); } calen(t); } int main() { int i,n,t,iCase = 0; double x1,y1,x2,y2; while(scanf("%d",&n),n) { iCase++; t = 1; for(i = 1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line[t].x = x1; line[t].y1 = y1; line[t].y2 = y2; line[t].f = 1; y[t] = y1; t++; line[t].x = x2; line[t].y1 = y1; line[t].y2 = y2; line[t].f = -1; y[t] = y2; t++; } sort(line+1,line+t,cmp); sort(y+1,y+t); Build(1,1,t-1); update(1,line[1]); double res = 0; for(i = 2;i<t;i++) { res+=seg[1].cnt*(line[i].x - line[i-1].x); update(1,line[i]); } printf("Test case #%d Total explored area: %.2f ",iCase,res); } return 0; }

    扫描线

  • 相关阅读:
    nginx设置缓存策略
    http缓存原理理解
    promise教程
    vue项目发布到github演示
    vue路由嵌套子路由不渲染
    hihocoder 1403 1407 1415 后缀数组*3 最长公共子串/最长不重叠重复出现的子串/最长k次重复子串
    hdu 4691 后缀数组+rmq 求子串lcp
    hdu1506+ luogu 1440 单调栈/单调队列裸题
    codeforces 873F 后缀数组+单调栈(fastio)
    HihoCoder
  • 原文地址:https://www.cnblogs.com/yangyongqian/p/3906452.html
Copyright © 2011-2022 走看看