【POJ】poj 2528 (线段树_离散化)

poj2528(贴海报)线段树离散化 //poj2528贴海报(线段树离散化) #include #include #include #include using namespace std; const int maxn=100005; //要开10倍的数组,否则RE bool hash[maxn]; int li[maxn],ri[maxn]; int cov[maxn<<4]; int a[maxn*3]; int cnt; 阅读详情

 

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

题目大意:在一个公告栏上面张贴海报;张贴之后问可以看到几张海报

题目分析:线段树问题,但是如果以一个点为叶子节点的话就会超时妥妥的,所以采用离散化思想,首先将所有的点进行排序;用一个数组来进行对应,下标对应实际坐标,下标对应的数字表示离散化之后的坐标,通过离散化之后的坐标可以建立线段树

代码:

 

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>

using namespace std;
struct code
{
    int r;int l;
}p[50005];
bool tree[50010*4];///这个树的定义是是否这个区间被覆盖;
void build ( int p,int l,int r)
{
    if ( l == r ) {tree[p] = true; return;}///将所有区间赋值为没有被覆盖
    int mid = ( l + r ) >> 1;
    build ( 2 * p, l, mid);
    build ( 2 * p + 1,mid + 1, r);
    tree[p] = tree[p*2]||tree[p*2+1];
}
bool post(int p,int l,int r, int sl, int sr)
{
    if ( tree[p] == 0 ) return false;
    if ( sl <= l&& sr >= r )
    {
        tree[p] = false;
        return true;
    }
    int mid = ( l + r ) >> 1;
    bool ok = 0;
    if ( sl <= mid ) ok = post( 2 * p , l , mid, sl , sr )||ok;///如果左边区间有值的话在左边区间查询
    if ( sr > mid ) ok = post( 2 * p + 1, mid + 1, r, sl, sr )||ok;///如果右边区间有值的话在右边区间查询
    tree[p] = tree[p*2]||tree[p*2+1];
    return ok;
}
int x[50005 << 1];
int hashs[10000002];
int posts;
int main()
{
    //freopen("in.txt","r",stdin);
    int test;
    scanf ( "%d",&test);
    while ( test > 0 )
    {
        test--;
        scanf("%d",&posts);
        int counts = 0;
        for ( int i = 0; i < posts; i++ )
        {
            scanf("%d %d",&p[i].l,&p[i].r);
            x[counts++] = p[i].l;
            x[counts++] = p[i].r;
        }
        sort(x,x+counts);
        counts = unique(x,x+counts) - x;///变量去重
        int node = 0;
        for ( int i = 0; i < counts; i++ )
        {
            hashs[x[i]] = node;///将所有的点进行对应,下标表示原来的坐标,下标对应的数字应该是离散化之后的坐标
            if ( i < counts - 1 )
            {
                if ( x [i+1] - x [i] == 1 )
                    node++;
                else
                    node+=2;
                ///如果两个变量之间的坐标差值超过1的话就要将中间的区间离散化进去;
                ///如果为1 10;1 4;5 10;和 1 10;1 4;6 10;如果第二种情况四和六之间差值大于1,如果不将中间的
                ///5离散化之后就会将第一种情况和第二种情况计算为2;
            }
        }
        build ( 1, 0,node);///建树的时候要注意区间是从0到node的这个区间是左闭右闭;
        int ans = 0;
        for ( int i = posts-1; i >= 0; i--)
        ///这里是将所有的海报倒着循环,也就是说如果前面的海报的所有地方被后面的海报占的话就不能显示出来了
        {
            if (post (1,0,node,hashs[p[i].l],hashs[p[i].r]))
                ans++;
        }

        printf ("%d\n",ans);

    }
}

 

 

 

 

 

 

 

 

IDEA中Maven工具栏与命令执行全攻略:高效开发必备技巧 IntelliJ IDEA 作为强大的 Java 开发工具,其 Maven 功能独具优势。Maven 工具栏位于工作区右侧,图标功能丰富,涵盖项目导入、源码处理、依赖操作等。执行 Maven 命令有多种途径,Run Anything 窗口可输入或选目标执行,上下文菜单能右键运行目标,还可创建运行配置,指定命令参数后运行,为开发者高效管理 Maven 项目、便捷执行命令提供有力支持。 阅读详情

相关推荐

【学习笔记】一文搞懂IEPE传感器

本文介绍了IEPE(集成电路压电式)传感器的工作原理及其在振动监测中的应用。通过压电效应原理,IEPE传感器将机械振动转换为电信号,内置电荷放大器将高阻抗电荷信号转换为低阻抗电压信号输出。还介绍了传感器的三轴测量结构、4mA恒流源供电机制以及灵敏度与量程的关系,并通过风扇实验演示了故障诊断过程。实验使用USB-3123采集卡和SRD-1104信号调理器,展示了正常与异常状态下的振动数据差异,验证了IEPE传感器在设备状态监测中的有效性。

m0_62969865的博客 2815

UVA10587 Mayor‘s posters 题解 线段树 离散化

damn,bro,No正规翻译,(氧化钙)

达瓦里希,这里禁喵。 1096

UOS的ARM机编译QT5.14.2记录

我需要的qt版本是Qt5.14.2,所以选择编译Qt5.14.2源码;几个月前尝试了编译一次,当时编译过程中出现了报错,当时时间很紧最后安装的默认版本来使用,最近有时间重新进行了一次编译,历经3个星期(4G的内存)终于解决了各种坑,编译成功了,现在将编译过程中遇见的一些坑记录一下,方便日后能记得这一次可恶的编译过程,如有描述欠缺的地方,还希望各位大佬给点意见指导指导🙏。

NiF693zzz的博客 1617

C语言线段树(4)___Mayor's posters(2528)

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 coun

YzlCoder的记事本 1272

Rockchip RK3568实战:手把手教你调试I2S音频驱动(附ALSA设备树配置详解)

本文提供Rockchip RK3568平台I2S音频驱动的实战调试指南。详细解析了从硬件连接到软件配置的全过程,重点涵盖ALSA驱动框架下的设备树配置、时钟计算、DMA参数设置以及常见问题(如无声、爆音)的排查与解决方法,帮助开发者快速定位并解决音频驱动难题。

weixin_28358083的博客 495

Autowired的使用介绍

我们都知道在spring中@Autowired注解,是用来自动装配对象的。@Service@Autowired没错,这样是能够装配成功的,因为默认情况下spring是按照类型装配的,也就是我们所说的byType方式。此外,@Autowired注解的required。

白话机器学习 1349

【线段树】Mayor's posters

描述 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 final...

weixin_38281074的博客 332

BUCK-BOOST升降压电路Multisim电路仿真分析

本文分析了三极管型BUCK-BOOST升降压开关电源的工作原理。该电路结合了BUCK降压和BOOST升压拓扑,通过控制三极管Q1的高频通断,使电感L1周期性储能和释能,实现输出电压可高于或低于输入电压且极性相反。详细阐述了Q1导通和关断阶段的工作过程,推导了输出电压与占空比的关系式Vo=-D/(1-D)*Vi,并分析了不同占空比下的工作模式(降压、升压)。通过Multisim仿真验证了理论分析,当D=25%时输出降压至1.6V,D=60%时输出升压至7.5V。电路具有升降压灵活、极性反转、效率高等特点,适用

XU157303764的博客 1435

poj 2528 线段树的离散化

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 placin

SsteelXD的博客 320

Open3D 计算点云基本特征

在 Open3D 中,可以通过 PCA(主成分分析)来求得点云的特征值和特征向量。然后基于这些特征值,进一步计算各种几何特征,如线性、平面性、球度等。

纵马踏花向自由 1605

【网络与爬虫 62】小程序爬虫终极攻略:微信支付宝小程序数据采集完全指南

本文深入解析微信和支付宝小程序的爬虫技术,从基础架构分析到高级逆向工程,涵盖WXML/AXML模板解析、API接口拦截、网络流量分析等核心技术。通过实战案例和代码示例,帮助开发者掌握小程序数据采集的完整技术栈。

熵数实验室 3415

POJ 2528(线段树)-Mayor's posters

题目: 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 fina...

weixin_44757834的博客 220

硬件电路设计采集电路

摘要:采集电路是硬件设计中连接物理世界与数字系统的关键环节,主要用于将温度、压力等物理量转换为电信号。常见的采集电路包括模拟量采集(如温度、电压)、数字量采集(如开关状态)和特殊信号采集(如光、声音)。设计时需重点关注传感器选型、信号调理、电源接地、抗干扰和ADC配置等核心要素,确保信号链路的完整性和稳定性。合理的电路设计需结合具体应用场景,通过仿真和测试验证性能,最终实现精确可靠的数据采集。

qq_37999850的博客 1797

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

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

Dafang_Xu的博客 793

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Statistics and Machine Learning Toolbox

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

线段树之离散化(POJ-2528)

线段树系列之离散化,介绍了离散化的步骤以及线段树离散化的应用。

月巴虫丰博客 1611

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Mayors posters POJ - 2528 (线段树+离散化)

思路:对坐标进行离散化,然后建立线段树进行染色,每次对他的区间染一个颜色,然后遍历整面墙看看有多少个不同颜色 #include <cstdio> #include <cstring> #include <algorithm> #include <set> #include<iostream> //#include<bits/stdc++.h> using namespace std; typedef long long ll; #..

Ray.C.L的博客 507

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

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

L_X_ 235

POJ2528线段树区间合并加离散化

POJ2528Mayor’s posters就只支持一种操作,一次性的给一段区间涂上颜色,且每次颜色均不一样。问最后一共可以看到多少种颜色。 关于线段树的更详细实现请参考线段树解决区间问题包括延迟操作以及离散化 /* 在数轴上,一次给一个线段涂上颜色 后面的颜色会覆盖前面的颜色 问最后能看到多少个颜色 显然是成段更新,线段树 区间范围是1千万,需要离散化 ...

RBS的专栏 314

POJ2528,线段树+离散化

题目链接: POJ2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 50574   Accepted: 14658 Description The citizens of Bytetown, AB, could not

北 501

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

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

L_X_ 360

poj 2528 线段树数据离散化+区间更新

POJ2528 题大意为在一片等高长度为10000000的墙上粘贴n(n<=10000)份海报,在相同区间内后粘贴的海报会覆盖之前海报,求所有操作结束后有多少张海报未被完全覆盖。 Sample Input1 5 1 4 2 6 8 10 3 4 7 10Sample Output4因为题目的数据规模较大,直接建立线段树会使用过高的空间并造成极大的空间浪费,所以需要对数据进行离散化处

the_Nickname的博客 725

POJ 2528 Mayor's posters // 线段树 区间更新 离散化

题目描述POJ 2528 Mayor’s posters解题思路题目大意: 在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报?首先数据量较大,应该进行离散化,然后进行线段树的区间更新和查询. 但这题的难点在于每个数字其实表示的是一个单位长度(并非一个点),这样普通的离散化会造成许多错误 给出下面两个简单的例子应该能体现普通离散化的缺陷: eg1: [1, 10] [1, 5] [

FZU_zyt的专栏_(:з」∠)_ 403

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

本文出自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 place

svitter的专栏 1976

POJ - 2528 Mayor's posters (线段树离散化+区间更新)

POJ - 2528 http://poj.org/problem?id=2528 以为过了但是看了discuss,有组数据有问题,先放着,之后会回来改。 想要学习骚气的写法所以改了改自己的线段树,第一次自己能主动想到离散化,结果还是有问题。//离散化+线段树(区间更新)/*过不了的数据 3 3 5 6 4 5 6 8 3 主要是这组 1 10 1 3 6 10 5 1 4 2 6 8

shinimashzi 345

POJ 2528(线段树 区间更新 离散化)

POJ 2528题目大意有一排分成若干组长为1区间的墙,现在要往墙上贴n张海报,一个海报覆盖连续的L到R的区间,按照一定的顺序贴,问最后有多少海报全部或有一部分漏在了表面。(1≤L≤R≤107,n≤10000)(1\le L \le R\le 10^7 ,n\le 10000)分析由于L和R比较大但线段数目不大(最多出现20000种数字),所以需要先对数据进行一下离散化,将L和R映射到一个较小的区间

AIDreamer 1193
上一篇: 【HDU】hdu 1394(线段树)
下一篇: 【POJ】poj 1094(拓扑排序)
三生石1103
博客等级 码龄12年 16粉丝 87原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值