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 ...
随机推荐
- 根据 plist 还原 图片
1. python 环境自己配置(支持windows Mac ) 2. 把所有的 plist 和 大图片放到一个目录下 3.如果添加了 系统环境变量 就直接双击运行脚本,如果没有设置,把脚本拽到DO ...
- python 第二章 对象与类型
可变对象和不可变对象 1,可变对象,list(列表),dict(字典),集合(set),字节数组. 2,不可变对象,数值类型,字符串,字节串,元组(具体形式 ()). 注意条件:可变和不可变指的是该对 ...
- 导航狗IT周报第十五期(July 8, 2018)
摘要:Seclists.Org: 微信支付SDK存在XXE漏洞:WordPress 4.9.6存在文件删除漏洞:linux中常用的文件打包/解包与压缩/解压缩命令总结… 安全播报 Seclists.O ...
- offset宏的讲解【转】
转自:http://blog.csdn.net/tigerjibo/article/details/8299584 1.offset宏讲解 #define offsetof(TYPE, MEMBER) ...
- 64_p5
php-nette-bootstrap-2.4.3-1.fc26.noarch.rpm 20-Feb-2017 07:19 16290 php-nette-caching-2.5.3-1.fc26.n ...
- curl 发送请求的时候报错
AWS HTTP error: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see ...
- 最全Pycharm教程(26)——Pycharm搜索导航之文件名、符号名搜索(转)
1.准备一个工程 向你的工程中添加一个Python文件,并输入一些源码,例如: 2.转到对应文件.类.符号 Pycharm提供的一个很强力的功能就是能够根据名称跳转到任何文件.类.符号所在定义位置. ...
- C# 多线程多文件批量下载---子线程中更新UI 实例
代码1: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;usi ...
- 常用对称加密算法(DES/AES)类(PHP)
看注释,啥也不说了,欢迎各种跨平台测试! /** * 常用对称加密算法类 * 支持密钥:64/128/256 bit(字节长度8/16/32) * 支持算法:DES/AES(根据密钥长度自动匹配使用: ...
- 性能测试常用的linux命令
性能测试常用的linux命令 linux测试nginx64bitredhatlighttpd 查看日志 awk '$4 ~/^\[27\/Nov\/2008:15:2[0-5]/ {print ...