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 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...
随机推荐
- BZOJ 2173 整数的lqp拆分
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2173 题意:给出输出n.设一种拆分为n=x1+x2+x3,那么这种拆分的价值为F(x1) ...
- CollectionBase类
在命名空间System.Collections下的CollectionBase类 The CollectionBase class exposes the interfaces IEnumerable ...
- [CF660C]Hard Process(尺取法)
题目链接:http://codeforces.com/problemset/problem/660/C 尺取法,每次遇到0的时候补一个1,直到补完或者越界为止.之后每次从左向右回收一个0点.记录路径用 ...
- Longest Valid Parentheses(最长有效括号)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- android 启动第三方程序的代码
//启动媒体库 Intent i = new Intent(); ComponentName comp = new ComponentName("com.android.camera&q ...
- Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)
Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...
- js数组的声明与应用
js数组的声明与应用 数组:一种容器,存储批量数据.JavaScript是一种弱类型语言.什么是弱类型,就是变量可以存储多种类型变量的引用不会报错.所以js数组可以存储不同的数据. 一.数组的作用:只 ...
- python知识:json格式文本;异常处理;字符串处理;unicode类型和str类型转换
python进程中的实例和json格式的字符串之间的映射关系是非常直接的,相当于同一个概念被编码成不同的表示: stream in json form ----json.loads(str)----- ...
- Mysql 数据库文件存储在哪个目录
也就是说我在mysql里建了一个叫 ac 的数据库,但是我找不到其存储位置,Mysql里面的数据库是怎么存储的,是否也像sqlserver 那样,有一个日志文件和数据文件? mysql数据库在系统上是 ...
- pageX,clientX,offsetX,layerX的区别
pageX,clientX,offsetX,layerX的区别 在各个浏览器的JS中,有很多个让你十分囧的属性,由于各大厂商对标准的解释和执行不一样,导致十分混乱,也让我们这些前端攻城狮十分无语和纠结 ...