[Tree-like array] [Number of reverse order] Japan

Source: Internet
Author: User

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

Idea: the K-Path according to path[i].x from small to large sort (if path[i].x the same, according to PATH[I].Y from small to large sort), at this time the path[1~k].y sequence of the number of reverse order is the answer; tree-like array/merge sort to find the inverse number;
Tree-like array: https://www.cnblogs.com/hsd-/p/6139376.html
AC Code:
//tree-like array for inverse number O (nlogn+logn)#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#defineLowbit (x) x& (-X)typedefLong Longll;using namespacestd;structpath{intx, y;} path[1000010];BOOLCMP (Path A,path b) {if(a.x!=b.x)returna.x<b.x; Else returna.y<b.y;}intn,m,k;intc[1010];voidAddintXintval) {   for(intI=x;i<=m;i+=lowbit (i)) c[i]+=Val;} ll Getsum (intx) {LL ret=0;  for(intI=x;i>0; I-=lowbit (i)) ret+=C[i]; returnret;}intMain () {intT; scanf ("%d",&t);  for(ints=1; s<=t;s++) {scanf ("%d%d%d",&n,&m,&k);  for(intI=1; i<=k;i++) scanf ("%d%d",&path[i].x,&path[i].y); Sort (Path+1, path+1+k,cmp); Memset (c,0,sizeof(c)); ll ans=0;  for(intI=1; i<=k;i++) {Add (Path[i].y,1), or//the number is marked as 1 ans+ = (-getsum (PATH[I].Y));//The number of the number of the preceding I is greater than path[i].y is equal to the number of the I-I-I number is less than or equal to PATH[I].Y (that is, Getsum (PATH[I].Y))} printf ( c10>"Test Case%d:", s); cout<<ans<<Endl; }    return 0;}

AC Code:

//Merge sort to find the reverse number O (NLOGN+NLOGN)#include <iostream>#include<cstdio>#include<algorithm>typedefLong Longll;using namespacestd;structpath{intx, y;} path[1000010];BOOLCMP (Path A,path b) {if(a.x!=b.x)returna.x<b.x; Else returna.y<b.y;}intn,m,k;intb[1000010];inttmp[1000010];ll ans=0;voidMerge_ (intLintMintR) {  intCnt=0; inti,j;  for(i=l,j=m+1; i<=m&&j<=R;) {if(B[i]<=b[j]) {Tmp[++cnt]=b[i]; i++;} Else{tmp[++CNT]=B[J]; J + +; Ans+ = (m-i+1); }  }   while(i<=m) {Tmp[++cnt]=b[i]; i++;}  while(J&LT;=R) {Tmp[++cnt]=b[j]; J + +;}  for(inti=l,j=1; i<=r&&j<=cnt;i++,j++) b[i]=tmp[j];}voidMerge_sort (intLintR) {  if(L==R)return; intM= (l+r) >>1;  Merge_sort (L,M); Merge_sort (M+1, R); Merge_ (l,m,r);}intMain () {intT; scanf ("%d",&t);  for(ints=1; s<=t;s++) {scanf ("%d%d%d",&n,&m,&k);  for(intI=1; i<=k;i++) scanf ("%d%d",&path[i].x,&path[i].y); Sort (Path+1, path+1+k,cmp);  for(intI=1; i<=k;i++) b[i]=path[i].y; Ans=0; Merge_sort (1, K); printf ("Test Case%d:", s); cout<<ans<<Endl; }    return 0;}

[Tree-like array] [Number of reverse order] Japan

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.