POJ-2528-Mayor's posters

Source: Internet
Author: User

POJ-2528-Mayor's posters

Http://poj.org/problem? Id = 2528

The discretization of the Line Segment tree is equivalent to first ing and then building

Test data for the question

1 4

2 6

8 10

3 4

7 10

Extracts and sorts the endpoints, and removes the same coordinates, that is, 1, 2, 3, 4, 6, 7, 8, 10.

1, 2, 3, 4, 6, 7, 8, 10 can be discretization

1, 2, 3, 4, 5, 6, 7, 8

The range given by the question can be expressed

1 4

2 5

7 8

3 4

6 8

This will reduce the building space.

# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> using namespace std; # define Max 10010 struct cam1 {int l, r;} post [Max]; int x [Max * 2]; // coordinate int h [10000005]; struct cam2 {int l, r; int flag; // mark whether it is overwritten} list [Max * 8]; int cmp (const void * a, const void * B) {return * (int *) a-* (int *) B;} void build (int k, int l, int r) {list [k]. l = l; list [k]. r = r; list [k]. flag = 0; if (l = r) return; int mid = (l + r)/2; build (k <1, l, mid ); build (k <1 | 1, mid + 1, r);} int query (int k, int l, int r) {if (list [k]. flag) return 0; if (list [k]. l = l & list [k]. r = r) {list [k]. flag = 1; return 1;} int result, mid; mid = (list [k]. l + list [k]. r)/2; if (r <= mid) result = query (k <1, l, r); else if (l> mid) result = query (k <1 | 1, l, r); elseresult = query (k <1, l, mid) | query (k <1 | 1, mid + 1, r); if (list [k <1]. flag & list [k <1 | 1]. flag) // report the child node to the parent node list [k]. flag = 1; return result;} int main () {int t, cnt; int I, j, k, n, ans; scanf ("% d", & t ); while (t --) {scanf ("% d", & n); cnt = 0; for (I = 0; I <n; I ++) {scanf ("% d", & post [I]. l, & post [I]. r); x [cnt ++] = post [I]. l; x [cnt ++] = post [I]. r;} sort (x, x + cnt); // sort cnt = unique (x, x + cnt)-x from small to large; // remove the same coordinates for (I = 0; I <cnt; I ++) h [x [I] = I; build (, cnt-1 ); ans = 0; for (I = n-1; I> = 0; I --) if (query (1, h [post [I]. l], h [post [I]. r]) ans ++; printf ("% d \ n", ans) ;}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.