ACM: 线段树 poj 2482 煽情的情书!…

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.
ACM: <wbr>线段树 <wbr>poj <wbr>2482 <wbr>煽情的情书!!

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

题意: 故事背景竟然是一封情信, 好煽情啊啊啊!!! 一个矩形框住的大小内, 每个星星的亮度和最大.

 

解题思路:

      1. 《黑书》上的金矿一题相同, 只是将计算个数变成了计算最大和. 这题主要是将坐标点离散化.

          从书上学到一种巧妙的离散方法.

         (1).对于X轴: 用矩形的长度作为范围, 用L1, L2两个竖线来确定一个范围, 在这里我们先不

             考虑Y轴范围, 用矩形(W,H)的中线来将每颗星星划分.如图:

               ACM: <wbr>线段树 <wbr>poj <wbr>2482 <wbr>煽情的情书!!

              将一颗星星的一分为二的线在矩形左右两边作为插入和删除的作用, 当星星第一次进入矩形 ,

              因为在W的范围内, 所以是插入(加上它的亮度), 当第二次出现时, 因为超出矩形的范围,所以

              是删除(把星星的亮度设为负数,然后插入相当于删除).

          (2). 对于Y轴, 我们用Y轴来作为线段树的左右子树, 因此在X轴分为左右两条线的基础上加上Y轴

               的离散, line[i*2]->l = y-(H/2-0.1), line[i*2]->r = y+(H/2-0.1);

                       line[i*2+1]->l = y-(H/2-0.1), line[i*2+1]->r = y+(H/2-0.1);

       2. 通过X,Y轴的离散化之后, 使用线段树, 可以将原本二维问题变成一维问题求解. 节点设置两个域

          maxsum: 当前插入线段的最大星星亮度和, val: 当前区间的星星亮度. 这里的区间可以理解为:

          前面所有插入的星星离散后的line[i*2],line[i*2+1], 在矩形范围内扫描的Y轴区间.

          (1). 当星星搜索到相应的区间时, p->maxsum = p->maxsum+val,

                                         p->val = p->val+val, (星星重叠在这个Y轴范围内).

          (2). 当搜索区间满足后, 回溯要相应的更新maxsum.

               p->maxsum = max(p->left->maxsum, p->right->maxsum) + p->val;

               这样一直回溯到根节点, 就相当于将插入新的星星之后更新了线段树最大值.

       3. 问题基本解决, 剩下是细节问题, 问题给的范围不准确, [1,10000]的范围不行, [1,100000]

          才行. 因为范围扩大好多, 在查找l,r离散后的范围时, 用线性查找会TLE, 后面改成二分查找

          AC.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 100005

struct Line
{
 double l, r, x;
 int val, flag;
}line[MAX*2];

struct node
{
 int l, r;
 int maxsum, val;
}pt[MAX*4];

int n, W, H;
double yt[MAX*2];
int x, y, c;

inline int max(int a, int b)
{
 return a > b ? a : b;
}

bool cmp(Line a, Line b)
{
 return a.x < b.x;
}

void buildTree(int l, int r, int pos)
{
 pt[pos].l = l, pt[pos].r = r;
 pt[pos].maxsum = pt[pos].val = 0;
 if(l == r) return ;
 int mid = (l+r)/2;
 buildTree(l, mid, pos*2);
 buildTree(mid+1, r, pos*2+1);
}

void insert(int l, int r, int val, int pos)
{
 if(l > pt[pos].r || r < pt[pos].l)
  return ;
 if(pt[pos].l >= l && pt[pos].r <= r)
 {
  pt[pos].maxsum += val;
  pt[pos].val += val;
  return ;
 }
 insert(l, r, val, pos*2);
 insert(l, r, val, pos*2+1);
 pt[pos].maxsum = max(pt[pos*2].maxsum, pt[pos*2+1].maxsum)+pt[pos].val;
}

int find(double a)
{
 int l = 0;
 int r = 2*n-1;
 while(l < r)
 {
  int mid = (l+r)/2;
  if(yt[mid] <= a)
   l = mid+1;
  else
   r = mid;
 }
 return l;
}

int main()
{
 int i;
// freopen("input.txt","r",stdin);
 while(scanf("%d %d %d",&n, &W, &H) != EOF)
 {
  double xx = W/2.0-0.1;
  double yy = H/2.0-0.1;
  for(i = 0; i < n; ++i)
  {
   scanf("%d %d %d",&x, &y, &c);
   line[2*i].l = y-yy;
   line[2*i].r = y+yy;
   line[2*i].val = c;
   line[2*i].x = x-xx;
   line[2*i].flag = 1;

   line[2*i+1].l = y-yy;
   line[2*i+1].r = y+yy;
   line[2*i+1].val = -c;
   line[2*i+1].x = x+xx;
   line[2*i+1].flag = -1;

   yt[2*i] = y-yy;
   yt[2*i+1] = y+yy;
  }

  sort(yt, yt+2*n);
  sort(line, line+2*n, cmp);
  buildTree(0, 2*n-1, 1);

  int result = 0;
  for(i = 0; i < 2*n; ++i)
  {
   if(line[i].flag > 0)
   {
    insert(find(line[i].l), find(line[i].r), line[i].val, 1);
    result = max(result, pt[1].maxsum);
   }
   else
    insert(find(line[i].l), find(line[i].r), line[i].val, 1);
  }
  printf("%d\n",result);
 }

 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值