POJ2528 Mayor's posters(线段树+离散化)

来源:互联网 发布:java ftp断点续传 下载 编辑:程序博客网 时间:2024/06/01 09:00

Mayor’s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 57835 Accepted: 16725
Description

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

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will 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 are placed given the information about posters’ size, their place and order of placement on the electoral wall.
Input

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 two integer numbers li and ri which are the number of the wall segment occupied by 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 is placed, it entirely covers all wall segments numbered li, li+1 ,… , ri.
Output

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

The picture below illustrates the case of the sample input.

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10
Sample Output

4
Source

Alberta Collegiate Programming Contest 2003.10.18
给你一块公告板,然后有很多人往上面贴海报,后面往上贴的人可以把前面贴的海报盖住,问你最后能看到几张海报(只要有露出来就算)。
首先这题告诉你公告板的宽度是千万级别的,想用线段树做区间更新,肯定不能用它直接作为叶子节点了,我们就想先给他离散化一下。因为点最多就10000*2(最多10000张 假设所有点都不一样) 那么离散化之后最多也就这些点,这样就可以建树了。再用二分查找做一下映射。然后就是正常的线段树区间更新了。

#include "cstring"#include "cstdio"#include "iostream"#include "string.h"#include "algorithm"#include "vector"#define MAXSIZE 200005using namespace std;vector<int> p;typedef struct{    int mark;    int left, right;}NODE;NODE tree[MAXSIZE * 5];int temp[200005];void build(int root, int lf, int rt){    int mid;    tree[root].left = lf;    tree[root].right = rt;    tree[root].mark = 0;    if (lf == rt)    {        tree[root].mark = 0;        return;    }    mid = (lf + rt) / 2;    build(2 * root, lf, mid);    build(2 * root + 1, mid + 1, rt);    return;}void update(int root, int left, int right, int id){    if (tree[root].left == left&&tree[root].right == right)    {        tree[root].mark = id;        return;    }    if (tree[root].mark != 0)    {        tree[root * 2 + 1].mark = tree[root * 2].mark = tree[root].mark;        tree[root].mark = 0;    }    int mid = (tree[root].left + tree[root].right) / 2;    if (mid<left)        update(root * 2 + 1, left, right, id);    else    {        if (mid >= right)            update(root * 2, left, right, id);        else        {            update(root * 2, left, mid, id);            update(root * 2 + 1, mid + 1, right, id);        }    }    return;}bool vis[20005];int sum = 0;void count(int root){    int l = tree[root].left;    int r = tree[root].right;    if (tree[root].mark != 0)    {        if (!vis[tree[root].mark])        {            sum++;            vis[tree[root].mark] = 1;        }        return;    }    if (l == r)        return;    count(root * 2);    count(root * 2 + 1);}int main(){    //freopen("in.txt", "r", stdin);    int c;    scanf("%d", &c);    while (c--)    {        int mm;        scanf("%d", &mm);        memset(temp, 0, sizeof(temp));        memset(vis, 0, sizeof(vis));        sum = 0;        int cnt = 1;        struct node        {            int left, right;        } poster[20005];        for (int i = 1; i <= mm; i++)        {            scanf("%d%d", &poster[i].left, &poster[i].right);            p.push_back(poster[i].left);            p.push_back(poster[i].right);        }        sort(p.begin(), p.end());        p.erase(unique(p.begin(), p.end()), p.end());        for (int i = 1; i <= mm; i++) {            poster[i].left = lower_bound(p.begin(), p.end(), poster[i].left) - p.begin() + 1;            poster[i].right = lower_bound(p.begin(), p.end(), poster[i].right) - p.begin() + 1;        }        build(1, 1, p.size());        for (int i = 1; i <= mm; i++)        {            int l, r;            update(1,poster[i].left, poster[i].right, i);        }        count(1);        printf("%d\n", sum);    }}
0 0
原创粉丝点击