Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remember, vividly, on the beautiful Zhuhai Campus, 4 years ago, from the moment I saw you smile, as you were walking out of the 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.

扫描线学得人好迷啊……虽然题目背景挺好的……

边界问题把我整佛了,草……


对于本题的思维转化,网上的题解一大把,在这方面已经说的非常清楚了。这篇文章只讨论两点:第一,扫描线的本质是什么;第二,边界问题的处理。

首先第一点,扫描线一般用来解决二维图形的交并问题,其实质是用一条边的顺序遍历将其中一维有序,另一维便可用区间数据结构维护。在本题中,我们把扫描线设为一条竖直直线使得水平方向有序,竖直方向的线段便可用线段树维护。可以将竖直方向切割出来的各个线段想象为一个区间,线段树的一个节点便维护了一条被切割的线段(即一个节点的 \(l,r\) 就对应原图形中被切割线段的左右端点)。

第二点,本题的边界问题。考虑到矩形的边界无法覆盖,而又考虑到星星的坐标均为整数,便稍作变换,将星星的横纵坐标均减去 \(0.5\) 个单位长度,变为 \((x-0.5,y-0.5)\)。此时再假设矩形的坐标均为整数,那么此时矩形的对角线的两点变为 \((x,y)\) 与 \((x+w-1,y+h-1)\),这么做的目的是让矩形的边界也包含在内,便于计算。一般讲到这,基本上所有的题解便开始放代码了,无非就是再多说明一句排序时的注意事项,但是根本不解释为什么要这么排序。

考虑这样一张图:



红方块代表星星(平移 \(0.5\) 没有画出来),其范围已用三种不同颜色标识出来,橙色是扫描线。可以看到,如果按照排序时先算出边再算入边的方法,那么就会出现误差,而这误差就是体现在原图的深蓝色区域。这是坐标平移所带来的误差,所以补救措施就是修改排序使其先算入边再算出边。

#include <cstdio>
#include <algorithm>
using namespace std; const int N=10000;
struct Segment
{
int x,y1,y2,c;
bool operator<(const Segment &a) {return x==a.x?c>a.c:x<a.x;}
}Seg[(N<<1)+5];
struct Tree
{
int l,r,sum,tag;
#define l(x) t[x].l
#define r(x) t[x].r
}t[(N<<3)+5];
int n,w,h,lsy[(N<<1)+5]; void build(int rt,int l,int r)
{
l(rt)=l,r(rt)=r;
t[rt].sum=t[rt].tag=0;
if(l==r) return;
int mid=l+r>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
} inline void pushdown(int rt)
{
t[rt<<1].tag+=t[rt].tag;
t[rt<<1|1].tag+=t[rt].tag;
t[rt<<1].sum+=t[rt].tag;
t[rt<<1|1].sum+=t[rt].tag;
t[rt].tag=0;
} void upd(int rt,int l,int r,int k)
{
if(l<=l(rt)&&r>=r(rt))
{t[rt].sum+=k,t[rt].tag+=k; return;}
if(t[rt].tag) pushdown(rt);
int mid=l(rt)+r(rt)>>1;
if(l<=mid) upd(rt<<1,l,r,k);
if(r>mid) upd(rt<<1|1,l,r,k);
t[rt].sum=max(t[rt<<1].sum,t[rt<<1|1].sum);
} int main()
{
while(~scanf("%d%d%d",&n,&w,&h))
{
int ans=0;
for(int i=1;i<=n;++i)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
Seg[2*i-1]=(Segment){x,y,y+h-1,c};
Seg[2*i]=(Segment){x+w-1,y,y+h-1,-c};
lsy[2*i-1]=y,lsy[2*i]=y+h-1;
}
n<<=1;
sort(lsy+1,lsy+n+1);
int cnt=unique(lsy+1,lsy+n+1)-lsy-1;
build(1,1,cnt);
sort(Seg+1,Seg+n+1);
for(int i=1;i<=n;++i)
{
int l=lower_bound(lsy+1,lsy+cnt+1,Seg[i].y1)-lsy;
int r=lower_bound(lsy+1,lsy+cnt+1,Seg[i].y2)-lsy;
upd(1,l,r,Seg[i].c);
ans=max(ans,t[1].sum);
}
printf("%d\n",ans);
}
return 0;
}

POJ2482 Stars in Your Window 题解的更多相关文章

  1. POJ2482 Stars in Your Window(扫描线+区间最大+区间更新)

    Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...

  2. POJ2482 Stars in Your Window 和 test20180919 区间最大值

    Stars in Your Window Language:Default Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K T ...

  3. Poj2482 Stars in Your Window(扫描线)

    题面 Poj 题解 下面内容引用自"李煜东 <算法竞赛进阶指南>"(对原文略有缩减,侵删): 因为矩形的大小固定,所以矩形可以由它的任意一个顶点唯一确定.我们可以考虑把 ...

  4. poj2482 Stars in Your Window

    此题可用线段树或静态二叉树来做. 考虑用线段树: 很容易想到先限定矩形横轴范围再考虑在此纵轴上矩形内物品总价值的最大值. 那么枚举矩形横轴的复杂度是O(n)的,考虑如何快速获取纵轴上的最大值. 我们不 ...

  5. 【POJ2482】Stars in Your Window

    [POJ2482]Stars in Your Window 题面 vjudge 题解 第一眼还真没发现这题居然™是个扫描线 令点的坐标为\((x,y)\)权值为\(c\),则 若这个点能对结果有\(c ...

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

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

  7. 【POJ2482】【线段树】Stars in Your Window

    Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw ...

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

  9. Stars in Your Window(线段树求最大矩形交)

    题目连接 http://poj.org/problem?id=2482 Description Fleeting time does not blur my memory of you. Can it ...

随机推荐

  1. Redis系列(三):Bitmaps和HyperLogLog

    本篇介绍Bitmaps和HyperLogLog. 一.Bitmaps 计算机中最小的单位是bit(位),很多计算机语言也提供了位操作符,比如Java中就有&.|.>>.>&g ...

  2. 【题解】poj 3254 Corn Fields

    题目描述 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的玉米,供 ...

  3. WEB安全新玩法 [2] 防范前端验证绕过

    用户登录,几乎是所有 Web 应用所必须的环节.Web 应用通常会加入一些验证手段,以防止攻击者使用机器人自动登录,如要求用户输入图形验证码.拖动滑动条等.但是,如果验证的逻辑仅仅在前端执行,是很容易 ...

  4. WPF使用 INotifyPropertyChanged 实现数据驱动

    如下图,有这么一个常见需求,在修改表单明细的苹果价格时,总价会改变,同时单据总和也随之改变. 按照Winfrom事件驱动的思想来做的话,我们就需要在将UI的修改函数绑定到CellEdit事件中来实现. ...

  5. CMD批处理(4)——批处理循环语句结构

    FOR函数,对一组文件批量执行命令,基本模式如下 1.文件搜索 for [/D] [/R [路径]] %%变量 in (集合) do (命令) 2.等差数列 for /L %%变量 in (开始,间隔 ...

  6. 重新整理 .net core 实践篇—————HttpClientFactory[三十二]

    前言 简单整理一下HttpClientFactory . 正文 这个HttpFactory 主要有下面的功能: 管理内部HttpMessageHandler 的生命周期,灵活应对资源问题和DNS刷新问 ...

  7. 『心善渊』Selenium3.0基础 — 9、使用Seleniun中的By类定位元素

    目录 1.使用By定位的前提 2.By定位的方法 3.By定位的使用 4.复数形式的示例 我们还可以通过Seleniun测试框架中的By类,来实现页面中的元素定位. 1.使用By定位的前提 需要导入B ...

  8. POJ 1015 Jury Compromise dp

    大致题意: 从n个候选人中选出m个人作为陪审团.为了让陪审团的选择更公平,辩方和控方都为这n个候选人给出了满意度(辩方为D[j],控方为P[j],范围0至20).现在要使得选出的m位候选人的辩方总和与 ...

  9. 信息论估计工具jidt基本使用

    JIDT基本介绍 JIDT是 Java Information Dynamics Toolkit的简称,用于研究复杂系统中信息论相关度量的计算,它是一个基于java的开源工具库,也可以在Matlab. ...

  10. Docker:docker安装部署jenkins

    Docker安装步骤请转到:https://www.cnblogs.com/nhdlb/p/11262527.html 查看docker的jenkins镜像版本 #查看jenkins版本命令 dock ...