POJ 题目2482 Stars in Your Window(线段树+离散化)
2015-09-04 00:50
357 查看
Stars in Your Window
Description
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.
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
There are several test cases in the input. The first line of each case contains 3 integers: n, W, H, indicating the number of stars, the horizontal length and the vertical height of the rectangle-shaped window. Then n lines follow,
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
For each test case, output the maximum brightness in a single line.
Sample Input
Sample Output
Source
POJ Contest,Author:kinfkong@ZSU
题目大意:
就是给你一个n,w,h,下边n个星星,每个星星都有自己的坐标,亮度,,w h表示窗户的宽和高,然后问这个窗户能包括的最大的亮度和
思路:
就是跟扫描线,扫矩形一样,(我咋感觉这就是扫描线啊,,)对每个星星作为窗户的左下角建窗户,让他对y轴投影,每个矩形窗户的左右两个边有val值,左边是正的,右边的是负的,把这些所有的和y轴平行的线段按x排序,(要是x相等,负的在前边,先减去他,再往下加),然后从左往右扫,扫到后就update,他对y轴的投影区域都加val,然后问题就转化成了,求所有的区间的最大值了,从左扫到最后就行了,被上个题的__int64 坑怕了,丧心病狂的我都用的64位,,,pushdown中右子树的maxn写成了add,wa了,两次,卧槽,我都不知道我在想些什么
ac代码
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11023 | Accepted: 3022 |
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.
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
There are several test cases in the input. The first line of each case contains 3 integers: n, W, H, indicating the number of stars, the horizontal length and the vertical height of the rectangle-shaped window. Then n lines follow,
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
For each test case, output the maximum brightness in a single line.
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
POJ Contest,Author:kinfkong@ZSU
题目大意:
就是给你一个n,w,h,下边n个星星,每个星星都有自己的坐标,亮度,,w h表示窗户的宽和高,然后问这个窗户能包括的最大的亮度和
思路:
就是跟扫描线,扫矩形一样,(我咋感觉这就是扫描线啊,,)对每个星星作为窗户的左下角建窗户,让他对y轴投影,每个矩形窗户的左右两个边有val值,左边是正的,右边的是负的,把这些所有的和y轴平行的线段按x排序,(要是x相等,负的在前边,先减去他,再往下加),然后从左往右扫,扫到后就update,他对y轴的投影区域都加val,然后问题就转化成了,求所有的区间的最大值了,从左扫到最后就行了,被上个题的__int64 坑怕了,丧心病狂的我都用的64位,,,pushdown中右子树的maxn写成了add,wa了,两次,卧槽,我都不知道我在想些什么
ac代码
Problem: 2482 User: kxh1995 Memory: 1960K Time: 172MS Language: C++ Result: Accepted
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; #define max(a,b) (a>b?a:b) #define N 10010 struct Seg { __int64 x,y1,y2,val; }seg[N<<1]; struct s { __int64 maxn,add; }node[N<<3]; __int64 a[N<<2]; int cmp(Seg a,Seg b) { if(a.x==b.x) return a.val<b.val; return a.x<b.x; } void build(__int64 l,__int64 r,__int64 tr) { node.add=0; node .maxn=0; if(l==r) return; int mid=(l+r)>>1; build(l,mid,tr<<1); build(mid+1,r,tr<<1|1); } __int64 bseach(__int64 key,__int64 n) { __int64 l,r; l=0; r=n-1; while(l<=r) { int mid=(l+r)>>1; if(a[mid]==key) return mid; if(a[mid]<key) l=mid+1; else r=mid-1; } return l; } void pushup(__int64 tr) { node .maxn=max(node[tr<<1].maxn,node[tr<<1|1].maxn); } void pushdown(__int64 tr) { if(node .add) { node[tr<<1].add+=node .add; node[tr<<1|1].add+=node .add; node[tr<<1|1].maxn+=node .add; node[tr<<1].maxn+=node .add; node .add=0; } } void update(__int64 L,__int64 R,__int64 l,__int64 r,__int64 tr,__int64 val) { if(L<=l&&r<=R) { node .add+=val; node .maxn+=val; //printf("%I64d %I64d\n",tr,node .maxn); return; } pushdown(tr); int mid=(l+r)>>1; if(L<=mid) update(L,R,l,mid,tr<<1,val); if(R>mid) update(L,R,mid+1,r,tr<<1|1,val); pushup(tr); } int main() { __int64 n,w,h; while(scanf("%I64d%I64d%I64d",&n,&w,&h)!=EOF) { __int64 cnt=0,i; for(i=0;i<n;i++) { scanf("%I64d%I64d%I64d",&seg[i].x,&seg[i].y1,&seg[i].val); a[cnt++]=seg[i].y1; a[cnt++]=seg[i].y1+h; seg[i].y2=seg[i].y1+h; seg[i+n].x=seg[i].x+w; seg[i+n].y1=seg[i].y1; seg[i+n].y2=seg[i].y1+h; seg[i+n].val=-seg[i].val; } sort(seg,seg+(n<<1),cmp); sort(a,a+cnt); __int64 k=(__int64)(unique(a,a+cnt)-a); build(1,k,1); __int64 ans=0; for(i=0;i<(n<<1);i++) { __int64 l=bseach(seg[i].y1,k)+1; __int64 r=bseach(seg[i].y2,k); if(l>r) swap(l,r); update(l,r,1,k,1,seg[i].val); if(node[1].maxn>ans) ans=node[1].maxn; } printf("%I64d\n",ans); } } 相关文章推荐
- 多线程和异步
- textView设置onclick:xxx无效
- HDU5001-Walk-AsiaRegionalAnshanOnline2014 (DP问题)
- hdu2050 折线分割平面
- C++ 虚函数表解析
- CURLOPT_NOSIGNAL
- 常见漏洞第三方扫描器
- python 编码问题:'ascii' codec can't encode characters in position 的解决方案
- 【Unlock xcode files?】
- 如何迅速将磁盘中文件目录树结构做成文本保存
- 修改Eclipse jdk环境
- [CSS] Animating SVG
- debug签名 和 release签名
- Expression Tree Introduction
- Android 录音PCM 转G711U,非常简单,非FFMPEG
- [Search Engine] 搜索引擎技术之网络爬虫
- swift 学习<五> String
- 通过SQL语句直接实现Excel与数据库的导入导出
- hdu2049 不容易系列之(4)——考新郎
- 【POJ3020】【总数-最大二分匹配】【最小路径覆盖和点覆盖的区别是 路径的话要所有点覆盖了】