zoukankan      html  css  js  c++  java
  • xtu数据结构 H. City Horizon

    H. City Horizon

    Time Limit: 2000ms
    Memory Limit: 65536KB
    64-bit integer IO format: %lld      Java class name: Main
     

    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 Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.

     

    Input

    Line 1: A single integer: N
    Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: AiBi, and Hi
     

    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

    解题:看了解题报告的,还在领悟离散化是什么灰机,不过这题目线段树部分其实很容易的,只是加上了离散化这一操作,Eggache啊!


     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <climits>
     7 #include <algorithm>
     8 #include <cmath>
     9 #define LL long long
    10 #define INF 0x3f3f3f
    11 using namespace std;
    12 const int maxn = 40010;
    13 struct Building{
    14     LL a,b,h;
    15 }bb[maxn];
    16 struct node{
    17     int lt,rt,h;
    18 }tree[maxn<<3];
    19 LL p[maxn<<1],n,ans;
    20 int cnt;
    21 bool cmp(const Building &x,const Building &y){
    22     return x.h < y.h;
    23 }
    24 void build(int lt,int rt,int v){
    25     tree[v].lt = lt;
    26     tree[v].rt = rt;
    27     tree[v].h = 0;
    28     if(rt-lt == 1) return;
    29     int mid = (lt+rt)>>1;
    30     build(lt,mid,v<<1);
    31     build(mid,rt,v<<1|1);
    32 }
    33 void update(int lt,int rt,int val,int v){
    34     if(tree[v].lt == lt && tree[v].rt == rt){
    35         tree[v].h = val;
    36         return;
    37     }
    38     if(tree[v].h > 0){
    39         tree[v<<1].h = tree[v<<1|1].h = tree[v].h;
    40         tree[v].h = 0;
    41     }
    42     int mid = (tree[v].lt+tree[v].rt)>>1;
    43     if(rt <= mid) update(lt,rt,val,v<<1);
    44     else if(lt >= mid) update(lt,rt,val,v<<1|1);
    45     else{
    46         update(lt,mid,val,v<<1);
    47         update(mid,rt,val,v<<1|1);
    48     }
    49 }
    50 void query(int v){
    51     if(tree[v].h > 0){
    52         ans += (LL)tree[v].h*(p[tree[v].rt-1] - p[tree[v].lt-1]);
    53         return;
    54     }
    55     if(tree[v].rt - tree[v].lt == 1) return;
    56     query(v<<1);
    57     query(v<<1|1);
    58 }
    59 int bsearch(int lt,int rt,int val){
    60     while(lt <= rt){
    61         int mid = (lt+rt)>>1;
    62         if(p[mid] == val)
    63             return mid+1;
    64         else if(val < p[mid])
    65          rt = mid-1;
    66         else lt = mid+1;
    67     }
    68     return 0;
    69 }
    70 int main(){
    71     int i,j,r;
    72     scanf("%lld",&n);
    73     for(i = 0; i < n; i++){
    74         scanf("%d%d%d",&bb[i].a,&bb[i].b,&bb[i].h);
    75         p[cnt++] = bb[i].a;
    76         p[cnt++] = bb[i].b;
    77     }
    78     sort(p,p+cnt);//排序是为了使用二分查找。
    79     sort(bb,bb+n,cmp);
    80     build(1,n<<1,1);
    81     r = (n<<1)-1;
    82     for(i = 0; i < n; i++){
    83         int lt = bsearch(0,cnt-1,bb[i].a);
    84         int rt = bsearch(0,cnt-1,bb[i].b);
    85         update(lt,rt,bb[i].h,1);
    86     }
    87     ans = 0;
    88     query(1);
    89     printf("%lld",ans);
    90     return 0;
    91 }
    View Code
  • 相关阅读:
    java lambda表达式检查list集合是否存在某个值
    Java使用枚举来消除if else(转载)
    常用的设计模式汇总,超详细!
    mybatis generator 自动生成代码
    eclipse集成lombok插件
    eclipse使用mybatis实现Java与xml文件相互跳转
    GitLab企业级代码管理仓库
    详解Eureka 缓存机制
    JAVA设计模式之工厂模式
    solr常见错误
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3861413.html
Copyright © 2011-2022 走看看