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

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

  不妨以每个窗户的中心代表每个窗户,那么每颗星星所对应的窗户的范围即以其为中心的、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. Hash破解神器:Hashcat的简单使用

    Hash破解神器:Hashcat的简单使用 2014-06-10 21:02:42|  分类: 离线密码破解 |  标签:密码字典  rar密码破解  zip密码破解  密码破解  |举报|字号 订阅 ...

  2. xargs -i 和-I 的区别【转】

    xargs与find经常结合来进行文件操作,平时删日志的时候只是习惯的去删除,比如  # find . -type f -name "*.log" | xargs rm -rf * ...

  3. python的sorted函数对字典按value进行排序

    场景:词频统计时候,我们往往要对频率进行排序 sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数.其中iterable表示可 ...

  4. Maven整合Spring与Solr

    首先,在maven的pom.xml文件中配置对spring和solrj客户端的依赖: <project xmlns="http://maven.apache.org/POM/4.0.0 ...

  5. [ python ] 全局和局部作用域变量的引用

    全局与局部变量的引用 (a)locals(b)globals 这里还需要在补充2个关键字一起比较学习,关键字:(c)nonlocal(d)global locals 和 globals locals: ...

  6. spring(四)之基于注解(Annotation-based)的配置.md

    注解 这里讲的注解有下面几个 @Autowired @Qualifier(" ") @Genre(" ") @Offline @Resource(name=&q ...

  7. 转:google测试分享-问题和挑战

    原文: http://blog.sina.com.cn/s/blog_6cf812be0102vxer.html 前言:这个系列分享的内容大部分都是出自于<google是如何测试的>的书, ...

  8. promise应用于ajax

    promise应用于ajax,可以在本页打开控制台,复制代码试验 var url = 'https://www.cnblogs.com/mvc/blog/news.aspx?blogApp=dkplu ...

  9. MacBook Pro查找已安装的python目录

    MacBook Pro上下载的python安装后,发现查找目录无从下手,如下则是给出解决方案. 1.可下载pip进行安装,安装完成后,打开终端,输入:pip 并回车,则看到pip安装成功 2.再次输入 ...

  10. 如何去除decimal后面的零?

    如何去除decimal后面的零? 1.260000m.ToString("G29") 不显示科学记数法? decimal.Parse("0.0000001",S ...