• POJ_1151 Atlantis(线段树)


      /*线段树扫描线问题,感觉可以作为这类问题的模板。问过师兄以后才过的。。。

    按y轴建线段树。
    */

    struct node{
    int l, r, c; //c表示被覆盖的情况,不为0表示被覆盖
    double sum, len; //len表示当前段的长度,sum表示有效长度。就是可以计入求面积的长度
    }node[N<<2];

    struct line{
    double x;  //记录x坐标
    double y1;
    double y2; //y1,y2用来记录平行y轴的线段的两个端点,
    int flag; //标记是左边的边还是右边的边
    }l[N];

    //My Code:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define L(t) t << 1
    #define R(t) t << 1 | 1

    using namespace std;

    const int N = 210;

    struct node{
    int l, r, c;
    double sum, len;
    }node[N<<2];

    struct line{
    double x;
    double y1;
    double y2;
    int flag;
    }l[N];

    double y[N];

    bool cmp(line a, line b){
    return a.x < b.x;
    }

    void creat(int t, int l, int r){
    node[t].l = l;
    node[t].r = r;
    node[t].c = 0;
    node[t].sum = 0;
    node[t].len = y[r] - y[l];
    if(l + 1 == r) return ;
    int mid = (l + r) >> 1;
    creat(L(t), l, mid);
    creat(R(t), mid, r);
    }

    void updata_sum(int t){
    if(node[t].c > 0) node[t].sum = node[t].len;
    else if(node[t].r != node[t].l + 1) node[t].sum = node[L(t)].sum + node[R(t)].sum;
    else node[t].sum = 0;
    }

    void updata(int t, int l, int r, int c){
    if(node[t].l == l && node[t].r == r){
    if(c) node[t].c ++;
    else node[t].c --;
    updata_sum(t);
    return ;
    }
    int mid = (node[t].l + node[t].r) >> 1;
    if(l >= mid) updata(R(t), l, r, c);
    else if(r <= mid) updata(L(t), l, r, c);
    else{
    updata(L(t), l, mid, c);
    updata(R(t), mid, r, c);
    }
    updata_sum(t);
    }

    int find(double key, int n){
    int l = 1, r = n, mid;
    while(l <= r){
    mid = (l + r) >> 1;
    if(y[mid] == key) return mid;
    else if(y[mid] > key) r = mid-1;
    else l = mid + 1;
    }
    return 0;
    }

    int main(){
    //freopen("data.in", "r", stdin);

    int t, n, m, i, cas = 0;
    double x1, x2, y1, y2, ans;
    while(scanf("%d", &t), t){
    m = 1;
    while(t--){
    scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
    y[m] = y1; l[m].x = x1; l[m].y1 = y1; l[m].y2 = y2; l[m].flag = 1; m++;
    y[m] = y2; l[m].x = x2; l[m].y1 = y1; l[m].y2 = y2; l[m].flag = 0; m++;
    }
    sort(y+1, y+m);
    sort(l+1, l+m, cmp);
    m--; n = 2;
    for(i = 1; i < m; i++)
    if(y[i] != y[i+1]) y[n++] = y[i+1];
    n--; creat(1, 1, n);
    for(ans = 0, i = 1; i < m; i++){
    if(l[i].flag) updata(1, find(l[i].y1, n), find(l[i].y2, n), 1);
    else updata(1, find(l[i].y1, n), find(l[i].y2, n), 0);
    ans += node[1].sum * (l[i+1].x - l[i].x);
    }
    printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++cas, ans);
    }
    return 0;
    }
  • 相关阅读:
    ES6相关概念及新增语法
    正则表达式
    递归
    高阶函数和闭包
    严格模式
    this指向
    递归
    严格模式
    函数内部的this指向
    函数的定义和调用
  • 原文地址:https://www.cnblogs.com/vongang/p/2225239.html
Copyright © 2020-2023  润新知