(POJ 3067) Japan (slowly familiar tree-like array)

Source: Internet
Author: User

Japan
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 29295 Accepted: 7902

Description

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must is built for the venue. Japan is Tall island with N cities on the East coast and M cities on the West coast (M <=, N <= 1000). K superhighways would be build. Cities on each coast is numbered 1, 2, ... Each superhighway are straight line and connects city in the East coast with city of the West coast. The funding for the construction are guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At the most of the superhighways cross at the one location. Write A program that calculates the number of the crossings between superhighways.

Input

The input file starts with t-the number of test cases. Each test case is starts with three numbers–n, M, K. Each of the next K lines contains, numbers–the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one are the number of the city of the West coast.

Output

For each test case, write one line in the standard output:
Test Case Number: (Number of crossings)

Sample Input

13 4 41 42 33 23 1

Sample Output

Test Case 1:5


Seems to be a dichotomy of the problem, in fact, after some simple (fog) transformation, it can be converted to a tree-like array to solve
Since yesterday, after being taught to be a man, my understanding of the tree array has deepened: 0

General Test instructions: Japan has two east and West Islands, from north to south sequentially labeled as ... then give the K-bar, find out how many intersections of K-bar (note is 22 intersection, as long as two lines have a point of intersection even if one).

After analysis, it can be found that if two edges have intersections, they must meet x1-x2>0&&y1-y2<0 or x1-x2<0&&y1-y2>0;
There are two conditions, so we can change the x sort to a condition: all edges are sorted from small to large by x, and then traversed while maintaining the tree array. For the side K, in the side 1 to the edge of the k-1 to find the edge of the Y>yk, there are several such edges, then for the side K, in the 1~k-1 there are several side of the intersection.
While maintaining the tree array, the value of the intersection is updated, and the number of all intersections can be calculated when the traversal is complete.

PS: When sorting, if the x1==x2, then to the order of y1<y2, otherwise in the calculation and the small Y value of the edge of the intersection of the number of edges will be equal to the X is also counted (because the tree-like array only stores y values).

#include <iostream>#include<cstdio>#include<vector>#include<Set>#include<map>#include<string.h>#include<cmath>#include<algorithm>#include<queue>#defineLL Long Long#defineINF 0x3f3f3f3fusing namespacestd;intn,m,k;structde{intx, y; BOOL operator< (ConstDe &other)Const    {        if(x==other.x)returny<Other.y; returnx<other.x; }};d e qiao[ -*1010]; LL run[10100]={0};intLowbit (inti) {returni& (-i);}voidUpdateinti) {     while(i<=m) {Run[i]++; I+=lowbit (i); }}ll sum (inti) {LL s=0;  while(i>0) {s+=Run[i]; I-=lowbit (i); }    returns;}intMain () {intT; scanf ("%d",&t);  for(intp=1;p <=t;p++) {scanf ("%d%d%d",&n,&m,&k); memset (Qiao,0,sizeof(Qiao)); memset (Run,0,sizeof(run));  for(intI=1; i<=k;i++) scanf ("%d%d",&qiao[i].x,&qiao[i].y); Sort (Qiao+1, qiao+1+k); LL ans=0;  for(intI=1; i<=k;i++) {ans+=sum (M)-sum (QIAO[I].Y);        Update (QIAO[I].Y); //cout << "ans=" << ans << endl;} printf ("Test Case%d:%i64d\n", P,ans); }    return 0;}

(POJ 3067) Japan (slowly familiar tree-like array)

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.