如果按一般的思路来想,去求窗户能框住的星星,就很难想出来。

  如果换一个思路,找出每颗星星能被哪些窗户框住,这题就变得非常简单了。

  不妨以每个窗户的中心代表每个窗户,那么每颗星星所对应的窗户的范围即以其为中心的、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 线段树的更多相关文章

  1. 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 ...

  2. POJ 2482 Stars in Your Window (线段树区间合并+扫描线)

    这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用)  题意就是在平面上给你一些星 ...

  3. POJ 2482 Stars in Your Window(线段树+扫描线)

    题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...

  4. POJ 2482 Stars in Your Window(线段树)

    POJ 2482 Stars in Your Window 题目链接 题意:给定一些星星,每一个星星都有一个亮度.如今要用w * h的矩形去框星星,问最大能框的亮度是多少 思路:转化为扫描线的问题,每 ...

  5. poj 2482 Stars in Your Window(扫描线)

    id=2482" target="_blank" style="">题目链接:poj 2482 Stars in Your Window 题目大 ...

  6. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  7. poj 2482 Stars in Your Window + 51Nod1208(扫描线+离散化+线段树)

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13196   Accepted:  ...

  8. POJ 2482 Stars in Your Window 离散化+扫描法 线段树应用

    遇见poj上最浪漫的题目..题目里图片以上几百词为一篇模板级英文情书.这情感和细腻的文笔深深地打动了我..不会写情书的童鞋速度进来学习.传送门 题意:坐标系内有n个星星,每个星星都有一个亮度c (1& ...

  9. POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)

    该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...

随机推荐

  1. spring boot 自定义属性覆盖application文件属性

    参考 Spring boot源码分析-ApplicationListener应用环境: https://blog.csdn.net/jamet/article/details/78042486 加载a ...

  2. Mysql中的primary key 与auto_increment

    mysql> create table cc(id int auto_increment); ERROR (): Incorrect table definition; there can be ...

  3. PHP代码审计学习

    原文:http://paper.tuisec.win/detail/1fa2683bd1ca79c 作者:June 这是一次分享准备.自己还没有总结这个的能力,这次就当个搬运工好了~~ 0x01 工具 ...

  4. KVC, KVO 实现原理

    Key-Value Coding: 键值编码 (KVC) 方法调用: // 对象属性 // 类似: Person -> name setValue: forKey: // 对象的属性或者 属性的 ...

  5. .htaccess教程:简介、访问控制、验证、目录浏览控制

    一..htaccess简介 1.什么是.htaccess .htaccess是一个纯文本文件,里面存放着Apache服务器配置相关的一些指令,它类似于Apache的站点配置文件,如httpd.conf ...

  6. H5移动端视频问题(苹果全屏播放问题等)

    iphone上,手动.自动.窗口化等问题 iphone窗口化 解决方案: 通过canvas + video标签结合处理 原理: 获取video的原图帧,通过canavs绘制到页面. 我们一般在苹果上在 ...

  7. AssetBundle——外部加载资源Asset

    几篇很不错的文章  AssetBundle创建到使用入门 全面理解Unity加载和内存管理 实用的创建AssetBundle的脚本   相关资源 相关的共享资源下载  本共享包括创建assetbund ...

  8. Vue.js——60分钟快速入门(转)

    var vm = new Vue({ el: '#app', data: { people: [{ name: 'Jack', age: 30, sex: 'Male' }, { name: 'Bil ...

  9. js完成密码输入为空,和两次输入不一致

    <!DOCTYPE html><html><body> <script language="javascript"> functio ...

  10. NTP路由器配置

    14.1. 路由器日志显示时间戳 提问 在路由器 的日志和排错信息里面显示时间 回答 Router#configure terminal Enter configuration commands, o ...