HDU 5091 Beam Cannon

Source: Internet
Author: User

Review a bit of line tree, only to find the line tree or do too little, many forget

Segment tree has the following functions: RMQ, Interval sum query, single point update, interval update

Forgot to delay the update when the interval was updated, here at the beginning WA.

Say the idea of the problem:

With each point as the lower-left corner of the rectangle, draw all the rectangles to the upper-right corner of the rectangle with any point in the area with the most overlap of the rectangle, which is the rectangle we requested.

That is, we ask for the maximum number of times the rectangle is covered, so that the scan line can be solved, but the direct scan of the past to n^2 complexity, will time out, so use line tree

Each scan to an edge, that is equivalent to the point covered in the y-axis range of all the values have been updated, that is, the interval update, so that the maintenance of a maximum value is good

#include <stdio.h> #include <algorithm>using namespace std; #define N 20005struct line{int X,y1,y2,flag;} S[n];int y[n];int max (int a,int b) {return a>b?a:b;} struct Node{int l,r,ma,add;} Node[n*4];int CMP (line A,line b) {if (a.x==b.x) return A.flag>b.flag;return a.x<b.x;} void Build (int cur,int l,int r) {//[l,r) node[cur].l=l;node[cur].r=r;node[cur].ma=0;node[cur].add=0;if (l==r) {// The current node is L return;} int mid= (L+R) >>1; Build (Cur*2,l,mid); Build (cur*2+1,mid+1,r);} void Push_down (int cur) {node[cur*2].add+=node[cur].add;//left the value of the grandson node node[cur*2+1].add+=node[cur].add;node[cur*2].ma +=node[cur].add;node[cur*2+1].ma+=node[cur].add;node[cur].add=0;} void Update (int cur,line p) {//To pay attention to deferred updates, if each update to a leaf node the complexity becomes n^2, if (y[node[cur].l]>p.y2| | Y[NODE[CUR].R]&LT;P.Y1) return;if (y[node[cur].l]>=p.y1&&y[node[cur].r]<=p.y2) {node[cur].ma+= P.flag; node[cur].add+=p.flag;//The current node add is the return that needs to be added to the child node;} /*if (NODE[CUR].L==NODE[CUR].R) {return;//Degenerate to a point, jump out of the}*///this sentence has the preceding conditions, do not write, either completely contains, or completely does not contain, the otherwill continue to look for Push_down (cur), int mid= (NODE[CUR].L+NODE[CUR].R) >>1;update (cur*2,p); update (cur*2+1,p); node[cur].ma =max (node[cur*2+1].ma,node[cur*2].ma);//can perform this step, stating that the value of the child node has been updated}int main () {#ifndef online_judgefreopen ("In.txt", "R" , stdin); #endifint n,w,h;while (scanf ("%d%d%d", &n,&w,&h)) {if (n<0) break;int tmp1,tmp2;int cnt=1;int i; for (i=0;i<n;i++) {scanf ("%d%d", &AMP;TMP1,&AMP;TMP2); s[cnt].x=tmp1;s[cnt].y1=tmp2;s[cnt].y2=tmp2+h;y[cnt]= Tmp2;s[cnt++].flag=1;s[cnt].x=tmp1+w;s[cnt].y1=tmp2;s[cnt].y2=tmp2+h;y[cnt]=tmp2+h;s[cnt++].flag=-1;} Sort (y+1,y+cnt); sort (s+1,s+cnt,cmp); int Ycnt=unique (y+1,y+cnt)-y; Build (1,1,ycnt-1); int ans=0;for (i=1;i<cnt;i++) {update (1,s[i]); Ans=max (ans,node[1].ma);} printf ("%d\n", ans);}}


HDU 5091 Beam Cannon

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.