`
java-mans
  • 浏览: 11341398 次
文章分类
社区版块
存档分类
最新评论

HDU 1255 覆盖的面积 线段树 扫描线

 
阅读更多

还是先离散化坐标,然后用线段树扫描线

其中sum代表被覆盖过一次的长度,sum2代表被覆盖过2次及以上的长度。

然后注意pushup操作比较麻烦。

/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 2222
#define MAXM 164444
#define INF 100000000
#define eps 1e-7
#define L(X) X<<1
#define R(X) X<<1|1
using namespace std;

struct node
{
    int left, right, mid;
    int cnt;
    double sum, sum2;
}tree[4 * MAXN];

struct Seg
{
    double h, l, r;
    int s;
    Seg(){}
    Seg(double a, double b, double c, int d) {l = a; r = b; h = c; s = d;}
    bool operator <(const Seg &cmp)const{
    return h < cmp.h;
    }
}seg[2 * MAXN];
double x[MAXN];
int ct ;
void up(int C)
{
    if(tree[C].cnt > 1)
    {
        tree[C].sum = 0.0;
        tree[C].sum2 = x[tree[C].right + 1] - x[tree[C].left];
    }
    else if(tree[C].cnt == 1)
    {
        if(tree[C].left == tree[C].right){tree[C].sum = x[tree[C].right + 1] - x[tree[C].left]; tree[C].sum2 = 0;}
        else
        {
            tree[C].sum2 = tree[L(C)].sum + tree[L(C)].sum2 + tree[R(C)].sum + tree[R(C)].sum2;
            tree[C].sum = x[tree[C].right + 1] - x[tree[C].left] - tree[C].sum2;
        }
    }
    else
    {
        if(tree[C].left == tree[C].right) tree[C].sum = tree[C].sum2 = 0.0;
        else
        {
            tree[C].sum = tree[L(C)].sum + tree[R(C)].sum;
            tree[C].sum2 = tree[L(C)].sum2 + tree[R(C)].sum2;
        }
    }
}

void make_tree(int s, int e, int C)
{
    tree[C].left = s;
    tree[C].right = e;
    tree[C].mid = (s + e) >> 1;
    tree[C].cnt = 0;
    tree[C].sum = 0.0;
    tree[C].sum2 = 0.0;
    if(s == e) return;
    make_tree(s, tree[C].mid, L(C));
    make_tree(tree[C].mid + 1, e, R(C));
}

void update(int s, int e, int v, int C)
{
    if(tree[C].left >= s && tree[C].right <= e)
    {
        tree[C].cnt += v;
        up(C);
        return;
    }
    if(tree[C].mid >= s) update(s, e, v, L(C));
    if(tree[C].mid < e) update(s, e, v, R(C));
    up(C);
}

int bin(double v, int bound)
{
    int low = 0, high = bound - 1;
    while(low <= high)
    {
        int mid = (low + high) >> 1;
        if(x[mid] == v) return mid;
        if(x[mid] < v) low = mid + 1;
        else high = mid - 1;
    }
    return -1;
}

int main()
{
    int T, n;
    scanf("%d", &T);
    while(T--)
    {
        ct = 0;
        scanf("%d", &n);
        int m = 0;
        double a, b, c, d;
        while(n--)
        {
            scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
            x[m] = a;
            seg[m++] = Seg(a, c, b, 1);
            x[m] = c;
            seg[m++] = Seg(a, c, d, -1);
        }
        sort(x, x + m);
        sort(seg, seg + m);
        int k = unique(x, x + m) - x;
        make_tree(0, k - 1, 1);
        double ans = 0;
        for(int i = 0; i < m - 1; i ++)
        {
            int l = bin(seg[i].l, k);
            int r = bin(seg[i].r, k) - 1;
            update(l, r, seg[i].s, 1);
            ans += tree[1].sum2 * (seg[i + 1].h - seg[i].h);
        }
        printf("%.2f\n", ans);
    }
    return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics