zoukankan      html  css  js  c++  java
  • 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
    ", _case, total);
        }
        return 0;
    }
  • 相关阅读:
    dp P1103 书本整理 洛谷
    dp 洛谷P1977 出租车拼车 线性dp
    Layui 在新标签中打开页面 / 模拟点击菜单
    布局 基础知识
    SpreadJS 生成报表
    ABP abp zreo 老版本 支持dotnet framework 4.0
    IIS 加载字体
    IIS 登录失败 该登陆名来自不受信任的域,不能与windows身份认证一起使用
    C# WebForm 打开默认页
    IIS 报错 Cannot open database "test4" requested by the login. The login failed. Login failed for user 'IIS APPPOOL est1'.
  • 原文地址:https://www.cnblogs.com/huoxiayu/p/4715091.html
Copyright © 2011-2022 走看看