zoukankan      html  css  js  c++  java
  • 1645: [Usaco2007 Open]City Horizon 城市地平线

    1645: [Usaco2007 Open]City Horizon 城市地平线

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 315  Solved: 157
    [Submit][Status]

    Description

    Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings. The entire horizon is represented by a number line with N (1 <= N <= 40,000) buildings. Building i's silhouette has a base that spans locations A_i through B_i along the horizon (1 <= A_i < B_i <= 1,000,000,000) and has height H_i (1 <= H_i <= 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.

    N个矩形块,交求面积并.

    Input

    * Line 1: A single integer: N

    * Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: A_i, B_i, and H_i

    Output

    * Line 1: The total area, in square units, of the silhouettes formed by all N buildings

    Sample Input

    4
    2 5 1
    9 10 4
    6 8 2
    4 6 3

    Sample Output

    16

    OUTPUT DETAILS:

    The first building overlaps with the fourth building for an area of 1
    square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3 - 1 = 16.

    HINT

    Source

    题解:
    这题的思路比较巧妙。
    全部的图形被分成了2*n-1个矩形,所以只要用线段树维护每一个矩形的高即可,取max
    代码:(copy)
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #define ll long long
     7 #define inf 10000000000
     8 using namespace std;
     9 inline ll read()
    10 {
    11     int x=0,f=1;char ch=getchar();
    12     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    13     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    14     return x*f;
    15 }
    16 int n;
    17 int x[40005],y[40005],val[40005],disc[80005];
    18 struct seg{int l,r,mx,tag;}t[320005];
    19 int find(int x)
    20 {
    21     int l=1,r=2*n;
    22     while(l<=r)
    23     {
    24         int mid=(l+r)>>1;
    25         if(disc[mid]<x)l=mid+1;
    26         else if(disc[mid]==x)return mid;
    27         else r=mid-1;
    28     }
    29 }
    30 void pushdown(int k)
    31 {
    32     if(t[k].l==t[k].r)return;
    33     int tag=t[k].tag;t[k].tag=0;
    34     if(tag)
    35     {
    36         t[k<<1].tag=max(t[k<<1].tag,tag);
    37         t[k<<1|1].tag=max(t[k<<1|1].tag,tag);
    38         t[k<<1].mx=max(t[k<<1].mx,tag);
    39         t[k<<1|1].mx=max(t[k<<1|1].mx,tag);
    40     }
    41 }
    42 void build(int k,int l,int r)
    43 {
    44     t[k].l=l;t[k].r=r;
    45     if(l==r)return;
    46     int mid=(l+r)>>1;
    47     build(k<<1,l,mid);build(k<<1|1,mid+1,r);
    48 }
    49 void update(int k,int x,int y,int val)
    50 {
    51     pushdown(k);
    52     int l=t[k].l,r=t[k].r;
    53     if(l==x&&y==r)
    54     {
    55         t[k].tag=val;t[k].mx=max(t[k].mx,val);
    56         return;
    57     }
    58     int mid=(l+r)>>1;
    59     if(y<=mid)update(k<<1,x,y,val);
    60     else if(x>mid)update(k<<1|1,x,y,val);
    61     else
    62     {
    63         update(k<<1,x,mid,val);update(k<<1|1,mid+1,y,val);
    64     }
    65 }
    66 int query(int k,int x)
    67 {
    68     pushdown(k);
    69     int l=t[k].l,r=t[k].r;
    70     if(l==r)return t[k].mx;
    71     int mid=(l+r)>>1;
    72     if(x<=mid)return query(k<<1,x);
    73     else return query(k<<1|1,x);
    74 }
    75 int main()
    76 {
    77     n=read();build(1,1,n<<1);
    78     for(int i=1;i<=n;i++)
    79     {
    80         x[i]=read(),y[i]=read(),val[i]=read();
    81         disc[(i<<1)-1]=x[i];disc[i<<1]=y[i];
    82     }
    83     sort(disc+1,disc+(n<<1)+1);
    84     for(int i=1;i<=n;i++)
    85         x[i]=find(x[i]),y[i]=find(y[i]);
    86     for(int i=1;i<=n;i++)
    87     {
    88         update(1,x[i],y[i]-1,val[i]);
    89     }
    90     ll ans=0;
    91     for(int i=1;i<2*n;i++)
    92     {
    93         ans+=(ll)query(1,i)*(disc[i+1]-disc[i]);
    94     }
    95     printf("%lld",ans);
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    [No0000159]C# 7 中的模范和实践
    [No0000158]思维模型1-20
    [No000014B]Office-PPT设置默认打开视图
    [No000014C]让大脑高效运转的24个技巧
    [No000014A]Linux简介与shell编程
    [No000014D]chrome console 调试 引入 jquery等外部库
    [No0000157].net core项目中拼音,excel,pdf处理库
    [No0000156]天干地支-狗年我懂,戊戌二字怎么来的?
    [No0000155]为什么32位机器最大只能用到4GB内存
    [No0000154]详解为什么32位系统只能用4G内存.
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3992526.html
Copyright © 2011-2022 走看看