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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 732  Solved: 333
[Submit][Status][Discuss]

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

线段树扫描线模板题

求矩形面积和要比求周长和容易点

#include<stdio.h>
#include<algorithm>
using namespace std;
#define LL long long
typedef struct Tree
{int cnt;int s1;
}Tree;
Tree tre[322233];
typedef struct Line
{int x, y;int flag;bool operator < (const Line &b) const{if(x<b.x || x==b.x && flag<b.flag)return 1;return 0;}
}Line;
Line s[80005];
int pos[80005];
void Atonce(int l, int r, int x)
{if(tre[x].cnt>=1)tre[x].s1 = pos[r]-pos[l-1];else{if(l!=r)tre[x].s1 = tre[x*2].s1+tre[x*2+1].s1;elsetre[x].s1 = 0;}
}
void Update(int l, int r, int x, int h, int val)
{int m;if(r<=h){tre[x].cnt += val;Atonce(l, r, x);return;}m = (l+r)/2;Update(l, m, x*2, h, val);if(h>=m+1)Update(m+1, r, x*2+1, h, val);Atonce(l, r, x);
}
int main(void)
{LL ans;int n, i, x1, x2, cnt, h;scanf("%d", &n);cnt = 0;for(i=1;i<=n;i++){scanf("%d%d%d", &x1, &x2, &h);s[++cnt].x = x1, s[cnt].y = h, s[cnt].flag = 1;s[++cnt].x = x2, s[cnt].y = h, s[cnt].flag = -1;pos[i] = h;}sort(pos+1, pos+n+1);n = unique(pos+1, pos+n+1)-(pos+1);sort(s+1, s+cnt+1);ans = 0;for(i=1;i<=cnt;i++){h = lower_bound(pos+1, pos+n+1, s[i].y)-pos;Update(1, n, 1, h, s[i].flag);ans += (LL)tre[1].s1*(s[i+1].x-s[i].x);}printf("%lld\n", ans);return 0;
}

bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线(线段树扫描线)相关推荐

  1. bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】

    bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...

  2. [Usaco2007 Open]City Horizon 城市地平线

    1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec   Memory Limit: 64 MB Submit: 411   Solve ...

  3. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  4. BZOJ 2138 stone(霍尔定理推论,线段树)【BZOJ 修复工程】

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 题目链接 https://hydro.ac/d/bzoj/p/2138 是 hydro 的 BZOJ ...

  5. BZOJ 5326 [JSOI2017]博弈 (模拟费用流、线段树)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=5326 题解 终于成为第8个A掉这题的人--orz tzw神仙早我6小时 本以为这东西常数 ...

  6. BZOJ 1683.City skyline 城市地平线

    传送门 从左到右扫一遍,考虑什么时候会和之前形成同一幢房子从而不用统计 显然是当前的高度和之前某个点高度相同,并且它们之间没有更矮的建筑 考虑用一个单调栈维护一个单调上升的房子轮廓,然后对于扫到的每一 ...

  7. 【BZOJ】3339: Rmq Problem 3585: mex(线段树+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3585 好神的题. 但是!!!!!!!!!!!!!!我线段树现在要开8倍空间才能过!!!!!!!!!! ...

  8. BZOJ 3218(a + b Problem-二分图套值域线段树)

    出这题的人是怎么想出来的-- 言归正传,这题是二分图套值域线段树. 首先经过 @Vfleaking的神奇建图后,把图拆成二分图, 不妨利用有向图最小割的性质建图(以前我一直以为最小割和边的方向无关,可 ...

  9. 【BZOJ】1756: Vijos1083 小白逛公园(线段树)

    题目 传送门:QWQ 分析 线段树维护一下最大子序列 维护一下最大前缀 最大后缀  区间和 就ok了 好像只能用结构体..... 代码 #include <bits/stdc++.h> u ...

最新文章

  1. 为sort函数指定排序规则时注意的问题以及错误的写法
  2. 漂浮窗口拖动杂谈(续)
  3. 解决libreadline.so.6: cannot open shared object file: No such file or directory的问题
  4. 计算机审计风险背景,计算机环境下审计风险的特征与对策研究 毕业论文.doc
  5. 使用vue的生命周期函数_异步_同步获取字典数据---基于Vue的uniapp手机端_前端UI_uview工作笔记003
  6. Unitest框架的使用(一)准备工作,接口定义和Unittest方法
  7. oracle 11g新特性之IPS Packing demonstration
  8. objective-c 中的关联介绍
  9. Linux电源管理(2)_Generic PM之基本概念和软件架构
  10. mysql 协议解析源码 c_MySQL协议分析2
  11. 管理感悟:如何处理不干活的主管
  12. 小数保留4位_实验结果保留几位有效数字?
  13. python网格搜索、贝叶斯调参实战
  14. 计算机准考证要打印多大的纸,2020税务师准考证打印要求 应该用多大的纸打印?...
  15. 云桌面优缺点_传统PC与云桌面办公的优劣势对比
  16. 光纤中的非线性光学效应
  17. 计算机制图基本知识6,机械制图及计算机绘图教案.doc
  18. GPS测量原理及应用 知识总结
  19. SKETCH 切出背景透明的图标
  20. 银盒子智慧餐厅硬件尺寸规格推荐机型

热门文章

  1. python兼职程序员工资-没想到,学会Python即使不做程序员都能月入过万!
  2. 零基础学python还是c语言-零基础学Python之前需要学c语言吗
  3. 高通宣称其语音识别系统准确率高达95%
  4. this.$router.push用query传参对象时需注意的地方
  5. x264源代码简单分析:宏块编码(Encode)部分
  6. ActionScript 3.0 API 中的 Video 类
  7. Flash 平台音视频直播的实现
  8. Java 获取系统信息
  9. erlang 变量存储在哪里_erlang中变量作用域
  10. 【软考】面向对象程序设计复习指南