Poj 2528 mayor's posters [discretization + line segment tree]

Source: Internet
Author: User

Title: poj 2528 mayor's posters


Question: Add a poster with a very long length of AI to the wall. Because some of them will overwrite it, find the number of posters that can be seen at last.


Analysis: The question is exactly the same as that of poj2777, and the method is the same, but this should be discretization, And the array should be larger. At least 2 times.

The C ++ pair class is used for discretization, which is relatively easy to use.


Code:

# Include <iostream> # include <algorithm> # include <utility> # include <cstring> # include <cstdio> using namespace STD; const int n = 25000; pair <int, int> P [2 * n]; pair <int, int> V [N]; int N; int comp (pair <int, int> A, pair <int, int> B) {if (. first! = B. first) return. first <B. first;} int CMP (pair <int, int> A, pair <int, int> B) {if (. second! = B. Second) return a. Second <B. Second; If (A. First! = B. first) return. first <B. first;} int discrete () // discretization {for (INT I = 0; I <n; I ++) {int X, Y; scanf ("% d", & X, & Y); P [I * 2] = make_pair (X, I ); P [I * 2 + 1] = make_pair (Y, I);} Sort (p, p + 2 * n, comp); int CNT = 2, TMP = P [0]. first; P [0]. first = 1; for (INT I = 1; I <n * 2; I ++) {If (P [I]. first = TMP) {TMP = P [I]. first; P [I]. first = (cnt-1);} else {TMP = P [I]. first; P [I]. first = CNT ++ ;}} sort (p, p + 2 * n, CMP); For (INT I = 0; I <2 * n; I + = 2) V [I /2] = make_pair (P [I]. first, P [I + 1]. first); CNT --; return CNT;} struct node {int L, R; long num;}; node tree [4 * n]; int vis [N * 2]; void build (int l, int R, int o) {tree [O]. L = L; tree [O]. R = r; tree [O]. num = 1; if (L = r) return; int mid = (L + r)/2; build (L, mid, O * 2 ); build (Mid + 1, R, O * 2 + 1);} void Update (int l, int R, int T, int o) {If (tree [O]. L = L & tree [O]. R = r) {tree [O]. num = T; return;} If (tree [O]. num = T) return; If (Tree [O]. num! =-1) {tree [2 * o]. num = tree [O]. num; tree [2 * O + 1]. num = tree [O]. num; tree [O]. num =-1;} int mid = (tree [O]. L + tree [O]. r)> 1; if (r <= mid) Update (L, R, T, O + O); else if (L> mid) Update (L, R, t, O + 1); else {Update (L, mid, T, O * 2); Update (Mid + 1, R, T, O * 2 + 1) ;}} void query (int l, int R, int o) {If (tree [O]. num! =-1) {vis [tree [O]. num] = 1; return;} int mid = (tree [O]. L + tree [O]. r)> 1; if (r <= mid) query (L, R, O + O); else if (L> mid) query (L, R, O + 1); else {query (L, mid, O * 2); query (Mid + 1, R, O * 2 + 1 );}} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N ); int CNT = discrete (); Build (1, CNT, 1); For (INT I = 0; I <n; I ++) {Update (V [I]. first, V [I]. second, I, 1);} memset (VIS, 0, sizeof (VIS); query (1, CNT, 1); int ans = 0; for (INT I = 0; I <= CNT; I ++) if (vis [I]) {// printf ("-- % d", I ); ans ++;} printf ("% d \ n", ANS); memset (p, 0, sizeof (p); memset (v, 0, sizeof (v);} 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.