POJ 2482 线段树+离散化

来源:互联网 发布:好听的男英文歌知乎 编辑:程序博客网 时间:2024/06/01 10:07

Stars in Your Window
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11203 Accepted: 3071

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个星星,每个星星有一个亮度。给一个矩形,问能用这个矩形包围的星星亮度和最大是多少。星星不能再矩形边上。

题解

通过建模而转化,我们以矩形中心代替这个矩形。则要想包围主某一个星星时,必然在以这个星星为中心的矩形内。那么将每个星星都画一个这样的矩形,分析他们的重叠部分就能得到答案。由于x和y的值过大,但是星星的个数不大。我们先离散化,以每个矩形和y轴平行的两个边来代表这个矩形。Edge(x,y1,y2,c),x为这个线段的横坐标,y1,y2为在y轴的投影长度。c为亮度。那么对于一个矩形我们用两个边来表示,且一个边的权值是c,一个是-c(这样扫描完后就减去权值)。细节处理上,我们每个点都存2*x1,2*y1,当每个点都这么处理后。每两个点之间的距离就扩大了2倍。然后用原来两倍大的矩形去圈,这样就避免了原来矩形长宽除2的误差。然后对x排序,如果x相同则按权值排序。这样当有边重叠时总是上一个矩形剪掉后再接下一个。然后由于我们把坐标都扩大了两倍,我们可以让y2-1,让上下边分离。就不会有星星在边上了。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <set>#include <map>#include <queue>#include <sstream>#include <vector>#define m0(a) memset(a,0,sizeof(a))#define mm(a) memset(a,0x3f,sizeof(a))#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)using namespace std;#define SIZE 10000typedef long long ll;struct TT{    ll T_SUM,T_MAX;};struct Po{    ll x,y1,y2,v;};TT T[SIZE*8+100];Po Edge[SIZE*2+100];ll Y[SIZE*2+100];ll NN;bool cmp(Po a,Po b){    if(a.x<b.x) return true;    else if(a.x==b.x){        if(a.v<b.v)            return true;    }    return false;}int getP(ll k){    return lower_bound(Y,Y+NN,k)-Y+1;}void update(ll L,ll R,ll C,ll t,ll l,ll r){    if(L<=l&&r<=R){        T[t].T_MAX += C;        T[t].T_SUM += C;        return ;    }    int m = (l+r)>>1;    if(L<=m)update(L,R,C,t<<1,l,m);    if(m<R)update(L,R,C,t<<1|1,m+1,r);    T[t].T_MAX = T[t].T_SUM+max(T[t<<1].T_MAX,T[t<<1|1].T_MAX);}int main(){    ll n,w,h;    while(~scanf("%I64d%I64d%I64d",&n,&w,&h)){        m0(T);        ll i,j = 0,k = 0;        f(i,1,n){            ll x,y,c;            scanf("%I64d%I64d%I64d",&x,&y,&c);            x*=2;y*=2;            Edge[j].x = x-w; Y[k++] = Edge[j].y1 = y-h; Y[k++] = Edge[j].y2 = y+h-1; Edge[j++].v = c;            Edge[j].x = x+w; Edge[j].y1 = y-h; Edge[j].y2 = y+h-1; Edge[j++].v = (-1)*c;        }        sort(Edge,Edge+j,cmp);        sort(Y,Y+k);        NN = k;        ll Max = -0x3f3f3f;        f(i,0,j-1){            ll L = getP(Edge[i].y1);            ll R = getP(Edge[i].y2);            update(L,R,Edge[i].v,1,1,NN);            Max = max(Max,T[1].T_MAX);        }        printf("%I64d\n",Max);    }    return 0;}
0 0
原创粉丝点击