title Link:http://www.icpc.moe/onlinejudge/showProblem.do?problemId=3128
Test Instructions: there are n lines on the plane, given L, R, to find out the number of the intersection of these lines in the range (L, R).
Ideas:
First find the intersection of each line with a straight line x = L and a line x = R, for example.
Because the topic requirement interval (l, r) is open interval, so in order to avoid the intersection of the horizontal axis is just the case of L or R, I can first add a very small value, R minus a very small value, the gray line in.
Then find the intersection of each line and two gray lines, first by the Y value of the intersection with L, and then marked with an ID.
Then press the Y value of the intersection with R to sort from small to large.
If the two lines line1 and line2, if line1.ly < line2.ly, then if Line1.ry > Line2.ry, then this intersection must fall within the range.
So at this point the sorted ID sequence is 3 1 2, it is clear that the sequence of the inverse logarithm. Then we can use the line tree to find the inverse logarithm.
1#include <bits/stdc++.h>2 using namespacestd;3 intN;4 #defineMAXN 100105 #defineEPS 1e-86 #defineLson L, M, rt<<17 #defineRson m+1, R, rt<<1|18 struct Line9 {Ten DoubleK, B; OneLine (DoubleKkDoublebb) {k = KK; b =BB;} A Line () {} - }L[MAXN]; - DoubleL, R; the structNode - { - DoubleL, R; - intID; +Node (DoublellDoubleRrintII) {L = ll; r = RR; id =II;} - Node () {} + }NODE[MAXN]; A BOOLCMP1 (Node A, Node B) {returnA.L <B.L;} at BOOLCMP2 (Node A, Node B) {returnA.R <B.R;} - intsum[maxn<<2]; - voidPushup (intRT) - { -SUM[RT] = sum[rt<<1] + sum[rt<<1|1]; - } in voidBuildintLintRintRT) - { to if(L = =R) + { -SUM[RT] =0;return; the } * intm = (l+r) >>1; $ build (Lson);Panax Notoginseng build (Rson); - pushup (RT); the } + voidUpdateintPosintAddintLintRintRT) A { the if(L = =R) + { -SUM[RT] + = add;return; $ } $ intm = (l+r) >>1; - if(Pos <=m) Update (POS, add, Lson); - ElseUpdate (POS, add, Rson); the pushup (RT); - }Wuyi intQueryintLintRintLintRintRT) the { - if(l <= l && R <=R) Wu { - returnSum[rt]; About } $ intm = (l+r) >>1; - intRET =0; - if(L <= m) ret + =query (L, R, Lson); - if(R > m) ret + =query (L, R, Rson); A returnret; + } the intMain () - { $ //freopen ("In.txt", "R", stdin); the while(~SCANF ("%d", &N)) the { the for(inti =1; I <= N; i++) the { - Doublex1, y1, x2, y2; inscanf"%LF%LF%LF%LF", &x1, &y1, &X2, &y2); the DoubleK = (y2-y1)/(x2-x1); the Doubleb = y1-k*X1; AboutL[I].K = k; L[I].B =b; the } thescanf"%LF%LF", &l, &R); theL + = 1e-8; R-= 1e-8; + for(inti =1; I <= N; i++) - { theNODE[I].L = L[i].k*l +l[i].b;BayiNODE[I].R = L[i].k*r +l[i].b; the } theSort (node+1, node+1+N, CMP1); - for(inti =1; I <= N; i++) Node[i].id =i; - theSort (node+1, node+1+N, CMP2); the intAns =0; theBuild1N1); the for(inti =1; I <= N; i++) - { theUpdate (Node[i].id,1,1N1); theAns + = I-query (1, Node[i].id,1N1); the }94printf"%d\n", ans); the the } the return 0;98}
Zoj 3157 Weapon segment tree for inverse logarithm