POJ 3067 Japan tree Array

Source: Internet
Author: User

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;
}
 

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.