The intersection of the line the garlic Increase group Simulation (iii) DAY2 reverse order to merge sort

Source: Internet
Author: User

Lun Lun just studied analytic geometry in high school and learned to calculate the intersection of two straight lines. This day, the teacher assigned her a homework. There are n straight lines on the plane, and there are several intersections between them. Given a pair of plates (two parallel lines), ask how many pairs of straight lines, their intersections between this pair of plates (note (i, J) and (J, I) only one pair).

(Do you remember any ghost on both sides of the river on the reverse side of the question?) )

We set up a pair of tablet codes for X, Y, respectively.

For each line, we can solve the horizontal axis of its intersection on X, Y, and save it with a struct T.

We sort t by the horizontal axis above X

And then to T in the y above the intersection of the horizontal line to do the merge sort, reverse order number is the focus number, and a focus on a pair of legitimate lines, so the answer is the number of reverse.

Why are the numbers in reverse order the number of focal points?

Because the coordinates of x above are ordered, from small to large. For the coordinates y above, if there is an inverse pair, they will be able to generate the focus (you can verify it yourself).

Enclosed AC Code

#include <cstdio>#include<algorithm>using namespacestd;template<classT> InlinevoidRead (T &_a) {    BOOLf=0;int_ch=getchar (); _a=0;  while(_ch<'0'|| _ch>'9'){if(_ch=='-') f=1; _ch=GetChar ();}  while(_ch>='0'&& _ch<='9') {_a= (_a<<1) + (_a<<3) +_ch-'0'; _ch=GetChar ();} if(f) _a=-_a;}Const intmaxn=100001;intN;Long Longans;structnode{Doublex1,x2; InlineBOOL operator< (ConstNode X)Const{returnx1<x.x1;}} RES[MAXN];DoubleK1,A1,B1,K,B,T[MAXN];voidMergeintLintR) {    if(L==R)return; intMid= (l+r) >>1;    Merge (L,mid); Merge (Mid+1, R); intI=l,j=mid+1, q=l;  while(i<=mid&&j<=r) {if(res[i].x2>res[j].x2) {ans+=mid-i+1; T[q++]=res[j++].x2; } Elset[q++]=res[i++].x2; }     while(I<=mid) t[q++]=res[i++].x2;  while(J&LT;=R) t[q++]=res[j++].x2;  for(RegisterintI=l;i<=r;++i) res[i].x2=t[i];}intMain () {Freopen ("point.in","R", stdin); Freopen ("Point.out","W", stdout); scanf ("%LF%LF%LF",&k1,&a1,&B1);    Read (n);  for(RegisterintI=1; i<=n;++i) {scanf ("%LF%LF",&k,&b); Res[i].x1= (B-B1)/(k1-k); Res[i].x2= (B-A1)/(k1-k); } Sort (Res+1, res+n+1); Merge (1, N); printf ("%lld", ans); return 0;}

The intersection of the line the garlic Increase group Simulation (iii) DAY2 reverse order to merge sort

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.