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 ...
随机推荐
- GCD HDU - 1695 莫比乌斯反演入门
题目链接:https://cn.vjudge.net/problem/HDU-1695#author=541607120101 感觉讲的很好的一个博客:https://www.cnblogs.com/ ...
- Hive ORC表的使用
创建普通临时表: create table if not exists test_orc_tmp( name string, gender string, cnt BIGINT )row ...
- 回溯算法_01背包问题_Java实现
原文地址:http://blog.csdn.net/ljmingcom304/article/details/50314839 本文出自:[梁敬明的博客] 1.回溯算法 回溯算法也叫试探法,通俗的将就 ...
- 在Linux 系统上运行多个tomcat
--原来的不动,添加环境变量(.bash_profile)export JAVA_HOME=/home/public/jdk1.8.0_131export JRE_HOME=$JAVA_HOME/jr ...
- spring mvc 自定义编辑器
起始知识: Java标准的PropertyEditor的核心功能是将一个字符串转换为一个Java对象,以便根据界面的输入或配置文件中的配置字符串构造出一个JVM内部的java对象. 如何注册自定义的属 ...
- Hadoop(一):概述
一.Hadoop是什么? Hadoop是一个由Apache基金会所开发的分布式系统基础架构.Hadoop框架最核心的设计包含两个方面,一是分布式文件系统(Hadoop Distributed File ...
- linux下redis的安装与部署
一.Redis介绍 Redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcache类似,但很大程度补偿了Memcache的不足,它支持存储的value类型相对更多 ...
- prototype 与 __proto__
原文:http://rockyuse.iteye.com/blog/1426510 说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: 1 <script type= ...
- html学习-css
1.css初识 css 中文解释:层叠样式表,把html比作骨骼的话,css就是衣服,他的外在都能通过css来修饰,js则是肌肉,能使html动起来.产生用户交互... 1.1css样式表类型 css ...
- Ntp时间服务器与定时任务Crontab
一 NTP时间服务器 1 局域网内的NTP同步配置 注意 所有配置操作必须是root用户 ,局域网内node21作为NTP Server,node22,node23作为NTP Client与服务器进行 ...