pku3277 City Horizon
http://poj.org/problem?id=3277
线段树,离散化,成段更新
#include <stdio.h>
#include <stdlib.h> #define lson l, m, root<<1
#define rson m+1, r, root<<1|1 const long long N = ;
const long long inf = (<<)-; long long max1[N<<], color[N<<]; long long max(long long x, long long y)
{
return x>y? x: y;
} void push_up(long long root)
{
max1[root] = max(max1[root<<], max1[root<<|]);
} void push_down(long long l, long long r, long long root)
{
color[root<<] = max(color[root<<], color[root]);
color[root<<|] = max(color[root<<|], color[root]);
color[root] = -;
max1[root<<] = max(max1[root<<], color[root<<]);
max1[root<<|] = max(max1[root<<|], color[root<<|]);
} void build(long long l, long long r, long long root)
{
long long m;
color[root] = -;
if(l == r)
{
max1[root] = ;
return;
}
m = (l + r) >> ;
build(lson);
build(rson);
push_up(root);
} void update(long long L, long long R, long long x, long long l, long long r, long long root)
{
long long m;
if(L <= l && r <= R)
{
color[root] = max(color[root], x);
max1[root] = max(max1[root], color[root]);
return;
}
m = (l + r) >> ;
if(color[root] != -)
{
push_down(l, r, root);
}
if(L <= m)
{
update(L, R, x, lson);
}
if(m+ <= R)
{
update(L, R, x, rson);
}
push_up(root);
} long long query(long long L, long long R, long long l, long long r, long long root)
{
long long m, result = ;
if(L <= l && r <= R)
{
return max1[root];
}
m = (l + r) >> ;
if(color[root] != -)
{
push_down(l, r, root);
}
if(L <= m)
{
result = max(result, query(L, R, lson));
}
if(m+ <= R)
{
result = max(result, query(L, R, rson));
}
push_up(root);
return result;
} struct B
{
long long x, y;
long long h;
}b[N>>]; long long a[N], hash[N]; long long bs(long long l, long long r, long long x)
{
long long m;
while(l < r)
{
m = (l + r) >> ;
if(hash[m] == x)
{
return m;
}
if(hash[m] < x)
{
l = m + ;
}
else
{
r = m;
}
}
} int cmp0(const void *a, const void *b)
{
return *(long long *)a - *(long long *)b;
} int main()
{
long long n, i, j, temp, result = ;
long long k;
scanf("%lld", &n);
for(i=; i<=n; i++)
{
scanf("%lld%lld%lld", &b[i].x, &b[i].y, &b[i].h);
a[(i-)*+] = b[i].x;
a[(i-)*+] = b[i].y;
}
qsort(a+, *n, sizeof(a[]), cmp0);
j = ;
hash[j] = a[];
for(i=; i<=*n; i++)
{
if(a[i] != a[i-])
{
j = j + ;
hash[j] = a[i];
}
}
// for(i=1; i<=j; i++)
// {
// printf("%5d", i);
// }
// printf("\n");
// for(i=1; i<=j; i++)
// {
// printf("%5d", hash[i]);
// }
// printf("\n");
build(, j-, );
for(i=; i<=n; i++)
{
// printf("update %lld %lld %lld\n", bs(1, j+1, b[i].x), bs(1, j+1, b[i].y)-1, b[i].h);
update(bs(, j+, b[i].x), bs(, j+, b[i].y)-, b[i].h, , j, );
// for(k=1; k<=j-1; k++)
// {
// printf("%4d", k);
// }
// printf("\n");
// for(k=1; k<=j-1; k++)
// {
// temp = query(k, k, 1, j, 1);
// result += (temp * (hash[k+1]-hash[k]));
// printf("%4d", temp);
// }
// printf("\n");
}
result = ;
for(i=; i<=j-; i++)
{
temp = query(i, i, , j, );
result += (temp * (hash[i+]-hash[i]));
// printf("%lld %lld %lld\n", i, temp, hash[i+1]-hash[i]);
}
printf("%lld\n", result);
return ;
}
pku3277 City Horizon的更多相关文章
- [POJ3277]City Horizon
[POJ3277]City Horizon 试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, th ...
- 离散化+线段树 POJ 3277 City Horizon
POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...
- poj City Horizon (线段树+二分离散)
http://poj.org/problem?id=3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- 1645: [Usaco2007 Open]City Horizon 城市地平线
1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 315 Solved: ...
- bzoj1645 [Usaco2007 Open]City Horizon 城市地平线
Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at ...
- BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线
BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...
- bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)
P2061 [USACO07OPEN]城市的地平线City Horizon 扫描线 扫描线简化版 流程(本题为例): 把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序 ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
- 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...
随机推荐
- 标准模板库(STL)学习探究之vector容器
标准模板库(STL)学习探究之vector容器 C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...
- 1427. SMS(DP)
1427 题意不太好理解 其它没什么 细心啊 细心 一个0写成了1 WA半天 以每个字符是以第一种方式还是第二种方式来D #include <iostream> #include<c ...
- word-pattern(mock)
注意: // String要用equals,不然比较结果不对,会出bug// 使用String.split // boolean打印用 %b // abba 对应 cccc 也不行, 所以要用set ...
- Linux进程控制(二)
1. 进程的创建 Linux下有四类创建子进程的函数:system(),fork(),exec*(),popen() 1.1. system函数 原型: #include <stdlib.h&g ...
- PHP的模板引擎这点事儿
什么是模板引擎? 为什么要使用它? 为什么要assign一个变量给模板? https://dbforch.wordpress.com/2010/06/26/the-logic-behind-templ ...
- java读取Properties文件
方法一.通过java.util.Properties读取 Properties p=new Properties(); //p需要InputStream对象进行读取文件,而获取InputStream有 ...
- 相对定位、绝对定位在IE6的问题
注意: 关于绝对定位,在IE6下定位元素的父级宽高都为奇数那么在IE6下定位元素的right,bottom都有一像素的偏差(left,top无偏差).因此应尽量使用偶数. 关于绝对定位,在IE6下父级 ...
- windows 勾子简介
近段时间因朋友催促让试着写一个监控系统,主要是用来管理孩子使用电脑,帮助孩子合理使用电脑.在网上查询了相关内容发现没有这方面的资料,所以只有自已来试试,要用到钩子来对windows应用程序进行监控,也 ...
- 整理一些js中常见的问题
原文链接 1.js获取select标签选中的值 原生js var obj = document.getElementByIdx_x(”testSelect”); //定位id var index = ...
- (转)每天一个Linux命令(6):mv
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...