POJ2528解题报告,区间离散化,线段树

来源:互联网 发布:yura金亚荣家境知乎 编辑:程序博客网 时间:2024/06/14 13:00

POJ2528解题报告,区间离散化,线段树
Mayor’s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 51104 Accepted: 14790
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
这次主要学习了离散化:在不改变端点的相对顺序的情况下,将区间压缩到最小。例如,两个区间[1,10]、[5,11],对端点进行排序,然后重新赋值,令5=2,10=3,11=14,然后区间变成[1,3]、[2,4]。对输入的数排序去重后,把区间缩减。这样,总共就输入10000个线段,最多20000个端点,这样的线段树规模比原先的一千万就小了很多。

#include <cstring>#include <cstdlib>#include <cstdio>#include <algorithm>#define MAX 20010#define Lson(x) ((x) << 1)  //计算左孩子位置#define Rson(x) (((x) << 1) + 1)    //计算右孩子位置using namespace std;struct Node{    int l;  //线段左端点值    int r;  //线段右端点    int c;  //线段的颜色}nodes[MAX * 5];int map[MAX][2];    //记录输入线段的左右两个端点int record[MAX];    //记录颜色是否已经出现int total;//用于离散化struct Line{    int point;  //记录端点的坐标    int num;    //记录原来的编号}line[MAX * 2];int mycmp(const Line &a, const Line &b){    return a.point < b.point;}void buildTree(int l, int r, int Node)//口诀:赋值,判断,递归,(更新){    nodes[Node].l = l,nodes[Node].r = r,nodes[Node].c = 0;  //初始化颜色都为0    if(l == r) return ;    int mid = (l + r) >> 1;    buildTree(l, mid, Lson(Node));  //构造左子树    buildTree(mid + 1, r, Rson(Node));  //构造右子树}void insert(int l, int r, int Node, int color)//口诀:先判断完全覆盖,再判断更新左右儿子,后递归染色,(回溯更新){    if(nodes[Node].l == l && nodes[Node].r == r)    //刚好完全覆盖,修改颜色,直接返回    {        nodes[Node].c = color;        return ;    }    if(nodes[Node].c > 0)   //如果当前线段已经有颜色,先将颜色复制给左右两个子树,非常重要    {        nodes[Lson(Node)].c = nodes[Node].c;        nodes[Rson(Node)].c = nodes[Node].c;        nodes[Node].c = 0;  //将颜色传到左右子树后,标记线段没有颜色,等待再次染色    }    if(l >= nodes[Rson(Node)].l)    //完全在右子树        insert(l, r, Rson(Node), color);    else if(r <= nodes[Lson(Node)].r)   //完全在左子树        insert(l, r, Lson(Node), color);    else        //两个子树都有    {        insert(l, nodes[Lson(Node)].r, Lson(Node), color);        insert(nodes[Rson(Node)].l, r, Rson(Node), color);    }}void update(int Node){    if(nodes[Node].c != 0)  //如果当前线段有颜色,记录,不再搜索左右子树,直接返回,相当于上面的广告覆盖下面的广告。    {        if(!record[nodes[Node].c])//该颜色还没有计数        {            total++;            record[nodes[Node].c] = 1;//该下次就不会再计数        }        return ;    }    //如果当前线段没有颜色,递归调用左右子树,查询颜色    update(Lson(Node));    update(Rson(Node));    return ;}int main(){    freopen("output.txt","w",stdout);     freopen("input.txt","r",stdin);    int number,n,i;    scanf("%d", &number);    while(number--)    {        scanf("%d", &n);        for(i = 0; i < n; i++)        {            scanf("%d%d", &map[i][0], &map[i][1]);            line[2*i].point = map[i][0];    //记录数据,用于离散化            line[2*i + 1].point = map[i][1];            line[2*i].num = -(i + 1);   //线段第一个端点用负数记录            line[2*i + 1].num = i + 1;  //线段第二个端点用正数记录        }        //首先将所有端点排序        sort(line, line + 2*n, mycmp);        int temp = line[0].point;        int count = 1;  //重新开始编号,从1开始        for(i = 0; i < 2*n; i++)        {            if(temp != line[i].point)   //如果当前端点和前面的端点不一样,编号值+1            {                count++;                temp = line[i].point;            }            if(line[i].num < 0) map[-line[i].num - 1][0] = count;//反解求得相对顺序            else map[line[i].num - 1][1] = count;//反解求得相对顺序        }        buildTree(1, count, 1);        for(i = 0; i < n; i++) insert(map[i][0], map[i][1], 1, i + 1);        memset(record, 0, sizeof(record));        total = 0;        update(1);        printf("%d\n", total);    }    return 0;}
0 0
原创粉丝点击