POJ 2482 Stars in Your Window (segment tree scan line)

Source: Internet
Author: User

http://poj.org/problem?id=2482

Stars in Your Window
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9909 Accepted: 2743

Description

Fleeting time does not blur my memory for you. Can It really is 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 Walki Ng out of the classroom and turned your head back, with the soft sunset glow shining on your rosy cheek, I knew, I knew th At I is already drunk on you. Then, after several months ' observation and prying, your grace and your wisdom, your attitude to life and your aspiration For the were all strongly impressed on my memory. You were the glamorous and sunny girl whom I always dream of share the rest of my life with. Alas, actually you were far beyond my wildest dreams and I had no idea on how to bridge that gulf between you and me. So I schemed and 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 ins Tead of just waiting. 

These days, has parted with friends, roommates and classmates one after another, I still cannot believe the fact that a Fter waving hands, these familiar faces would soon vanish from our life and become no more than a memory. I'll move out from school tomorrow. And you is planning to fly far far from away, to pursue your the future and fulfill your dreams. Perhaps we'll not meet each of the more if without fate and luck. So tonight, I is 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 is not able to belch out a word. I cannot remember how many times I had passed your dormitory building both in Zhuhai and Guangzhou, and each time aspire D to see you appear in the balcony or your silhouette this cast on the window. I cannot remember how many times this idea comes to my mind:call she out to has 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 would 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 "the" to bright prospect. I truly hope you'll be happy everyday abroad and everything goes well. Meanwhile, I'll try to get out from puerility and become more sophisticated. To pursue my own love and happiness here in reality would be my ideal I never desert.

Farewell, my princess!

If Someday, somewhere, we have a chance to gather, even as gray-haired mans and woman, at the 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 Fri Ends, 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 in it with a location (x, y). For each star, there are a grade ranging from 1 to 1, representing its brightness, where are the brightest and is th E weakest. The window is a rectangle whose edges was 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 is right on the edge of the Windows does not count. The window can be translated but rotation are not allowed.

Input

There is several test cases in the input. The first line of all case contains 3 integers:n, W, H, indicating the number of stars, the horizontal length and the VE rtical 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. The stars is on the same point.

There is 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, the output of the maximum brightness in a single line.

Sample Input

3 5 41 2 32 3 26 3 13 5 41 2 32 3 25 3 1

Sample Output

56

Source

POJ Contest,author:[email protected]


Test instructions

The background is really touching, I can see the cry ...

See for yourself in front of you.

Give the coordinates and brightness of the stars, given a rectangle (length/width), the maximum value of the star brightness and the frame, just on the edge of not counting.

Analysis:

One months ago in the Shanghai Invitational, there is such a problem, when my line tree will only be a paragraph update, so the topic also did not touch ...

We think that the extent to which each star can affect is the area of a given rectangle, for convenience, it is advisable to set the position of the star as the lower-left corner of the rectangle.

Then the point in this range is all added to its brightness, and ultimately we are asking for the maximum value in that plane.

This will use the two-dimensional line tree ... But there are 10k stars ... How big an array it is to open ...

So is there a more elegant way?

Of course it is!

Think about whether to reduce the dimension, a star affects the range is limited, such as over-reduced to one-dimensional, then in this segment tree we can maintain the impact of the x-axis (interval plus, maintain the maximum), then the y-axis influence how to control it? Think of the segment tree scan line to find the rectangular area and. We just have to remove the effect of the point after it has been affected.

The method is obvious: For each star, we construct two edges: a range [x,x+w], a height of y, a weight of brightness, another range [x,x+w], a height of y+h, and a brightness of-brightness. So we sort all the edges, from low to high scans. Attention to discretization and boundary processing (rectangular edges do not count, I am accustomed to maintain left closed to open the interval so do not have to tube).


And because the wrong one variable WA a day more, so ugly!

/* * *author:fcbruce * *date:2014-08-21 09:19:53 * */#include <cstdio> #include <iostream> #include <sstrea m> #include <cstdlib> #include <algorithm> #include <ctime> #include <cctype> #include < cmath> #include <string> #include <cstring> #include <stack> #include <queue> #include < list> #include <vector> #include <map> #include <set> #define SQR (x) ((x) * (x)) #define LL Long long# Define ITN int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#ifdef _win32#define lld "%I64D" # Else#define LLD "%lld" #endif # define MAXM 20007#define maxn 20007using namespace std;struct __edge{long long a,b,y,brightn Ess;bool operator < (const __edge &e) const{if (y==e.y) return Brightness<e.brightness;return y<e.y;}} Edge[maxm];long long Maxv[maxn<<2],addv[maxn<<2];long long x[maxn];inline void pushup (int k) {Maxv[k]=max ( MAXV[K*2+1],MAXV[K*2+2]);} inline void pushdown (int k) {if (Addv[k]) {int lc=k*2+1,rc=k*2+2;addv[lc]+=addv[k];addv[rc]+=addv[k];maxv[lc]+=addv[k];maxv[rc]+=addv[k];addv[k]=0;}} void update (int a,int b,long long v,int k,int l,int R) {if (B<=l | | r<=a) return; if (a<=l && r<=b) {ADDV [K]+=v;maxv[k]+=v;return;} Pushdown (k); update (a,b,v,k*2+1,l,l+r>>1); update (A,B,V,K*2+2,L+R&GT;&GT;1,R);p ushup (k);} int main () {#ifndef online_judgefreopen ("/home/fcbruce/document/code/t", "R", stdin); #endif//Online_judgelong long n,w,h; while (~scanf (LLD LLD lld,&n,&w,&h)) {memset (maxv,0,sizeof maxv); memset (addv,0,sizeof ADDV); LL x,y,brightness;for (int i=0;i<n;i++) {scanf (LLD lld lld,&x,&y,&brightness); edge[i]= (__edge) {x,x+w, Y,brightness};edge[i+n]= (__edge) {x,x+w,y+h,-brightness}; X[i]=x; X[i+n]=x+w;} N<<=1;sort (edge,edge+n); sort (x,x+n); int M=unique (x,x+n)-x;long long max=0;for (int i=0;i<n;i++) {int A=lower _bound (X,X+M,EDGE[I].A)-x;int b=lower_bound (x,x+m,edge[i].b)-x;update (a,b,edge[i].brightness,0,0,m); Max=max (Max,maxv[0]);} Printf (LLD "\ n", MAX);} return 0;}



POJ 2482 Stars in Your Window (segment tree scan line)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.