Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at 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 starts with three numbers – N, M, K. Each of the next K lines contains two 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 is the number of the city of the West coast.
Output
For each test case write one line on the standard output:
Test case (case number): (number of crossings)
Sample Input
1 3 4 4 1 4 2 3 3 2 3 1
Sample Output
Test case 1: 5
日本计划迎接ACM ICPC世界总决赛的到来,为此必须修建大量道路。日本是高岛,东海岸有N个城市,西海岸有M个城市(M <= 1000, N <= 1000)。K高速公路将被建造。每个海岸上的城市编号为1、2、……从北到南。每条高速公路都是直线,将东海岸的城市和西海岸的城市连接起来。建设资金由ACM保证。总数的很大一部分是由高速公路之间的交叉口数量决定的。两条高速公路最多在一个地点交叉。编写一个程序来计算高速公路之间的交叉口数量。
输入
输入文件以T开头——测试用例的数量。每个测试用例都以三个数字开始——N、M、K。后面的K行每一个都包含两个数字——由高速公路连接的城市的数字。第一个是东海岸的城市编号,第二个是西海岸的城市编号。
输出
对于每个测试用例,在标准输出上写一行:
测试用例(用例号):(交叉数)
核心思想:将高速公路按东海岸城市编号升序排列,如果编号相同,则把另一端城市升序排列,交叉数即排序后西海岸城市序列的逆序对的数目
例如
1 4
2 3
3 2
3 1
排序后即
1 4
2 3
3 1
3 2
即求4 3 1 2序列的逆序对的数目
利用线段树求取逆序对参考https://blog.csdn.net/qq_40293424/article/details/103018864
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.StringTokenizer;
class L_{
int east;
int west;
}
class cmp1 implements Comparator<L_>{
public int compare(L_ a,L_ b){
if(a.east>b.east){
return 1;
}else if(a.east==b.east){
if(a.west>b.west) return 1;
else if(a.west==b.west) return 0;
else return -1;
}else{
return -1;
}
}
}
class InputReader3
{
BufferedReader buf;
StringTokenizer tok;
InputReader3()
{
buf = new BufferedReader(new InputStreamReader(System.in));
}
boolean hasNext()
{
while(tok == null || !tok.hasMoreElements())
{
try
{
tok = new StringTokenizer(buf.readLine());
}
catch(Exception e)
{
return false;
}
}
return true;
}
String next()
{
if(hasNext()) return tok.nextToken();
return null;
}
int nextInt()
{
return Integer.parseInt(next());
}
}
public class Main {
static int maxn=1005;
static int T,N,M,K;
static L_[] arr=new L_[maxn*maxn];
static long[] tree=new long[maxn<<2];
static void update_tree(int node,int start,int end,int LR){
if(start==end){
tree[node]+=1;
return;
}
int mid=(start+end)>>1;
if(LR<=mid){
update_tree(node<<1,start,mid,LR);
}else{
update_tree(node<<1|1,mid+1,end,LR);
}
tree[node]=tree[node<<1]+tree[node<<1|1];
}
static long query_tree(int node,int start,int end,int L,int R){
if(L<=start&&end<=R){
return tree[node];
}
int mid=(start+end)>>1;
if(R<=mid){
return query_tree(node<<1,start,mid,L,R);
}else if(L>mid){
return query_tree(node<<1|1,mid+1,end,L,R);
}else{
return query_tree(node<<1,start,mid,L,R)
+query_tree(node<<1|1,mid+1,end,L,R);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
InputReader3 sc=new InputReader3();
T=sc.nextInt();
int kk=0;
while(T>0){
Arrays.fill(tree,0);
N=sc.nextInt();
M=sc.nextInt();
K=sc.nextInt();
for(int i=1;i<=K;i++){
arr[i]=new L_();
arr[i].east=sc.nextInt();
arr[i].west=sc.nextInt();
}
Arrays.sort(arr,1,K+1,new cmp1());
long sum=0;
for(int i=1;i<=K;i++){
int a=arr[i].west;
update_tree(1,1,M,a);
sum+=(i-query_tree(1,1,M,1,a));
}
/*
4 3 1 2
1-1=0;
2-1=1;
3-1=2;
4-2=2;
逆序对的数目为5,即交叉数
*/
System.out.println("Test case "+(++kk)+": "+sum);
T--;
}
}
}