ZOJ 3597 hit the target! (Segment tree Scan line-the maximum number of points the rectangle can cover)

Source: Internet
Author: User

ZOJ 3597

Test instructions is said that there are n guns, there are m targets, each gun only one shot (that is, a gun can only hit a target), tell you the first gun to hit the first J target, and now the probability of the emergence of a continuous p gun, after knowing this p gun, you are allowed to choose a continuous q a target, The number of targets that this p has hit is the highest, and the expected number of targets to be hit.

This problem can be transformed into another model by simply transforming it:

If a gun can hit the b target, it is treated as a point on the two-bit plane (b, a), and the question is converted to the maximum number of points that a Q * p rectangle can cover. Just one thing to keep in mind is that the same gun can only hit a target, so a maximum of one B can be covered in the case of a equal.

As for how to find the number of rectangular overlay, this is the first time I write, so consulted the relevant information.

The method is to use the right boundary of the rectangle as the reference point, and find out which interval (line segment) of the reference point the rectangle can cover to this point, so that each point corresponds to a segment of equal y, and the original question is converted to the maximum of the number of overlays on x in the interval of height y less than p. You can do this by using the offline operation (scan line) of the line segment tree.

1#include <map>2#include <Set>3#include <stack>4#include <queue>5#include <cmath>6#include <ctime>7#include <vector>8#include <cstdio>9#include <cctype>Ten#include <cstring> One#include <cstdlib> A#include <iostream> -#include <algorithm> - using namespacestd; the #defineINF 0x3f3f3f3f - #defineInf (-((LL) 1<<40)) - #defineLson k<<1, L, (L + R) >>1 - #defineRson k<<1|1, ((L + R) >>1) + 1, R + #defineMem0 (a) memset (A,0,sizeof (a)) - #defineMem1 (a) memset (A,-1,sizeof (a)) + #defineMem (A, B) memset (A, B, sizeof (a)) A #defineFIN freopen ("In.txt", "R", stdin) at #defineFOUT freopen ("OUT.txt", "w", stdout) - #defineRep (I, A, b) for (int i = A; I <= B; i + +) -  -template<classT> T Cmp_min (t A, T b) {returnA <b;} -template<classT> T Cmp_max (t A, T b) {returnA >b;} -template<classT> T MAX (t A, T b) {returna > B?a:b;} intemplate<classT> T MIN (t A, T b) {returnA < b?a:b;} -template<classT> T GCD (t A, T b) {returnB? GCD (b, a%b): A; } totemplate<classT> T LCM (t A, T b) {returnA/GCD (A, b) *b; } +  - //typedef __int64 LL; thetypedefLong LongLL; * Const intMAXN =51000; $ Const intMAXM =110000;Panax Notoginseng Const DoubleEPS = 1e-4; - //LL MOD = 987654321; the  + #defineOK (i) (i > 0 && p[i-1].y = = p[i].y && p[i].x <= p[i-1].x + Q-1) A  the intT, N, M, P, Q, K; + structPoint { -     intx, y; $     BOOL operator< (ConstPoint &a)Const { $         returny = = A.y? X < A.x:y <a.y; -     } - }P[MAXM]; the  - structSegtree {WuyiLL ma[maxn<<2], add[maxn<<2]; the  -     voidBuildintKintLintR) { WuMA[K] = add[k] =0; -         if(L = = R)return ; About build (Lson); build (Rson); $     } -  -     voidPushdown (intk) { -ma[k<<1] + = Add[k]; add[k<<1] +=Add[k]; Ama[k<<1|1] + = Add[k]; add[k<<1|1] +=Add[k]; +ADD[K] =0; the     } -  $     voidUpdateintKintLintRintLintRintval) { the         if(R < L | | L > R)return ; the         if(l <= L && R <= R) {Ma[k] + = val; Add[k] + = val;return ; } the Pushdown (k); the Update (Lson, L, R, Val); - Update (Rson, L, R, Val); inMa[k] = max (ma[k<<1], ma[k<<1|1]); the     } the  AboutLL Query (intKintLintRintLintr) { the         if(R < L | | L > R)return 0; the         if(l <= L && R <= R)returnMa[k]; the Pushdown (k); +         returnMax (Query (Lson, L, R), query (Rson, L, R)); -     } the Bayi }segtree; the  the intMain () - { -     //FIN; the      while(~SCANF ("%d", &t)) while(t--) the     { thescanf"%d %d%d%d%d", &n, &m, &p, &q, &K); theRep (I,0K1) scanf ("%d%d", &p[i].y, &p[i].x); -Sort (p, p +K); the  theSegtree.build (1,1, M); theLL ans =0, FR =0, re =0;94 Rep (i, P, N) { the              while(Fr < K && P[fr].y <=i) { the                 intst = OK (FR)? p[fr-1].x +q:p[fr].x; the                 inted = min (p[fr].x + Q-1, M);98Segtree.update (1,1, M, St, Ed,1); AboutFr + +; -             }101              while(I-p[re].y >=P) {102                 intst = OK (re)? p[re-1].x +q:p[re].x;103                 inted = min (p[re].x + Q-1, M);104Segtree.update (1,1, M, St, Ed,-1); theRe + +;106             }107Ans + = Segtree.query (1,1M1, M);108         }109printf"%.2lf\n", (Double) ans/(n-p +1)); the     }111     return 0; the}

ZOJ 3597 hit the target! (Segment tree Scan line-the maximum number of points the rectangle can cover)

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.