【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)
【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11294 | Accepted: 3091 |
Description
classroom and turned your head back, with the soft sunset glow shining on your rosy cheek, I knew, I knew that I was already drunk on you. Then, after several months’ observation and prying, your grace and your wisdom, your attitude to life and your aspiration
for future were all strongly impressed on my memory. You were the glamorous and sunny girl whom I always dream of to share the rest of my life with. Alas, actually you were far beyond my wildest dreams and I had no idea about how to bridge that gulf between
you and me. So I schemed nothing but to wait, to wait for an appropriate opportunity. Till now — the arrival of graduation, I realize I am such an idiot that one should create the opportunity and seize it instead of just waiting.
These days, having parted with friends, roommates and classmates one after another, I still cannot believe the fact that after waving hands, these familiar faces will soon vanish from our life and become no more than a memory. I will move out from school tomorrow.
And you are planning to fly far far away, to pursue your future and fulfill your dreams. Perhaps we will not meet each other any more if without fate and luck. So tonight, I was wandering around your dormitory building hoping to meet you there by chance. But
contradictorily, your appearance must quicken my heartbeat and my clumsy tongue might be not able to belch out a word. I cannot remember how many times I have passed your dormitory building both in Zhuhai and Guangzhou, and each time aspired to see you appear
in the balcony or your silhouette that cast on the window. I cannot remember how many times this idea comes to my mind: call her out to have dinner or at least a conversation. But each time, thinking of your excellence and my commonness, the predominance of
timidity over courage drove me leave silently.
Graduation, means the end of life in university, the end of these glorious, romantic years. Your lovely smile which is my original incentive to work hard and this unrequited love will be both sealed as a memory in the deep of my heart and my mind. Graduation,
also means a start of new life, a footprint on the way to bright prospect. I truly hope you will be happy everyday abroad and everything goes well. Meanwhile, I will try to get out from puerility and become more sophisticated. To pursue my own love and happiness
here in reality will be my ideal I never desert.
Farewell, my princess!
If someday, somewhere, we have a chance to gather, even as gray-haired man and woman, at that time, I hope we can be good friends to share this memory proudly to relight the youthful and joyful emotions. If this chance never comes, I wish I were the stars in
the sky and twinkling in your window, to bless you far away, as friends, to accompany you every night, sharing the sweet dreams or going through the nightmares together.

Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a location (x, y). for each star, there is a grade ranging from 1 to 100, representing its brightness, where 100 is the brightest and 1 is the weakest. The window is a rectangle
whose edges are parallel to the x-axis or y-axis. Your task is to tell where I should put the window in order to maximize the sum of the brightness of the stars within the window. Note, the stars which are right on the edge of the window does not count. The
window can be translated but rotation is not allowed.
Input
with 3 integers each: x, y, c, telling the location (x, y) and the brightness of each star. No two stars are on the same point.
There are at least 1 and at most 10000 stars in the sky. 1<=W,H<=1000000, 0<=x,y<2^31.
Output
Sample Input
3 5 4
1 2 3
2 3 2
6 3 1
3 5 4
1 2 3
2 3 2
5 3 1
Sample Output
5
6
Source
非常考察综合应用的一个问题。。
。
反正我是卡了好久=.=
首先题目大意:天上有n颗星星(1 <= n <= 10000) 每一个星星有一个坐标 (x,y)(0 <= x , y < 2^31)和亮度 c(1 <= c <= 100)
你有一个矩形框 宽w 高h 问怎样框能让框里的星星亮度和最大
另外在边框上的星星的亮度不计入
直观的看 没什么思路……我是没思路…………………………………………暴力的话星星的选与不选会导致出现很多状态 想都甭想+。
+
既然是分在线段树专题 那就尽可能往线段树靠呗。。。
线段树是对区间查询 但仅仅支持一维区间 这样的二维区间仅仅能想办法把一个维度限制 这样 对于遍历到某个x的时候 出如今全部y的需多个区间的亮度和就easy求了
固定x就须要用到扫描线了 通过排序 让星星依照x有序 这样扫描全部的x 每当扫到一个x就把星星的亮度增加相应的y区间内
但仅仅加亮度满足了左边界 还须要在超出w宽度限制的时候把最前面的星星亮度从区间中取走
也就是用到了扫描线拆分线段的方法 在起点把这个块的价值增加 在终点减去 对于此题 起点是x 终点就是x+w 也就是从x最远能碰触到的边框
这样每一个点拆分成两部分 一部分是起点亮度为正值 还有一个是终点 亮度相反 对于全部拆出的2*n个点排序 优先依照x排序 x同样的价值为负的在前
由于要求边框上的星星不计入 因此须要先把边框上的星星亮度减去 再增加新星星
这样对于x处理好了 从头遍历 每遍历到一个点 就在y的区间内增加它的亮度(或正或负) y区间事实上就是[y,y+h-1] 就是它能够贡献价值的区间
可是y非常大 所以又涉及到一个离散化的问题 把全部出现过的y的值进行排序 然后离散化处理下就可以
不知道为什么 G++总是RE。。。可能哪里写挫了?。
。有G++ A的大神 还是自己水平不够啊~~。。
代码例如以下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8; struct Star
{
LL x,y1,y2,s;
bool operator < (const struct Star a)const
{
return x == a.x? s < 0: x < a.x;
}
}; LL sum[80004];
LL add[80004];
Star seg[20004];
LL ny[20004];
LL tp;
LL w,h; void Update(LL root,LL l,LL r,LL y1,LL y2,LL d)
{
//printf("l:%lld r:%lld\n",ny[l],ny[r]);
if(ny[l] == y1 && ny[r] == y2)
{
// printf("add:%lld",add[root]);
add[root] += d;
// printf("->%lld\n",add[root]); // printf("sum:%lld",sum[root]);
sum[root] += d;
// printf("->%lld\n",sum[root]);
return;
} LL mid = (l+r)>>1;
if(add[root])
{
sum[root<<1|1] += add[root];
add[root<<1|1] += add[root];
sum[root<<1] += add[root];
add[root<<1] += add[root];
add[root] = 0;
}
if(ny[mid] >= y2) Update(root<<1,l,mid,y1,y2,d);
else if(ny[mid+1] <= y1) Update(root<<1|1,mid+1,r,y1,y2,d);
else
{
Update(root<<1,l,mid,y1,ny[mid],d);
Update(root<<1|1,mid+1,r,ny[mid+1],y2,d);
}
sum[root] = max(sum[root<<1],sum[root<<1|1]);
} int main()
{
//fread();
//fwrite(); LL n; while(~scanf("%lld%lld%lld",&n,&w,&h))
{
for(LL i = 1; i <= n; ++i)
{
scanf("%lld%lld%lld",&seg[i].x,&seg[i].y1,&seg[i].s);
seg[i].y2 = seg[i].y1+h-1; seg[i+n] = seg[i];
seg[i+n].x = seg[i].x+w;
seg[i+n].s = -seg[i].s; ny[i] = seg[i].y1;
ny[i+n] = seg[i].y2;
} if(w == 0 || h == 0)
{
puts("0");
continue;
}
sort(ny+1,ny+2*n+1);
sort(seg+1,seg+2*n+1);
tp = 0;
for(LL i = 1; i <= 2*n; ++i)
if(i == 1 || ny[i] != ny[i-1])
ny[++tp] = ny[i]; LL ans = 0;
memset(sum,0,sizeof(sum));
memset(add,0,sizeof(add));
for(LL i = 1; i <= 2*n; ++i)
{
// printf("%lld %lld\n",seg[i].y1,seg[i].y2);
Update(1,1,tp,seg[i].y1,seg[i].y2,seg[i].s);
ans = max(ans,sum[1]);
} printf("%lld\n",ans);
} return 0;
}
【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 线段树
如果按一般的思路来想,去求窗户能框住的星星,就很难想出来. 如果换一个思路,找出每颗星星能被哪些窗户框住,这题就变得非常简单了. 不妨以每个窗户的中心代表每个窗户,那么每颗星星所对应的窗户的范围即以其 ...
- POJ 2482 Stars in Your Window (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
- POJ 2482 Stars in Your Window(线段树+扫描线)
题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...
- poj2482Stars in Your Window(线段树+离散化+扫描线)
http://poj.org/problem?id=2482 类似于上一篇 这题转化的比较巧妙 将一个点转化为一个矩形(x,y, x+w,y+h),扫描线入值为正 出值为负 也就是一根线过去 每进入一 ...
- 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
线段树+离散化+扫描线 AC之后,又认真读了一遍题目,好文章. #include<cstdio> #include<map> #include<algorithm> ...
- hdu1542 矩形面积并(线段树+离散化+扫描线)
题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
随机推荐
- 6. Intellij Idea 2017创建web项目及tomcat部署实战
转自:https://www.cnblogs.com/shindo/p/7272646.html 相关软件:Intellij Idea2017.jdk16.tomcat7 Intellij Idea直 ...
- 网络通信-ping命令
- 三分钟学会用SpringMVC搭建最小系统(超详细)_转载
前言 做 Java Web 开发的你,一定听说过SpringMVC的大名,作为现在运用最广泛的Java框架,它到目前为止依然保持着强大的活力和广泛的用户群. 本文介绍如何用eclipse一步一步搭建S ...
- Windows下使用VS的ADO访问MySQL
数据库的访问的一种方式就是:CS结构.即使用TCP/UDP协议进行远程访问,而数据库对于服务端的软件是本地访问!这种管理方式比较常见. 这里主要叙述Windows访问本地数据库的方法. 需要了解几个概 ...
- 企业级Nginx+Keepalived集群实战(双主架构)
随着Nginx在国内的发展潮流,越来越多的互联网公司都在使用Nginx,Nginx高性能.稳定性成为IT人士青睐的HTTP和反向代理服务器.Nginx负载均衡一般位于整个网站架构的最前端或者中间层,如 ...
- [NOIP2009] 靶形数独(搜索)
P1074 靶形数独 题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士 ...
- P2633 Count on a tree(主席树)
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- Java基础学习总结(11)——重载与重写
首先我们来讲讲:重载(Overloading) 一.方法的重载 方法名一样,但参数不一样,这就是重载(overload). 所谓的参数不一样,主要有两点:第一是参数的个数不一样,第二是参数的类型不一样 ...
- ZooKeeper 特性
ZooKeeper 拥有一个层次的命名空间.(like distributed) 注意:ZooKeeper 中不许使用相对路径. 一 ZooKeeper 数据模型 ...
- 软件project总结
软件project的文档完结了.在这里面学到了好多东西.也通过它分析了对机房收费系统进行了更加具体的分析.尽管早就明确了之间的联系,可是越温习越体会到逻辑的重要性和全心全意为人民服务的精神. 这些文档 ...