"POJ 2482" Stars in Your window (segment tree + discretization + scan line)

Source: Internet
Author: User
Tags fread

"POJ 2482" Stars in Your window (segment tree + discretization + scan line)

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11294 Accepted: 3091

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]


It is very important to investigate a problem of comprehensive application.

Anyway, I was stuck for a long time =.=

First of all, the main idea: there are n stars in the sky (1 <= n <= 10000) each star has a coordinate (x, y) (0 <= x, b < 2^31) and luminance C (1 <= C <= 100)

You have a rectangular box wide w height h ask how the box can let the stars in the box brightness and maximum

Also the brightness of the stars on the border does not count toward


Intuitive look no idea ... I have no idea ...... ....... ........................... Violent words the choice and the selection of the stars will lead to a lot of states do not want to forget +.

+

Since it is divided into the Segment tree topic, as far as possible to the line tree by Bai ...

Line-segment tree is a two-dimensional interval, but only one-dimensional interval is supported. Only one dimension can be limited in such a way that the brightness of multiple intervals for all Y to traverse to an X is easy to find.

Fixed x needs a scan line. By sorting the Stars by the X order so that all x scans each x to increase the brightness of the star in the corresponding Y-range.

But just add brightness to meet the left side of the boundary also need to exceed the W width limit when the front of the star brightness from the interval to take away

That is, the method of splitting line segments with a scan line increases the value of the block at the starting point minus the end of the X for the beginning of the question is the x+w, which is the border from the farthest touch of X.


So that each point is split into two parts is the starting point brightness is positive and one is the end of the brightness opposite for all the 2*n points sorted first by x sort x the same value is negative in front

Because the stars on the border are not counted, the brightness of the stars on the border must be subtracted before the new stars are added.


In this case, the X is processed, and each traversal to a point in the Y interval increases its luminance (or positive or negative) Y-band in the case of the "y,y+h-1", which is the interval in which it can contribute value.

But Y is so big that it involves a discretization problem. Sort the values of all occurrences of Y and then discretization to


Don't know why g++ always re ... Where could I write a setback?

。 There is g++ A of the great God or his level is not enough AH ~ ~.


The code is as follows:

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm > #include <map> #include <set> #define LL long long#define Pr pair<int,int> #define FREAD () freopen (" In.in "," R ", stdin) #define FWRITE () freopen (" Out.out "," w ", stdout) using namespace std;const int INF = 0x3f3f3f3f;const int MSZ = 10000;const int mod = 1e9+7;const double eps = 1e-8;struct star{ll x,y1,y2,s;bool operator < (const struct Star A ) Const{return x = = a.x? s < 0:x < a.x;}}; LL sum[80004]; LL add[80004]; Star seg[20004]; LL ny[20004]; LL TP; ll W,h;void Update (ll root,ll l,ll r,ll y1,ll y2,ll d) {//printf ("L:%lld r:%lld\n", ny[l],ny[r]); if (ny[l] = = Y1 && n Y[r] = = y2) {//printf ("Add:%lld", Add[root]); Add[root] + = d;//printf ("->%lld\n", Add[root]);//printf ("Sum:%lld", Sum[root]); Sum[root] + = d;//printf ("->%lld\n", Sum[root]); return;} LL mid = (l+r) >>1;if (Add[root]) {sum[root<<1|1] + = add[root];add[root<<1|1] + = Add[root];sum[root &LT;&LT;1] + = add[root];add[root<<1] + add[root];add[root] = 0;} if (Ny[mid] >= y2) update (root<<1,l,mid,y1,y2,d), else if (ny[mid+1] <= y1) update (ROOT&LT;&LT;1|1,MID+1,R, Y1,Y2,D); else{update (root<<1,l,mid,y1,ny[mid],d); Update (root<<1|1,mid+1,r,ny[mid+1],y2,d);} Sum[root] = max (sum[root<<1],sum[root<<1|1]);} int main () {//fread ();//fwrite (); ll N;while (~scanf ("%lld%lld%lld", &n,&w,&h)) {for (LL i = 1; I <= n; ++i) {scanf ("%lld%lld%lld", &seg[i] . x,&seg[i].y1,&seg[i].s); seg[i].y2 = Seg[i].y1+h-1;seg[i+n] = seg[i];seg[i+n].x = SEG[I].X+W;SEG[I+N].S =-seg [I].s;ny[i] = seg[i].y1;ny[i+n] = seg[i].y2;} if (w = = 0 | | h = = 0) {puts ("0"); continue;} Sort (ny+1,ny+2*n+1), sort (seg+1,seg+2*n+1), TP = 0;for (LL i = 1; I <= 2*n; ++i) if (i = = 1 | | ny[i]! = Ny[i-1]) NY[++TP] = n Y[i]; LL ans = 0;memset (sum,0,sizeof (sum)); Memset (Add,0,sizeoF (add)); for (LL i = 1; I <= 2*n; ++i) {//printf ("%lld%lld\n", seg[i].y1,seg[i].y2); Update (1,1,TP,SEG[I].Y1,SEG[I].Y2,SEG[I].S); ans = max (ans,sum[1]);} printf ("%lld\n", ans);} return 0;}




"POJ 2482" Stars in Your window (segment tree + discretization + 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.