Lighthouse (Lighthouse)
Description
As shown in the following figure, If another lighthouse are in gray area, they can beacons each other.
For example, in following figure, (b, R) is a pair of lighthouse which can beacons each other, while (b, G), (R, g) Yes not .
Input
1st Line:n
2nd ~ (N + 1) th Line:each line is X Y, means a lighthouse are on the point (X, Y).
Output
How many pairs of lighthourses can beacons each other
(For every lighthouses, X coordinates won ' t is the same, Y coordinates won ' t be the same)
Example
Input
32 24 35 1
Output
1
Restrictions
For 90% test cases:1 <= N <= 3 * 105
For 95% test cases:1 <= N <= 106
For all test cases:1 <= n <= 4 * 106
For every lighthouses, X coordinates won ' t is the same, Y coordinates won ' t be the same.
1 <= x, y <= 10^8
Time:2 sec
memory:256 MB
Hints
The range of int is usually [ -231, 231-1], and it may be too small.
In the video there are explanations I altogether didn't see, drunk.
is to ask for the reverse order on the line.
1#include <cstdlib>2#include <iostream>3#include <cstdio>4#include <cstring>5 using namespacestd;6 #defineLL Long Long7 Const intMax_size =4*1e6;8 LL y_val[max_size];9 LL tmp_arry[max_size];Ten ///speed Code, why Baidu One Const intSZ =1<< -; A structfastio{ - CharInbuf[sz]; - CharOutbuf[sz]; the FastIO () { - setvbuf (STDIN,INBUF,_IOFBF,SZ); - setvbuf (STDOUT,OUTBUF,_IOFBF,SZ); - } + }io; - struct Point + { A LL x, y; at } P[max_size]; - - intcmpConst void*a,Const void*b) - { - structPoint *c = (Point *) A; - structPoint *d = (Point *) b; in if(C->x! = d->x) - returnC->x-d->x; to Else + returnD->y-c->y; - } the *ll Merge (ll *arr, LL Beg, LL Mid, LL end, LL *Tmp_arr) $ {Panax Notoginsengmemcpy (Tmp_arr+beg, Arr+beg,sizeof(LL) * (End-beg +1)); -LL i =Beg; theLL j = mid+1; +LL k =Beg; ALL inversion =0; the while(I <= mid && J <=end) + { - if(Tmp_arr[i] <=Tmp_arr[j]) $arr[k++] = tmp_arr[i++]; $ Else - { -arr[k++] = tmp_arr[j++]; theInversion + = Mid-i +1; - }Wuyi } the - while(I <=mid) Wuarr[k++] = tmp_arr[i++]; - while(J <=end) Aboutarr[k++] = tmp_arr[j++]; $ returninversion; - } - -ll Mergeinversion (ll *arr, LL Beg, LL end, LL *Tmp_arr) A { +LL inversions =0; the if(Beg <end) - { $LL mid = (beg + end) >>1; theInversions + =mergeinversion (arr, Beg, Mid, Tmp_arr); theInversions + = Mergeinversion (arr, mid+1, end, Tmp_arr); theInversions + =Merge (arr, Beg, Mid, end, Tmp_arr); the } - returninversions; in } the the intMain () About { the LL N; theCIN >>N; the for(inti =0; I < n; i++) + { -CIN >> p[i].x >>p[i].y; the }Bayi theQsort (P, N,sizeof(p[0]), CMP); the for(inti =0; I < n; i++) - { -Y_val[i] =p[i].y; the } the thememcpy (Tmp_arry, Y_val,sizeof(LL) *n); thecout << N (n1) /2-Mergeinversion (Y_val,0, N-1, Tmp_arry) <<Endl; - return 0; the}
View Code
Then optimize the code, and my paragraph will not be the last point. It is not necessary to do more sorting operations.
Tsinghua Academy Lighthouse