POJ 2482 Stars in Your Window 线段树
如果按一般的思路来想,去求窗户能框住的星星,就很难想出来。
如果换一个思路,找出每颗星星能被哪些窗户框住,这题就变得非常简单了。
不妨以每个窗户的中心代表每个窗户,那么每颗星星所对应的窗户的范围即以其为中心的、W*H的矩形。把这些矩形的左边和右边,分别加上和减去其价值。而统计这些边只需要开一个线段树统计即可,用每次加边后得到的最大值更新答案。
需要注意的是,计算这些边上两点的坐标时可能出现0.5的情况,为了避免,把原坐标*2即可。而且坐标过大,需要离散化。
一开始打的时候,竟然,错了样例。仔细看了之后才发现窗户的长度为框住的点数+1,TAT
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map> using namespace std; #define ls (rt<<1)
#define rs ((rt<<1)+1)
typedef long long LL;
const int maxn = ;
int n, m, W, H;
struct Node
{
LL x, y1, y2, v;
Node (LL x = , LL y1 = , LL y2 = , int v = ):
x(x), y1(y1), y2(y2), v(v) {}
bool operator < (const Node &AI) const
{
if (x == AI.x)
return v < AI.v;
return x < AI.x;
}
}line[maxn*];
LL t[maxn];
struct Tree
{
LL mv[maxn*], delta[maxn*];
void Build(int rt, int l, int r)
{
delta[rt] = mv[rt] = ;
if (l == r)
return ;
int mid = (l+r)>>;
Build(ls, l, mid);
Build(rs, mid+, r);
}
void PushUp(int rt)
{
mv[rt] = max(mv[ls], mv[rs]);
}
void PushDown(int rt)
{
mv[ls] += delta[rt], mv[rs] += delta[rt];
delta[ls] += delta[rt], delta[rs] += delta[rt];
delta[rt] = ;
}
void Update(int rt, int l, int r, int L, int R, int k)
{
if (L <= l && r <= R)
{
mv[rt] += k;
delta[rt] += k;
return ;
}
if (delta[rt] != )
PushDown(rt);
int mid = (l+r)>>;
if (L <= mid)
Update(ls, l, mid, L, R, k);
if (R > mid)
Update(rs, mid+, r, L, R, k);
PushUp(rt);
}
}T;
map <LL, int> num_y; int main()
{
while (~scanf("%d %d %d", &n, &W, &H))
{
W ++, H ++;
m = ;
int Tcnt = ;
for (int i = ; i <= n; ++i)
{
LL x, y, v;
scanf("%lld %lld %lld", &x, &y, &v);
line[++m] = Node(x*-(W-)+, y*-(H-)+, y*+(H-)-, v);
line[++m] = Node(x*+(W-), y*-(H-)+, y*+(H-)-, -v);
t[++Tcnt] = y*-(H-)+, t[++Tcnt] = y*+(H-)-;
}
sort(t+, t+Tcnt+);
int las_cnt = Tcnt;
Tcnt = ;
for (int i = ; i <= las_cnt; ++i)
if (t[i] != t[i-] || i == )
{
t[++Tcnt] = t[i];
num_y[t[i]] = Tcnt;
}
sort(line+, line+m+);
T.Build(, , Tcnt);
LL ans = ;
for (int i = ; i <= m; ++i)
{
T.Update(, , Tcnt, num_y[line[i].y1], num_y[line[i].y2], line[i].v);
ans = max(ans, T.mv[]);
}
printf("%lld\n", ans);
}
return ;
}
POJ 2482 Stars in Your Window 线段树的更多相关文章
- POJ 2482 Stars in Your Window 线段树扫描线
Stars in Your Window Description Fleeting time does not blur my memory of you. Can it really be 4 ...
- POJ 2482 Stars in Your Window (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
- POJ 2482 Stars in Your Window(线段树+扫描线)
题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...
- POJ 2482 Stars in Your Window(线段树)
POJ 2482 Stars in Your Window 题目链接 题意:给定一些星星,每一个星星都有一个亮度.如今要用w * h的矩形去框星星,问最大能框的亮度是多少 思路:转化为扫描线的问题,每 ...
- poj 2482 Stars in Your Window(扫描线)
id=2482" target="_blank" style="">题目链接:poj 2482 Stars in Your Window 题目大 ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- poj 2482 Stars in Your Window + 51Nod1208(扫描线+离散化+线段树)
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13196 Accepted: ...
- POJ 2482 Stars in Your Window 离散化+扫描法 线段树应用
遇见poj上最浪漫的题目..题目里图片以上几百词为一篇模板级英文情书.这情感和细腻的文笔深深地打动了我..不会写情书的童鞋速度进来学习.传送门 题意:坐标系内有n个星星,每个星星都有一个亮度c (1& ...
- POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)
该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...
随机推荐
- Vue 脱坑记
问题汇总 Q:安装超时(install timeout) 方案有这么些: cnpm : 国内对npm的镜像版本 /* cnpm website: https://npm.taobao.org/ */ ...
- Callback2.0
Callback定义? a callback is a piece of executable code that is passed as an argument to other code, wh ...
- ip_rcv && ip_rcv_finish
(1) 在inet_init中注册了类型为ETH_P_IP协议的数据包的回调ip_rcv (2) 当二层数据包接收完毕,会调用netif_receive_skb根据协议进行向上层分发 (3) 类型为E ...
- JavaScript 跳转 页面
* window.location.href , self.location, window.location 出现问题不能跳转 Chome 不能本页跳转, IE 有时可以
- 「caffe编译bug」python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
在Makefile.config找到PYTHON_INCLUDE,发现有点不同: PYTHON_INCLUDE := /usr/include/python2.7 \ /usr/lib ...
- selenium启动chrome模拟器模拟手机
一.如果chrome选项里边有这个模拟设备(比如iPhone 6 Plus): 1.先启动Selenium Grid, 比如命令:java -jar selenium-server-standalon ...
- C/C++——C语言库函数大全
本文转载自:https://blog.csdn.net/yanfan0916/article/details/6450442###; 1. 分类函数: ctype.h int isalpha(int ...
- 看看PHP迭代器的内部执行过程
class myIterator implements Iterator { private $position = 0; private $array = array( "first_el ...
- php正则匹配以“abc”开头且不能以“xyz”结尾的字符串
本文介绍下,用php正则区配以"abc"开头的,且不能以"xyz"结尾的字符串的方法,有需要的朋友参考下. 要求:用php正则表达式匹配以“abc”开头,但结尾 ...
- C++函数的重载、覆盖和隐藏区别
a.成员函数被重载的特征:(1)相同的范围(在同一个类中)(2)函数名字相同(3)参数不同(4)virtual 关键字可有可无b.覆盖是指派生类函数覆盖基类函数,特征是:(1)不同的范围(分别位于派生 ...