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; }

    扫描线

  • 相关阅读:
    使用jquery.mobile和WebSQL实现记事本功能
    jqprint的网页打印,打印预览可以包含图片
    JDBC--处理Blob
    JDBC--DAO设计模式
    JDBC--使用beanutils工具类操作JavaBean
    JDBC--利用反射及JDBC元数据编写通用的查询方法
    JDBC--PreparedStatement使用
    JDBC--Statement使用
    JDBC--获取数据库连接
    Oracle笔记--PL/SQL(Procedure Language & Structured Query Language)
  • 原文地址:https://www.cnblogs.com/yangyongqian/p/3906452.html
Copyright © 2011-2022 走看看