HDU4022Bombing (Shanghai 2 in 11 years)

Source: Internet
Author: User
Question: give some vertices and give two operations: remove the vertices whose abscissa is a certain value or remove the vertices whose ordinate is a certain value. Ask the number of vertices removed each time because they are X points, for the Y operation, separate and sort X and Y. Then, for the operation, perform the binary search to check whether a point of the global variable is recorded... syntaxHighlighter. all ();

Question: give some vertices and give two operations: remove the vertices whose abscissa is a value or remove the vertices whose ordinate is a value, and ask the number of vertices removed each time.

 


Because it is divided into X and Y operations, the X and Y are separated, sorted, and then for the operation, binary search

Whether a point of the global variable record is removed

Sorting + binary + brute force Mark

Ah, I came to the water issue early in the morning.


[Cpp]
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define inf 110000000
# Define M 10005
# Define N 100005
# Define Min (a, B) (a) <(B )? (A) (B ))
# Define Max (a, B) (a)> (B )? (A) (B ))
# Define pb (a) push_back ()
# Define mem (a, B) memset (a, B, sizeof ())
# Define LL long
# Define MOD 1000000007.
Using namespace std;
Struct Node {
Int val, id;
} X [N], y [N];
Int n, q;
Int flag [N];
Bool cmp (Node n1, Node n2 ){
Return n1.val }
Int BinSearch (Node * a, int n, int m ){
Int low = 0, high = n-1, mid, pos =-1;
While (low <= high ){
Mid = (low + high)/2;
If (a [mid]. val = m) {pos = mid; break ;}
If (a [mid]. val Else high = mid-1;
}
If (pos =-1) return 0;
Int I = pos-1, ans = 0;
While (I> = 0 & a [I]. val = m ){
If (flag [a [I]. id] = 0) {flag [a [I]. id] = 1; ans ++ ;}
I --;
}
While (pos If (flag [a [pos]. id] = 0) {flag [a [pos]. id] = 1; ans ++ ;}
Pos ++;
}
Return ans;
}
Int main (){
While (scanf ("% d", & n, & q )! = EOF & n + q ){
For (int I = 0; I Scanf ("% d", & x [I]. val, & y [I]. val );
X [I]. id = y [I]. id = I;
}
Sort (x, x + n, cmp );
Sort (y, y + n, cmp );
Mem (flag, 0 );
While (q --){
Int k, p;
Scanf ("% d", & k, & p );
If (k = 0)
Printf ("% d \ n", BinSearch (x, n, p ));
Else
Printf ("% d \ n", BinSearch (y, n, p ));
}
Puts ("");
}
Return 0;
}

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define inf 110000000
# Define M 10005
# Define N 100005
# Define Min (a, B) (a) <(B )? (A) (B ))
# Define Max (a, B) (a)> (B )? (A) (B ))
# Define pb (a) push_back ()
# Define mem (a, B) memset (a, B, sizeof ())
# Define LL long
# Define MOD 1000000007.
Using namespace std;
Struct Node {
Int val, id;
} X [N], y [N];
Int n, q;
Int flag [N];
Bool cmp (Node n1, Node n2 ){
Return n1.val }
Int BinSearch (Node * a, int n, int m ){
Int low = 0, high = n-1, mid, pos =-1;
While (low <= high ){
Mid = (low + high)/2;
If (a [mid]. val = m) {pos = mid; break ;}
If (a [mid]. val Else high = mid-1;
}
If (pos =-1) return 0;
Int I = pos-1, ans = 0;
While (I> = 0 & a [I]. val = m ){
If (flag [a [I]. id] = 0) {flag [a [I]. id] = 1; ans ++ ;}
I --;
}
While (pos If (flag [a [pos]. id] = 0) {flag [a [pos]. id] = 1; ans ++ ;}
Pos ++;
}
Return ans;
}
Int main (){
While (scanf ("% d", & n, & q )! = EOF & n + q ){
For (int I = 0; I Scanf ("% d", & x [I]. val, & y [I]. val );
X [I]. id = y [I]. id = I;
}
Sort (x, x + n, cmp );
Sort (y, y + n, cmp );
Mem (flag, 0 );
While (q --){
Int k, p;
Scanf ("% d", & k, & p );
If (k = 0)
Printf ("% d \ n", BinSearch (x, n, p ));
Else
Printf ("% d \ n", BinSearch (y, n, p ));
}
Puts ("");
}
Return 0;
}

Related Article

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.