POJ 2482 Stars in Your Window (segment tree: Interval update)

Source: Internet
Author: User

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

It's a little sad to finish reading the question (?????? Test instructions: There are n stars (the coordinates of the star I are XI, Yi, Brightness is ci), give you a w*h rectangle, let you find the rectangle can cover the brightness of the stars and the maximum of how much

Idea: The rectangular size is fixed, so you can think in a different direction, the rectangle as a point (coordinates are the center of the rectangle), each star's influence interval range is w*h rectangle (the original coordinates of the star is the center of the rectangle), the brightness of the interval increased C. The problem becomes the one that asks which point is the most luminance. The coordinate range is too large and the direct brute force enumeration is not possible, so it needs to be discretized beforehand. Create a line segment tree vertically, and then enumerate the horizontal axis, updating the vertical coordinate interval that needs to be updated each time on the segment tree, and then querying.

#include <iostream>#include<cstring>#include<algorithm>#defineMAXN 10010#defineINF 1000000000#defineLL (x) x<<1#defineRR (x) x<<1|1using namespaceStd;typedefLong LongLL;//variable definestructline{intx, y1, y2, Light;};structtree{intL, R; intadd; LL Ma;}; Tree NODE[MAXN*8]; LL W, H, STARX[MAXN], STARY[MAXN], XX[MAXN*2], yy[maxn*2];intLIGHT[MAXN], n;line li[maxn*4];//function DefinevoidPush_down (intx);voidPUSH_UP (intx);voidBuild_tree (intLeftintRightintx); LL Query (intLeftintRightintx);voidUpdate_add (intLeftintRightintx, LL val);BOOLLine_compare (line L1, line L2);intMainvoid){     while(SCANF ("%d%lld%lld", &n, &w, &h)! =EOF) {         for(inti =0; I < n; ++i) {scanf ("%lld%lld%d", &starx[i], &stary[i], &Light[i]); Starx[i]*=2; Stary[i]*=2; }         for(inti =0; I < n; ++i) {xx[i*2] = Starx[i]-W; Xx[i*2+1] = Starx[i] +W; Yy[i*2] = Stary[i]-H; Yy[i*2+1] = Stary[i] + H-1; } sort (xx, xx+2*N); Sort (yy, yy+2*N);  for(inti =0; I < n; ++i) {intx, y1, y2; X= (int) (Lower_bound (xx, XX +2*n, Starx[i]-W)-xx); Y1= (int) (Lower_bound (yy, yy +2*n, Stary[i]-H)-yy); Y2= (int) (Lower_bound (yy, yy +2*n, Stary[i] + H-1) -yy); Li[i*2].x =x; Li[i*2].y1 =Y1; Li[i*2].y2 =Y2; Li[i*2].light =Light[i]; X= (int) (Lower_bound (xx, XX +2*n, Starx[i] + W)-xx); Li[i*2+1].x =x; Li[i*2+1].y1 =Y1; Li[i*2+1].y2 =Y2; Li[i*2+1].light =-1*Light[i]; } build_tree (1,2*n,1); LL ans=0; Sort (Li, Li+2*N, Line_compare);  for(inti =0; I <2*n; ++i) {Update_add (li[i].y1+1, Li[i].y2 +1,1, Li[i].light); Ans= max (ans, query (li[i].y1 +1, Li[i].y2 +1,1)); } printf ("%lld\n", ans); }    return 0;}voidBuild_tree (intLeftintRightintx) {NODE[X].L=Left ; NODE[X].R=Right ; Node[x].add= Node[x].ma =0; if(left = =Right )return; intLX =LL (x); intRx =RR (x); intMid = left + (right-left)/2;    Build_tree (left, mid, LX); Build_tree (Mid+1, right, RX); PUSH_UP (x);}voidPUSH_UP (intx) {    if(Node[x].l >=NODE[X].R)return; intLX =LL (x); intRx =RR (x); Node[x].ma=Max (node[lx].ma, node[rx].ma);}voidPush_down (intx) {    if(Node[x].l >=NODE[X].R)return; intLX =LL (x); intRx =RR (x); if(Node[x].add! =0) {Node[lx].add+=Node[x].add; Node[rx].add+=Node[x].add; Node[lx].ma+=Node[x].add; Node[rx].ma+=Node[x].add; }}voidUpdate_add (intLeftintRightintx, LL val) {    if(NODE[X].L = = Left && NODE[X].R = =Right ) {Node[x].add+=Val; Node[x].ma+=Val; return;    } push_down (x); Node[x].add=0; intLX =LL (x); intRx =RR (x); intMID = Node[x].l + (NODE[X].R-NODE[X].L)/2; if(Right <=mid) Update_add (left, right, LX, Val); Else if(Left >mid) Update_add (left, right, Rx, Val); Else{Update_add (left, Mid, LX, Val); Update_add (Mid+1, right, Rx, Val); } push_up (x);} LL Query (intLeftintRightintx) {    if(NODE[X].L = = Left && NODE[X].R = =Right ) {        returnnode[x].ma;    } push_down (x); Node[x].add=0; intMID = Node[x].l + (NODE[X].R-NODE[X].L)/2; intLX =LL (x); intRx =RR (x);    LL result; if(Right <=mid) Result=query (left, right, LX); Else if(Left >mid) Result=query (left, right, RX); Elseresult= max (query (left, Mid, LX), query (mid +1, right, Rx));    PUSH_UP (x); returnresult;}BOOLLine_compare (line L1, line L2) {if(l1.x! =l2.x)returnl1.x <l2.x; returnL1.light <l2.light;}

POJ 2482 Stars in Your Window (segment tree: Interval update)

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.