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

《数据结构》04-树4 是否同一棵二叉搜索树 题目 给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。 输入格式: 输入包含若干组测试数据。每组数据的第1行给出两个正整数N (≤10)和L,分别是每个序列插入元素的个数和需要... 阅读详情

Mayor's posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 55517 Accepted: 16098

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
 用到线段树,因为最多有10^6,建那么大的树肯定会爆空间和时间,所以想到要离散化,将每条线段的两端,就是海报的两头分开考虑,所有的端点放在一起非递减排序,
去掉相同的,计算出有n个不同的点,编号从1到n,——这是离散化过程。然后建二叉树。更新的时候先二分查找那条线段两端的点
的编号,然后在那棵建好的树上更新,遇到一样的节点就覆盖,如果线段在那个节点的子节点上,而且这个节点又被其他线段覆盖
过,那么就要更新它的子节点,他自己改成不被覆盖的。最后计算结果那里要用个辅助数组,从根节点开始找被覆盖的节点,分别
将不同的值放在数组里,最后看有几个结果就是几个。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
const int maxn = 10010;
struct node
{
    int l, r;
} Poster[maxn];
int Section[maxn*2];
int Hash[maxn*1000];
struct Tree
{
    int l, r;
    bool flag;
} ans[maxn*10];

void Build(int l, int r, int rt)
{
    ans[rt].l = l, ans[rt].r = r;
    ans[rt].flag = false;
    if(l == r)  return;
    int mid = (l + r) >> 1;
    Build(l, mid, rt<<1);
    Build(mid+1, r, rt<<1|1);
}

bool Judge(int l, int r, int rt)
{
    if(ans[rt].flag) return false;
    if(l == ans[rt].l && r == ans[rt].r){
        ans[rt].flag = true;
        return true;
    }
    bool Flag;
    int mid = (ans[rt].l + ans[rt].r) >> 1;
    if(r <= mid) Flag = Judge(l, r, rt<<1);
    else if(l > mid) Flag = Judge(l, r, rt<<1|1);
    else{
        bool flag1 = Judge(l, mid, rt<<1);
        bool flag2 = Judge(mid+1, r, rt<<1|1);
        Flag = (flag1 || flag2);
    }
    if(ans[rt<<1].flag && ans[rt<<1|1].flag) ans[rt].flag = true;
    return Flag;
}

int main()
{
    int T, n;
    scanf("%d",&T);
    while(T-- && scanf("%d",&n)){
        int Count = 0;
        for(int i=0; i<n; i++){
            scanf("%d%d",&Poster[i].l, &Poster[i].r);
            Section[Count++] = Poster[i].l, Section[Count++] = Poster[i].r;
        }
        sort(Section, Section+Count);
        Count = unique(Section, Section+Count) - Section;
        for(int i=0; i<Count; i++)
            Hash[Section[i]] = i;
        Build(0, Count-1, 1);
        int ant = 0;
        for(int i=n-1; i>=0; i--)
            if(Judge(Hash[Poster[i].l], Hash[Poster[i].r], 1))
                ant++;
        printf("%d\n",ant);
    }
    return 0;
}



在 ABAP CDS View 里把 Conversion Functions 用到极致:FLTP 精度、单位换算、货币换算与小数位处理的工程化实践 数据格式转换在真实项目中比业务逻辑更容易引发问题。ABAP CDS中的转换函数(如FLTP_TO_DEC、UNIT_CONVERSION、CURRENCY_CONVERSION和DECIMAL_SHIFT)可帮助在数据库层统一数据口径,避免应用层重复处理。这些函数支持数值精度治理、单位换算、货币转换和小数位调整,能显著提升报表性能并减少逻辑分散。通过将格式转换前移到数据模型层,可以确保服务层和UI层的数据一致性,同时降低接口传输错误风险。 阅读详情

相关推荐

【超详细】C#的单例模式

本文详细介绍了C#中的单例设计模式,包括两种实现方式:普通类泛型单例(Singleton<T>)和基于MonoBehaviour的组件单例(MonoSingleton<T>)。单例模式通过私有构造、静态私有实例和公共访问入口确保全局唯一实例,具有节省资源、保证数据一致性等优势。文章分别阐述了两者的核心原理、代码实现、使用场景及注意事项,并提供了日志管理器和音效管理器的具体应用示例。特别强调了多线程安全、初始化时机和避免过度使用等重要原则,为Unity开发中合理运用单例模式提供了全面指导。

grapefruit_lyy的博客 1534

POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

POJ.2528 Mayor’s posters (线段树 区间更新 区间查询 离散化)题意分析贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报。最多有10000张海报,海报左右坐标范围不超过10000000。 一看见10000000肯定就要离散化了,因为建树肯定是建不下。离散化的方法是:先存到一个数组里面,然后sort,之后unique去重,最后查他离散化的坐标lower_bo

Pengwill's Blog 332

华三(H3C)网络设备配置命令大全与学习指南

本文全面介绍 H3C 网络设备的配置命令,涵盖路由器、交换机、ACL、NAT、VTY、SSH、本地用户、日志、STP、防火墙、QoS 等模块,适合网络工程初学者与运维人员参考。

DreamCollector的博客 6568

【离散化+线段树】POJ 2528 Mayor's posters

POJ 2528 Mayor’s posters 题意: 在一个长度为1e7的墙上贴海报。n(<=1e4)张海报贴上去,海报可能会出现覆盖,贴完所有的海报之后能看见多少张(能看见一部分也算)海报。给出的海报信息是从第一张到最后一张(也就是贴的顺序)。 思路: 首先海报的位置可以是1-1e7间的任意位置,所以我们需要离散化一下。 关于离散化: 1. 左右位置2n个数去重排序后,如果前后两...

L_X_ 235

SAP与泛微OA集成(半导体行业)

本项目通过SAP PO中间件集成SAP与泛微OA在半导体行业的应用,由于行业特殊性,广泛深入应用了RSA加密等特性

集信通 1164

STM32驱动数码管:从硬件连接到动态显示实现

本文详细介绍了使用STM32单片机驱动数码管的完整流程,从硬件连接到软件实现。内容涵盖共阳/共阴数码管原理、GPIO配置、段码表生成,并重点解析了多位数码管动态显示的核心技术,包括动态扫描原理、消影处理及实战代码,帮助开发者快速掌握这一嵌入式开发基础技能。

p5l2m9n4o6q的博客 575

POJ 2528 Mayor's posters 线段树+离散化

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 counci

MetalSeed 4791

NoC架构深度解析:从拓扑结构到流量控制的全面指南

本文深入解析了片上网络(NoC)架构,从总线架构的瓶颈出发,阐述了NoC作为芯片内部“交通网”的核心优势。文章详细探讨了主流拓扑结构(如Mesh、Torus、Ring)的选型权衡,并系统介绍了数据包格式、路由算法、路由器流水线、流量控制机制以及网络接口(NI)设计,为理解现代多核SoC的通信基石提供了全面指南。

gg901的博客 1042

POJ2528:Mayor's posters(线段树区间更新+离散化)

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

ACM!荣耀之路! 7869

CircuitBreaker Resilience4J 介绍

在分布式环境中,我们的应用可能会面临着各种各样的可恢复的异常(比如超时,网络环境异常),此时我们可以利用不断重试的方式来从异常中恢复(Retry Pattern),使整个集群正常运行。然而,也有一些异常比较顽固,突然发生,无法预测,而且很难恢复,并且还会导致级联失败(举个例子,假设一个服务集群的负载非常高,如果这时候集群的一部分挂掉了,还占了很大一部分资源,整个集群都有可能遭殃)。如果我们这时还是不断进行重试的话,结果大多都是失败的。

zzhongcy的专栏 3541

深度强化学习调参技巧:以DQN、DDPG、TD3、PPO、SAC等算法为例

深度强化学习调参技巧:以D3QN、DDPG、TD3、PPO、SAC算法为例

丨汀、的博客 3445

poj 2528 Mayor's posters 线段树+离散化技巧

poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) 假设给出: 1~10 1~4 7-10 最后可以看见三张海报 如果离散化的时候不注意,就会变成 1 4 7 10(原始) 1 2 3 4 (离散化) 转化为: 1~4 1~2 3~4 这...

ddqb3235的博客 208

使用GitLab限制开发者使用CI/CD的权限,三种方案

使用三种方案来限制开发人员的CI/CD权限。

知无涯 5098

poj 2528 Mayor's posters 线段树+离散化

题意: 有一个100000000长的墙,现在要到墙上贴一些海报,因为贴的时候可以覆盖所以问最后你可以看见多少张海报(不要求完整的海报,有一个部分没被覆盖那么 就认为可以看见这个海报) 其中海报最多10000张 海报有2个数据 它贴在墙上的左端点值l和右端点值r 其中 110000000 数据的输入格式  先给出 T 情况的种数,再给出每一种情况的海报数量N 然后给出N张海报的数据,ok,题意

柯安的专栏 1023

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

[poj 2528] Mayor's posters 线段树+离散化

Mayor’s posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 54922 Accepted: 15935Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor

ATOD 613

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【线段树_离散化_染色问题】POJ 2528 Mayor's posters

POJ 2528 Mayor's posters 题意: 在一个长度为1e7的墙上贴海报。n(<=1e4)张海报贴上去,海报可能会出现覆盖,问:贴完所有的海报之后能看见多少张(能看见一部分也算)海报。给出的海报信息是从第一张到最后一张(也就是贴的顺序)。 思路: 线段树染色问题。 区间数据范围过大,离散化。【注意因为是区间,所以离散化的时候要注意两个数据是不是...

L_X_ 360

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

离散化/线段树 (POJ - 2528 Mayor's posters)

题目链接 对于这个题,网上的很多题解对离散化的过程基本是一笔带过,本蒻苟看了很久才懂。现在在这详细讲一下这个题区间的离散化。 首先,看下面这个例子(几个区间): [1, 5] [2, 7] [8, 10] 在数组上表示: 这是咱们发现一个事实:对于这些区间,我们保留某些点的信息是没有意义的,比如:3,4,6,9。对于这些点,它们一直被包含在一个大区间里。所以,我们考虑把这些点压缩点,你可以想...

平凡人Kalzn的博客 1278

poj2528 Mayor's posters(线段树,离散化)

离散化的思想: 对于这样的数据 (3,10000), (9,1000000), (5,100000), (1,1000), (7,1000000) 我们可以将其处理为 (2,7), (5,9), (3,8), (1,6), (4,9) 我们再对离散化之后的数据进行处理就行了。 题目意思: n(n 求出最后还能看见多少张海报。 参考代码:

Jinbao 973

POJ 2528 Mayor's posters 线段树和离散化

POJ 2528 Mayor's posters线段树和离散化

NNDXNM的专栏 668

POJ 2528 Mayor's posters 线段树+离散化

传送门:POJ2528 题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,

WA是一笔财富 240

poj2528 Mayor's posters 线段树+离散化

题目链接在这里 题目大意: 在墙上贴海报,每张海报占据[l, r]区间,海报之间可能存在覆盖。问所有的海报贴完之后,能有几张海报可以被看到。 解题思路: 如果暴力扫一遍的话肯定会超时,所以应该用线段树。但是区间太大了,所以要考虑离散化。 普通的离散化有问题,这里参考了一位博主的博客进行离散化。 我一直RE,数组开打了4倍才AC的,如果有RE的同志记得开大数组试试。 代码如下: #...

Joel 230

POJ2528 Mayor's posters 线段树区间更新+离散化

题目链接:http://poj.org/problem?id=2528 题目大意:一个长度为10^7的墙上有n张海报,可能相互重叠,告诉你每张海报的宽度(高度都一样)和先后的叠放次序,问没有被完全盖住的海报有多少张(海报端点不会落在瓷砖中间)。 分析:如果每个叶子节点都代表一块瓷砖,那么线段树会导致MLE,即单位区间的数目太多。实际上,由于最多有1W张海报,共计2W个端点,这些

AC_Gibson的专栏 545

poj 2528 Mayor's posters 离散化+线段树

题目大意:        在某段区间上jia

LHYL 559

POJ 2528 Mayor's posters 【离散化+线段树区间覆盖】

题目链接:http://poj.org/problem?id=2528 思路: 这里着重讲讲离散化的方式。离散化的方式是根据题目意思来选择的,不同的题目会有不同的离散化方式。 这里题目要求的是在n次区间覆盖过后,求没被覆盖的海报有多少个; 题目给出可能的区间多达 1e7 ,如果用线段树的区间覆盖就需要开 4倍的空间,因为题目给出的空间有限,直接就用线段树显然是不行的。 我们先从结果开始考虑...

Y390d的博客 376
上一篇: Ubuntu 安装qq2015
下一篇: PAT1004福尔摩斯的约会
__铭
博客等级 码龄12年 35粉丝 274原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值