hdu 3634 线段树求矩形面积并

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
typedef long long ll;
const int N = 1000;
int y[N];

struct Line
{
    int x, y1, y2, flag;
} line[N];

struct Node
{
    int l, r, cover, lf, rf, len;
} node[N];

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

void length ( int u )
{
    if ( node[u].cover > 0 )
    {
        node[u].len = node[u].rf - node[u].lf;
        return;
    }
    else if ( node[u].l + 1 == node[u].r )
    {
        node[u].len = 0; 
    }
    else
    {
        node[u].len = node[L(u)].len + node[R(u)].len;
    }
}

void build ( int u, int l, int r )
{
    node[u].l = l; node[u].r = r;
    node[u].lf = y[l]; node[u].rf = y[r];
    node[u].len = node[u].cover = 0;
    if ( l + 1 == r ) return;
    int mid = ( l + r ) / 2;
    build ( L(u), l, mid );
    build ( R(u), mid, r );
}

void update ( int u, Line e )
{
    if ( e.y1 == node[u].lf && e.y2 == node[u].rf )
    {
        node[u].cover += e.flag;
        length ( u );
        return;
    }
    if ( e.y1 >= node[R(u)].lf )
    {
        update ( R(u), e );
    }
    else if ( e.y2 <= node[L(u)].rf )
    {
        update ( L(u), e );
    }
    else
    {
        Line temp = e;
        temp.y2 = node[L(u)].rf;
        update ( L(u), temp );
        temp = e;
        temp.y1 = node[R(u)].lf;
        update ( R(u), temp );
    }
    length ( u );
}

const int M = 50;

struct Stc 
{
    int x1, y1, x2, y2, value;
    bool operator < ( const Stc & o ) const 
    {
        return value > o.value;
    }
} stc[M];

int main()
{
    int t;
    scanf("%d", &t);
    for ( int _case = 1; _case <= t; _case++ )
    {
        int n;
        scanf("%d", &n);
        for ( int i = 1; i <= n; i++ )
        {
            scanf("%d%d%d%d%d", &stc[i].x1, &stc[i].y1, &stc[i].x2, &stc[i].y2, &stc[i].value);
        }
        sort( stc + 1, stc + 1 + n );
        ll total = 0, pre = 0;
        for ( int i = 1; i <= n; i++ )
        {
            int p = 1;
            for ( int j = 1; j <= i; j++ )
            {
                line[p].x = stc[j].x1;
                line[p].y1 = stc[j].y1;
                line[p].y2 = stc[j].y2;
                line[p].flag = 1;
                y[p] = stc[j].y1;
                p++;
                line[p].x = stc[j].x2;
                line[p].y1 = stc[j].y1;
                line[p].y2 = stc[j].y2;
                line[p].flag = -1;
                y[p] = stc[j].y2;
                p++;
            }
            sort ( line + 1, line + p, cmp );
            sort ( y + 1, y + p );
            build ( 1, 1, p - 1 );
            update ( 1, line[1] );
            ll cur = 0;
            for ( int k = 2; k < p; k++ )
            {
                cur += node[1].len * ( line[k].x - line[k - 1].x );
                update ( 1, line[k] );
            }
            total += ( cur - pre ) * stc[i].value;
            pre = cur;
        }
        printf("Case %d: %I64d\n", _case, total);
    }
    return 0;
}

 

posted @ 2015-08-09 14:13  hxy_has_been_used  阅读(181)  评论(0编辑  收藏  举报