POJ 2528 Mayor ' s Posters "segment tree + discretization"

Source: Internet
Author: User
Tags integer numbers

Mayor ' s Posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 50643 Accepted: 14675


The citizens of Bytetown, AB, could not stand then the candidates in the mayoral election campaign has been placing their Electoral posters at all places at their whim. The city council have finally decided to build a electoral wall for placing the posters and introduce the following rules:
    • Every candidate can place exactly one poster on the wall.
    • All posters is of the same height equal to the height of the wall; The width of a poster can be any integer number of bytes (byte was the unit of length in Bytetown).
    • The wall is divided to segments and the width of each segment is one byte.
    • Each poster must completely cover a contiguous number of wall segments.

They has built a wall 10000000 bytes long (such that there are enough place for all candidates). When the electoral campaign is restarted, the candidates were placing their posters on the wall and their posters differe D widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown is curious whose posters would be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters was placed given the information about posters ' Si Ze, their place and order of placement on the electoral wall.


The first line of input contains a number C giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains the integer numbers Li and ri which is the number of the wall segment occupied b Y the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= N, 1 <= li <= ri <= 10000000. After the i-th poster are placed, it entirely covers all wall segments numbered Li, li+1,..., RI.


For each input data set print the number of visible posters after all the posters is placed.

The picture below illustrates the case of the sample input.

Sample Input

151 42 68 103 47 10

Sample Output


Test instructions: A city to run for mayor, the candidate can put posters on a wall for their own canvassing, each can be affixed to a continuous area, and later posted can cover the front, asked how many posters can be seen at the end.

First discretization: (scrolling array optimization) Orz online Daniel

#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 10000+100using namespace std; struct node{int x, y;};  Node num[10100];int color[maxn<<4];int rec[maxn<<4];//discretization stores int Find (int val, int *a, int L, int R)//subscript in a array [L,    R] Range looks for Val value subscript {int left = L, right = R;        while (left <= right) {int mid = (left + right) >> 1;        if (a[mid] = = val) return mid;        if (A[mid] < val) left = mid + 1;    else right = mid-1; } return-1;}        void pushdown (int o) {if (Color[o]) {color[o<<1] = color[o<<1|1] = Color[o];    Color[o] = 0;        }}void Update (int o, int l, int r, int l, int r, int v) {if (L <= l && R >= R) {Color[o] = V;    return;    } pushdown (o);    int mid = (L + r) >> 1;    if (l <= mid) Update (O&LT;&LT;1, L, Mid, L, R, V); if (R > Mid) Update (o<<1|1, mid+1, R, L, R, v);} Int Vis[10100];//Mark whether the poster has appeared int ans;//record number void query (int o, int l, int r) {if (Color[o]) {if (!vis[color[o]))            Ans++,vis[color[o]] = true;    return;    }//return;    if (L = = r) return;    int mid = (L + r) >> 1;    Query (O<<1, L, mid); Query (o<<1|1, mid+1, R);}    int main () {int T, N;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        memset (color, 0, sizeof (color));        int len = 1;            for (int i = 1; I <= N; i++) {scanf ("%d%d", &num[i].x, &AMP;NUM[I].Y);            rec[len++] = num[i].x;        rec[len++] = num[i].y;        } sort (rec+1, rec+len+1);        discretized int RR = 2;        for (int i = 2; i < Len; i++)//scroll array optimization {if (rec[i]! = Rec[i-1]) rec[rr++] = Rec[i]; } for (int i = RR-1; i > 1; i--) {if (Rec[i]! = Rec[i-1] + 1) rec[rr++] = RE        C[I-1] + 1; } soRT (Rec+1, REC+RR);//Not rr+1 for (int i = 1; I <= N; i++) {int L = Find (num[i].x, REC, 1, RR-1);            int r = Find (NUM[I].Y, REC, 1, RR-1);        Update (1, 1, RR-1, L, R, I);        } memset (Vis, false, sizeof (VIS));        Ans = 0;        Query (1, 1, RR-1);    printf ("%d\n", ans); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2528 Mayor ' s Posters "segment tree + discretization"

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.