ACM Learning process--noj1113 Game I (Greedy | | Line tree)

Source: Internet
Author: User
Tags cmath

Description

Nick invented such a game: on an axis, there are some circles, the center of the circle is on the x-axis, now given an x-axis point, to ensure that the point is not within these circles (and the circle), Nick can do this point as the center of the circle of any size, he wants to know how many can be with a given circle ( Tangent is also counted, contains not counted).

Input

Input has multiple sets of data input to the end of the file

Each set of data has an integer n (1<=n<=100000) that represents a total of n circles.

Next is n rows, two integers per line Xi,ri represents the Circle's center coordinate and radius.

The next line is an integer x, which represents the location of the nick pick point.

Range of X-XI [ -10^9,10^9] ri ranges [1,10^9]

A total of up to 10 sets of data.

Output

Each set of data outputs a row indicating how many circles the nick can cover.

Sample Input
21 22 14
Sample Output
2

The condition of this problem is to satisfy the |r-d| <= R <= r+d R can intersect a circle with R radius, where D is the distance between the center of the circle.

This becomes the interval increment, and then the maximum value in the interval is queried.

The first thought is the line tree, the complexity of O (2n*log (2n)). However, since the value of the radius range is discrete, map is used to make it continuous. But the AC takes around 500ms.

And then found that the direct processing after direct greedy on the line. Sort the left and right end points of all the intervals, and you need to save the tag to record whether the endpoint is the left endpoint of a certain interval or an end. Then sweep again, for the natural value of the left endpoint plus one, the natural value of the right endpoint minus one, and then greedy process of the maximum value. The AC takes around 85ms.


Greedy Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<map>#include<Set>#include<algorithm>#defineLL Long Longusing namespacestd;structnode{LL Index; BOOLIsleft;} ind[200005];intN, ans; LL x[100005], r[100005], XX;BOOLCMP (Node A, Node B) {returnA.index <B.index;} ll Abs (ll aa) {if(AA <0)        return-AA; Else        returnAA;}voidInit () {LL D, left, right;  for(inti =0; I < n; ++i) {d= Abs (x[i]-xx); Left= Abs (r[i]-d); Right= r[i]+D; Ind[i<<1].index =Left ; Ind[i<<1].isleft =true; Ind[i<<1|1].index =Right ; Ind[i<<1|1].isleft =false; } sort (IND, Ind+2*N, CMP);}intMain () {//freopen ("test.in", "R", stdin);     while(SCANF ("%d", &n)! =EOF) {         for(inti =0; I < n; ++i) {scanf ("%lld%lld", &x[i], &R[i]); } scanf ("%lld", &xx);        Init (); intLen =2*N; intnow =0; Ans=0;  for(inti =0; i < Len; ++i) {if(ind[i].isleft) now++; Else Now--; Ans=Max (ans, now); } printf ("%d\n", ans); }    return 0;}

Line segment Tree Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<map>#include<Set>#include<algorithm>#defineLL Long Longusing namespacestd;//Segment Tree//increment each point of interval to find the most value of intervalConst intMAXN =100005;structnode{intLT, RT; intVal, add;} tree[8*MAXN];//Update downvoidPushdown (intID) {    if(Tree[id].add! =0) {Tree[id<<1].add + =Tree[id].add; Tree[id<<1].val + =Tree[id].add; Tree[id<<1|1].add + =Tree[id].add; Tree[id<<1|1].val + =Tree[id].add; Tree[id].add=0; }}//Update upvoidPushup (intID) {Tree[id].val= Max (tree[id<<1].val, tree[id<<1|1].val);}//Create a line segment treevoidBuild (intLtintRtintID) {tree[id].lt=lt; Tree[id].rt=RT; Tree[id].val=0;//the initial value of each paragraph, according to the topic requirementsTree[id].add =0; if(LT = =RT) {        //Tree[id].add =??;        return; }    intMid = (lt + rt) >>1; Build (LT, Mid, id<<1); Build (Mid+1, RT, id<<1|1); //pushup (ID);}//increase the fixed value of each point within the intervalvoidADD (intLtintRtintIdintpls) {    if(LT <= tree[id].lt && RT >=tree[id].rt) {Tree[id].add+=pls; Tree[id].val+=pls; return;    } pushdown (ID); intMid = (tree[id].lt + tree[id].rt) >>1; if(LT <=mid) Add (LT, RT, id<<1, pls); if(Rt >mid) Add (LT, RT, id<<1|1, pls); Pushup (ID);}//querying for Zuizhi within a rangeintQuery (intLtintRtintID) {    if(LT <= tree[id].lt && RT >=tree[id].rt)returnTree[id].val;    Pushdown (ID); intMid = (tree[id].lt + tree[id].rt) >>1; if(Rt <=mid)returnQuery (LT, RT, id<<1); if(LT >mid)returnQuery (LT, RT, id<<1|1); returnMax (LT, RT, id<<1), Query (LT, RT, id<<1|1));}intN, CNT; LL x[100005], r[100005], ind[200005], XX; LL left[100005], right[100005];map<ll,int>ID;BOOLCMP (ll A, ll b) {returnA <b;} ll Abs (ll aa) {if(AA <0)        return-AA; Else        returnAA;}voidInit () {id.clear ();    LL D;  for(inti =0; I < n; ++i) {d= Abs (x[i]-xx); Left[i]= Abs (r[i]-d); Right[i]= r[i]+D; Ind[i<<1] =Left[i]; Ind[i<<1|1] =Right[i]; } sort (IND, Ind+2*N, CMP); intLen =2*N; CNT=1;  for(inti =0; i < Len; ++i) {if(i = =0) {id[ind[0]] =1; Continue; }        if(Ind[i]! = ind[i-1]) {Id[ind[i]]= ++CNT; }} Build (1Cnt1);}intMain () {//freopen ("test.in", "R", stdin);     while(SCANF ("%d", &n)! =EOF) {         for(inti =0; I < n; ++i) {scanf ("%lld%lld", &x[i], &R[i]); } scanf ("%lld", &xx);        Init ();  for(inti =0; I < n; ++i) {ADD (Id[left[i]], Id[right[i]],1,1); } printf ("%d\n", Query (1Cnt1)); }    return 0;}

ACM Learning process--noj1113 Game I (Greedy | | Line tree)

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.