POJ 3397 Stars in Your Window (线段树)

Stars in Your Window
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8175   Accepted: 2246

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

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的矩形去围这些星星,(边上的不算) 求能得到的最大亮度值。

思路:想了很久一直不懂 只能看别人的解题报告 。。。。
原来只要转换一下,就能把其转换为求线段区间的最大值 每个星星所能影响的范围[(x,y),(x+w-1,y+h-1)]且有一权值 它们重合就表示 能被这个矩形框在一起,也就是说,只要求出重合的矩形的权值最大就行了。

以x从小到大排序,y值离散化,投影到y轴上,那么对于每个星星的纵坐标,y,y+h-1就是每个星星可以影响到的矩形 然后x,x+w-1就是一个进入事件和一个出去事件,其所带的值互为相反数. node[1].sum 保存当前的最大值 当所有的矩形都遍历一遍 取其中的最大值就是ans

PS:看了讨论板说的,要用__int64 但我只把x,y,sum,add的值定义为__int64 其余为int 但 死活WA 本人认为 其它的变量按题目要求是不会超过int的范围,而且也有人说 全部都用int也能过, 但在无数次WA 之后,实在找不出原因,索性把全部变量都定义为__int64 就AC了 = = 。。。。 我还能说什么呢,不知代码哪里写挫了。
 
 
unique()  : 

unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),还有一个容易忽视的特性是它并不真正把重复的元素删除。他是c++中的函数,所以头文件要加#include<iostream.h>,具体用法如下:

    int num[100];

   unique(num,mun+n)返回的是num去重后的尾地址,之所以说比不真正把重复的元素删除,其实是,该函数把重复的元素一到后面去了,然后依然保存到了原数组中,然后返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

const int N=10010;

#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1

struct node1{
    long long x,y1,y2,val;
}seg[N<<1];

struct ndoe2{
    int l,r;     //这两变量我觉得真的不可能超过int
    long long sum,add;  //sum是该结点的最大亮度值,add是该区间被覆盖的值 用于延迟更新
}tree[N<<3];

long long y[N<<1];

int cmp(node1 a,node1 b){   //x从小到大,x相等,左边的边在前 因为是x+w-1
    if(a.x!=b.x)
        return a.x<b.x;
    return a.val>b.val;
}

void build(long long l,long long r,int rt){
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].sum=tree[rt].add=0;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
}

void PushDown(int rt){
    tree[L(rt)].sum+=tree[rt].add;
    tree[L(rt)].add+=tree[rt].add;
    tree[R(rt)].sum+=tree[rt].add;
    tree[R(rt)].add+=tree[rt].add;
    tree[rt].add=0;
}

void update(long long val,long long l,long long r,int rt){
    if(l==y[tree[rt].l] && y[tree[rt].r]==r){
        tree[rt].sum+=val;
        tree[rt].add+=val;
        return ;
    }
    if(tree[rt].l==tree[rt].r)
        return ;
    if(tree[rt].add)
        PushDown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(r<=y[mid])
        update(val,l,r,L(rt));
    else if(l>=y[mid+1])
        update(val,l,r,R(rt));
    else{
        update(val,l,y[mid],L(rt));
        update(val,y[mid+1],r,R(rt));
    }
    tree[rt].sum=max(tree[L(rt)].sum,tree[R(rt)].sum);  //父结点是儿子结点中最大的一个,而不是它们的和
}

int main(){

    //freopen("input.txt","r",stdin);

    int n,w,h;
    while(~scanf("%d%d%d",&n,&w,&h)){
        for(int i=0;i<n;i++){
            scanf("%I64d%I64d%I64d",&seg[i].x,&seg[i].y1,&seg[i].val);
            y[i*2+1]=seg[i].y1; //y保存实际值 下标从1开始
            y[i*2+2]=seg[i].y1+h-1;
            seg[i].y2=seg[i].y1+h-1;
            seg[n+i]=seg[i];
            seg[n+i].x=seg[i].x+w-1;
            seg[n+i].val=-seg[i].val;   //出边的val为负,表不在这矩形里
        }
        sort(y+1,y+2*n+1);
        sort(seg,seg+2*n,cmp);
        int cnt=unique(y+1,y+2*n+1)-y-1;    // 离散 ,unique 去重函数
        build(1,cnt,1);
        long long ans=0;
        for(int i=0;i<2*n;i++){
            update(seg[i].val,seg[i].y1,seg[i].y2,1);
            ans=max(ans,tree[1].sum);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jackge/p/3037065.html