HDU 4268 Alice and Bob (Multiset | Line Segment tree)

Source: Internet
Author: User
Alice and Bob Time Limit: 10000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2901 accepted submission (s): 941


Problem descriptionalice and Bob's game never ends. today, they introduce a new game. in this game, both of them have n Different rectangular cards respectively. alice wants to use his cards to cover Bob's. the card a can cover the card B if the height of A is not smaller than B and the width of A is not smaller than B. as the best programmer, you are asked to compute the maximal number of Bob's cards that Alice can cover.
Please pay attention that each card can be used only once and the cards cannot be rotated.
 
Inputthe first line of the input is a number T (t <= 40) which means the number of test cases.
For each case, the first line is a number n which means the number of cards that Alice and Bob have respectively. each of the following n (n <= 100,000) lines contains two integers H (H <= 1,000,000,000) and W (W <= 1,000,000,000) which means the height and width of Alice's card, then the following n lines means that of Bob's.
 
Outputfor each test case, output an answer using one line which contains just one number.
 
Sample Input
221 23 42 34 532 35 76 84 12 53 4 
 
Sample output
12
 
Source2012 ACM/ICPC Asia Regional Changchun online
Recommendliuyiding | we have carefully selected several similar problems for you: 4272 4277 4273 4270 question: A and B each have n rectangles. (N <= 1e5) now a wants to overwrite the rectangle of B with its own rectangle. The condition that one rectangle overwrites the other is that the width and height do not correspond to the width and height of the other rectangle. Now I want to ask how many rectangles A can overwrite B. Each rectangle can overwrite one or more rectangles. The rectangle cannot be rotated. The idea is greedy. First, sort the rectangles of two people by W. Then, for each rectangle of. Find a rectangle of W <= self W. H <self H and try to be close to B. Remove the two rectangles after overwriting. You can use Multiset to maintain the rectangle that finds the preceding conditions. However, during the competition, I changed my posture and used the line segment tree to write the hair. Similar ideas. Discretization. Maintenance Interval and. Locate the right-most position where the value is less than or equal to P. For details, see the code:
# Include <iostream> # include <stdio. h >#include <algorithm> using namespace STD; # define lson L, mid, ls # define rson Mid + 1, R, rsconst int maxn = 100010; int num [maxn <3]; // There are 2 * n after discretization and you forgot to change the space. Pit struct node {int W, H;} Ag [maxn], BG [maxn]; int H [maxn <1]; inline bool CMP (const node &, const node & B) {if (. W = B. w) return. h <B. h; return. W <B. w;} void build (int l, int R, int RT) {num [RT] = 0; If (L = r) return; int ls = RT <1, rs = ls | 1, mid = (L + r)> 1; build (lson); Build (rson);} void Update (int l, int R, int RT, int P, int d) {If (L = r) {num [RT] + = D; return;} int ls = RT <1, RS = ls | 1, mid = (L + r)> 1; if (P <= mid) Update (Lson, P, d); else Update (rson, P, d); num [RT] = num [ls] + num [RS];} int Qu (INT l, int R, int RT, int p) {If (! Num [RT]) Return-1; if (L = r) return l; int ls = RT <1, RS = ls | 1, mid = (L + r)> 1, Pos =-1; if (P <= mid) return Qu (lson, P); If (Num [RS]) pos = Qu (rson, P); If (Pos! =-1) return Pos; return Qu (lson, P);} int main () {int t, n, I, m, p, ans; scanf ("% d", & T); While (t --) {m = 0; scanf ("% d", & N); for (I = 0; I <n; I ++) {scanf ("% d", & Ag [I]. h, & Ag [I]. w); H [M ++] = Ag [I]. h ;}for (I = 0; I <n; I ++) {scanf ("% d", & BG [I]. h, & BG [I]. w); H [M ++] = BG [I]. h;} Sort (AG, Ag + N, CMP); sort (BG, BG + N, CMP); sort (H, H + M); M = unique (H, H + M)-H; build (1, m, 1); ans = p = 0; for (I = 0; I <n; I ++) {While (P <n & BG [p]. W <= Ag [I]. w) UPD Ate (1, m, 1, lower_bound (H, H + M, BG [p]. h)-H + 1, 1), P ++; int Pos = Qu (1, m, 1, lower_bound (H, H + M, Ag [I]. h)-H + 1); If (Pos! =-1) {ans ++; update (1, m, 1, POs,-1) ;}} printf ("% d \ n", ANS );} return 0 ;}


HDU 4268 Alice and Bob (Multiset | Line Segment tree)

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.