Japan
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23111 | Accepted: 6232 |
Description
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)
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
Source
树状数组第三题。
这道题可以先排序,x升序,x相同的时候,y升序
然后求y中的逆序对数。
大概知道了怎么用树状数组求逆序对。
下标(从0开始)-比a[i]小的=a[i]与之前形成的逆序对数。
具体见代码注释
1 /************************************************************************* 2 > File Name: code/poj/3067.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年08月04日 星期二 10时49分06秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #define y0 abc111qqz 21 #define y1 hust111qqz 22 #define yn hez111qqz 23 #define j1 cute111qqz 24 #define tm crazy111qqz 25 #define lr dying111qqz 26 using namespace std; 27 #define REP(i, n) for (int i=0;i<int(n);++i) 28 typedef long long LL; 29 typedef unsigned long long ULL; 30 const int inf = 0x7fffffff; 31 const int N=1E3+5; 32 33 int n,m,k; 34 int t[N*N/2]; 35 struct R 36 { 37 int x,y; 38 }r[N*N/2]; 39 40 bool cmp(R a,R b) 41 { 42 if (a.x<b.x) return true; 43 if (a.x==b.x&&a.y<b.y) return true; 44 return false; 45 } 46 int lowbit(int x) 47 { 48 return x&(-x); 49 } 50 void update (int x,int c) 51 { 52 for ( int i = x ; i < N ; i = i + lowbit(i)) 53 { 54 t[i] = t[i] + c; 55 } 56 } 57 LL sum ( int x) 58 { 59 int res = 0 ; 60 for ( int i = x ; i >= 1 ; i = i - lowbit(i)) 61 { 62 res = res + t[i]; 63 } 64 return res; 65 } 66 int main() 67 { 68 int T; 69 cin>>T; 70 LL ans ; 71 int cas = 0; 72 while (T--) 73 { 74 memset(t,0,sizeof(t)); 75 memset(r,0,sizeof(r)); 76 ans = 0 ; 77 cas++; 78 scanf("%d %d %d",&n,&m,&k); 79 for ( int i = 0 ; i < k ; i ++ ) 80 { 81 scanf("%d %d",&r[i].x,&r[i].y); 82 } 83 sort(r,r+k,cmp); 84 for ( int i = 0 ; i < k; i ++ ) //起点终点相同不算crossing 85 { 86 // update(r[i].x,1); 87 // if (r[i].x==r[i-1].x||r[i].y==r[i-1].y) continue; 88 ans = ans +i- sum(r[i].y); //求y的逆序对数,好好理解这行代码! 89 // cout<<"ans:"<<ans<<endl; 90 update(r[i].y,1); 91 } 92 printf("Test case %d: %lld ",cas,ans); 93 94 } 95 return 0; 96 }