Mayor's posters -- poj_2528 -- segment update and discretization of line tree

Source: Internet
Author: User

address: http://poj.org/problem? Id = 2528

/* Author: bob lee2012.9.28 ======================================== =================================== question: posters are pasted on the walls, which can be covered by each other, at the end of the question, you can see several posters ============================================= ==================================== train of thought: line Segment tree, but the data for this question is very big. We need to use discretization through the silly blog. We have learned something that is, normal discretization is flawed, especially for how to solve these interval problems, the question process is clearly described in the question, first, read the data and then perform discretization. Then, update each interval. Each update has its own color tag, which is the number of I. Then, it uses hash to indicate whether the data has already appeared, as I mentioned in the question, it is possible that a large interval will be covered by a cell in the middle. Only the two ends of the two ends can be counted once. So hash is used and the total number of new colors is calculated every time. + Finally, we can find out the advanced questions about the total number range line segment tree, still classic */# include <iostream> # include <cstdio> # include <algorithm> # include <cstring> using namespace STD; # define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 # define maxn 11111 bool hash [maxn]; int Li [maxn], RI [maxn]; // The following array sizes are also exquisite/* because the array X is used to save each interval segment, there will be 2 data at a time, so at least 2 times or more should be enabled, and COL is generally 4 times open is enough, here, why do we need to increase the speed by 16 times? Because each time you read two numbers, you need to open more. This is a little different from single-point update */int x [maxn * 3]. int Col [maxn <4]; int CNT; void Pushdown (int rt) {If (Col [RT]! =-1) {Col [RT <1] = Col [RT <1 | 1] = Col [RT]; Col [RT] =-1 ;}} void Update (int l, int R, int C, int L, int R, int RT) {If (L <= L & R <= r) {Col [RT] = C; return;} Pushdown (RT); int M = (L + r)> 1; if (L <= m) Update (L, r, C, lson); If (r> m) Update (L, R, C, rson);} void query (int l, int R, int RT) {If (COL [RT]! =-1) {If (! Hash [col [RT]) CNT ++; hash [col [RT] = true; return;} If (L = r) return; int M = (L + r)> 1; query (lson); query (rson);} int Bin (INT key, int N, int X []) {int L = 0, r = n-1; while (L <= r) {int M = (L + r)> 1; if (X [m] = key) return m; If (X [m] <key) L = m + 1; else r = m-1;} return-1;} int main () {int t, n; scanf ("% d", & T); While (t --) {scanf ("% d", & N); int nn = 0; // discretization for (INT I = 0; I <n; I ++) {scanf ("% d ",& Li [I], & Ri [I]); X [NN ++] = Li [I]; X [NN ++] = Ri [I];} sort (x, x + nn); int M = 1; // remove duplicate for (INT I = 1; I <nn; I ++) {If (X [I]! = X [I-1]) x [M ++] = x [I];} // This is to avoid the defect of normal discretization/* the following two simple examples should reflect the defect of normal discretization: Example 1: 1-10 1-4 5-10 Example 2: 1-10 1-4 6-10 after normal discretization, they all become [] [1, 2] [3, 4] Line Segments 2 overwrite [1, 2], and line segment 3 overwrite [3, 4]. is line 1 completely overwritten? The first example is completely overwritten, and the second example is not covered. To solve this defect, we can add some processing to the sorted array, for example, [1, 2, 6, 10] if the distance between adjacent numbers is greater than 1, add any number to it, such as adding [1, 2, 3, 6, 7, 10], and then create a line segment tree. */For (INT I = s-1; I> 0; I --) {If (X [I]! = X [I-1] + 1) x [M ++] = x [I-1] + 1;} Sort (x, x + M); memset (COL,-1, sizeof (COL); For (INT I = 0; I <n; I ++) {int L = Bin (Li [I], M, X ); int r = Bin (RI [I], M, x); Update (L, R, I, 0, M, 1);} CNT = 0; memset (hash, false, sizeof (hash); query (0, M, 1); printf ("% d \ n", CNT);} 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.