POJ2528___(线段树+哈希(离散化))

来源:互联网 发布:上海网络推广5atg 编辑:程序博客网 时间:2024/06/17 09:03

本文出自Svitter的blog

——踏踏实实的做事儿啊!

POJ2528

题意


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.

给你n张海报,海报都是正好一块,不会在中间。每张海报给你两个l, r两个边界,求没有被盖住的海报的个数

输入输出分析


Input151 42 68 103 47 10output4

算法数据结构分析


线段树+哈希表(离散化) 边利用离散化进行处理,来减少多余的区间。通过存左右边到数组,然后排序,把中间多余的部分使用哈希函数处理掉,去掉重复的值。
利用nc = unique(x,x+nc)-x去掉重复的值得到个数。 注意贴海报的,先贴最外面的,然后把里面的往里插入,如果能露出来说明没有被盖住。如果线段树左右两边都被盖住, 整个区间都被盖住。

AC代码:


//author: svtter//#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <map>#include <algorithm>#include <queue>#include <cmath>#define INF 0xffffff#define lln long long#ifdef ONLINE_JUDGE#define FOI(file) 0#define FOW(file) 0#else#define FOI(file) freopen(file,"r",stdin);#define FOW(file) freopen(file,"w",stdout);#endifusing namespace std;struct CNode{    int l, r;    bool cover;    CNode *pLeft, *pRight;    int mid()    {        return (l+r)/2;    }};struct poster{    int l, r;};#define N 10000000CNode tree[N];poster post[10100];int x[20200];int hash[N+10];int ncount = 0;void BuildTree(CNode *root, int l, int r){    root->l = l;    root->r = r;    root->cover = false;    if(l == r)        return;    ncount++;    root->pLeft = tree + ncount;    ncount++;    root->pRight= tree + ncount;    BuildTree(root->pLeft, l, (l+r)/2);    BuildTree(root->pRight, (l+r)/2+1, r);}bool Post(CNode *root, int l, int r ){    if(root->cover) return 0;    if(root->l == l && r == root->r)        return root->cover = 1;    bool res;    if(r <= root-> mid())        res = Post(root->pLeft, l, r);    else if(l > root->mid())        res = Post(root->pRight, l, r);    else    {        bool a = Post(root->pLeft, l, root->mid());        bool b = Post(root->pRight, root->mid()+1, r);        res = a||b;    }    //两边都被覆盖了, 那么本身就被覆盖了。    if(root->pLeft->cover && root->pRight->cover)        root->cover = true;    return res;}int main(){    //FOI("input");    //FOW("output");    //write your programme here    int c, n;    int i;    int sum;    int nc = 0;    int hashkey=1;    scanf("%d", &c);    while(c--)    {        nc = 0;        scanf("%d", &n);                for(i = 1; i <= n ;i ++)        {            scanf("%d%d", &post[i].l, &post[i].r);            x[nc++] = post[i].l;            x[nc++] = post[i].r;        }        sort(x, x+nc);        nc = unique(x, x+nc) - x;        hashkey = 1;        for(i = 0; i < nc; i++)            hash[x[i]] = hashkey++;                CNode *root = tree;        BuildTree(root, 1, hashkey);        sum = 0;        for(i = n; i >=1 ; i--)        {            if(Post(root, hash[post[i].l], hash[post[i].r]))                sum++;        }                //printf("MAX:%d\n", MAX);        printf("%d\n", sum);    }    return 0;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩不想去学校怎么办 讨厌父母的性格怎么办 老师揪孩子耳朵怎么办 老师整天骂孩子怎么办 教师被学生骂怎么办 幼儿园学生骂老师怎么办 学生骂老师外号怎么办 小孩上课很多嘴怎么办 学生老玩手机怎么办 和校长有了矛盾怎么办 家长打了我怎么办 小朋友不想去幼儿园怎么办 小朋友不想上幼儿园怎么办 高中老师打学生家长怎么办 老师偏心学生该怎么办? 老师能打孩子怎么办 有的幼儿打老师怎么办? 孩子特别怕老师怎么办 孩子跟老师认生怎么办 和搭班老师不合怎么办 学生厌学了老师怎么办 孩子很怕我怎么办 斗米报名之后怎么办 孩子鼻子长疮怎么办 六年级孩子不爱学习怎么办 孩子不要爱学习怎么办? 孩子说不愿意读书怎么办 孩子三天没吃饭怎么办 孩子三天不吃饭怎么办 初中学生不爱学习怎么办 孩子写字头歪怎么办 学习习惯差该怎么办 孩子沉迷网络游戏家长怎么办 二年级贪玩厌学怎么办 孩子太注重外表怎么办 宝宝老爱摔跟头怎么办 小孩很讨厌你怎么办 初二作业没写完怎么办 初中孩子上课困怎么办 老师不搭理孩子怎么办 心里纠结一件事怎么办