Question: There are cities on both sides, arranged from top to bottom. Some cities have roads, and there will be intersections between roads.
Idea: First, you can convert a connected city into a point on the plane. For example, if there is a path between 1 and 2, there is a point. The coordinate is (1, 2 ). After conversion, you can use a tree array to find that the final result is not related to the given order. Therefore, we can sort the result from the Y axis to the large one. If the Y axis is equal, then, the rows are arranged from large to small on the X axis.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string. h>
Using namespace std;
# Define CLR (arr, val) memset (arr, val, sizeof (arr ))
Typedef long LL;
Const int n= 1010;
LL num [N];
Int flag [N] [N];
Struct road {
Int x, y;
} Rr [N * N];
Bool cmp (road a, road B ){
If (a. y = B. y)
Return a. x> B. x;
Return a. y> B. y;
}
Int lowbit (int x ){
Return x & (-x );
}
Void update (int x ){
While (x <N ){
Num [x] + = 1;
X + = lowbit (x );
}
}
LL add (int x ){
LL s = 0;
While (x> 0 ){
S + = num [x];
X-= lowbit (x );
}
Return s;
}
Int main (){
// Freopen ("1.txt"," r ", stdin );
Int numcase;
Scanf ("% d", & numcase );
Int ca = 1;
While (numcase --){
Int n, m, k;
CLR (num, 0 );
CLR (flag, 0 );
Scanf ("% d", & n, & m, & k );
Int x, y;
LL sum = 0;
For (int I = 0; I <k; ++ I ){
Scanf ("% d", & rr [I]. x, & rr [I]. y );
}
Sort (rr, rr + k, cmp );
For (int I = 0; I <k; ++ I ){
If (flag [rr [I]. x] [rr [I]. y])
Continue;
Sum + = add (rr [I]. x-1 );
Update (rr [I]. x );
Flag [rr [I]. x] [rr [I]. y] = 1;
}
Printf ("Test case % d: % lld \ n", ca ++, sum );
}
Return 0;
}