POJ 2528-Mayor's posters(线段树+离散化)_mayor's posters openj_bailian - 2528-程序员宅基地

技术标签: 线段树  ACM  ACM_线段树  

Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 73717   Accepted: 21263

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 l i 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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+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

题目大意:一面墙给你贴广告,求在最后能看到的广告总数。

解题思路:线段树的区间更新,最后求出出广告的种类即可,主要题目给的区间范围太大,我们要对区间进行离散化一下(就是缩小一下区间跨度)。
在离散化的的时候对于区间的 宽度大于2的中间需要插一个点进去,避免区间覆盖出错!例如:下图的三个区间

AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeofa());
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(n) printf("%d\n", n);
using namespace std;
typedef pair<long long, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int T, n;
int x[10010], y[10010], a[40010], b[40010], vis[40010];
char str[10];

struct node
{
    int l;
    int r;
    int lazy;
    int num;
}e[40010*4];

void push(int k)
{
    if(e[k].lazy) {
        e[2*k].num = e[k].lazy;
        e[2*k+1].num  = e[k].lazy;
        e[2*k].lazy = e[k].lazy;
        e[2*k+1].lazy = e[k].lazy;
        e[k].lazy = 0;
    }
}

void build(int l, int r, int k)
{
    e[k].l = l;
    e[k].r = r;
    e[k].lazy = 0;
    if(l == r) {
        e[k].num = 0;
        return;
    }
    int mid = (l+r)/2;
    build(l, mid, 2*k);
    build(mid+1, r, 2*k+1);
}

void updata(int l ,int r, int add, int k)
{
    if(l == e[k].l && r == e[k].r) {
        e[k].num = add;
        e[k].lazy = add;
        return;
    }
    push(k);
    int mid = (e[k].l+e[k].r)/2;
    if(r <= mid) updata(l, r, add, 2*k);
    else if(l >= mid+1) updata(l, r, add, 2*k+1);
    else {
        updata(l ,mid, add, 2*k);
        updata(mid+1, r, add, 2*k+1);
    }
}

int quary(int l, int r, int k)
{
    if(e[k].l == l && e[k].r == r) {
        return e[k].num;
    }
    push(k);
    int mid = (e[k].l + e[k].r)/2;
    if(r <= mid) return quary(l ,r, 2*k);
    else if(l+1 >= mid) return quary(l, r, 2*k+1);
}

int main()
{
    in1(T);
    while(T --) {
        in1(n);
        int k = 0, ans = 0;
        mem0(vis);
        for(int i = 0; i < n; i ++) {
            in2(x[i], y[i]);
            a[k ++] = x[i];
            a[k ++] = y[i];
        }
        sort(a, a+k);
        int s = 1;
        b[0] = a[0];
        //离散化坐标
        for(int i = 1; i < k; i ++) {
            if(a[i] != a[i-1]) {
                b[s ++] = a[i];
                if(a[i] - a[i-1] >= 2)
                    b[s ++] = a[i-1]+1;
            }
        }
        sort(b, b+s);
        build(1, s, 1);
        k = 1;
        for(int i = 0; i < n; i ++) {
            int l = upper_bound(b, b+s, x[i]) - b; //查找角标(也就是新的区间跨度)
            int r = upper_bound(b, b+s, y[i]) - b;
            updata(l, r, k++, 1);
        }
        for(int i = 1; i <= s; i ++) { //查找广告种类
            if(!vis[quary(i, i, 1)] && quary(i, i, 1)) {
                ans ++;
                vis[quary(i, i, 1)] = 1;
            }
        }
        out1(ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/I_believe_CWJ/article/details/80284801

智能推荐

PhpStrom2018的激活范式_phpstrom2018.2.3-程序员宅基地

文章浏览阅读409次。1:找到windows-》System32-》drivers-》etc-》host-》配置域名(0.0.0.0 account.jetbrains.com)2:打开浏览器http://idea.lanyus.com/ 获取注册码3:复制注册码 ,注册时切换至Activation Code选项4:OK。_phpstrom2018.2.3

3dmax渲染有噪点的六大原因及解决方案_3dmax噪点多怎么解决-程序员宅基地

文章浏览阅读261次。原因:环境阻光(AO)的设置不当,导致模型结构转变处产生过多的暗部阴影,从而使渲染图像呈现出颗粒感和模糊。原因:3dmax中的材质细分不够,影响渲染效果,导致图像出现颗粒感和噪点。原因:3dmax中的图像尺寸过低,导致渲染后的效果图呈现出颗粒感和噪点。原因:发光贴图和灯光缓存的设置不当,导致渲染图像出现颗粒感和噪点。原因:3dmax中的主光源灯光细分不够,导致渲染图像有颗粒感。原因:DMC采样的参数设置不合理,导致渲染图像出现噪点。按照这些设置,通常可以避免图像出现噪点。,从而避免渲染后的模糊和噪点。_3dmax噪点多怎么解决

前端渲染CSR和SSR的结合使用分析_next同时使用ssr与csr-程序员宅基地

文章浏览阅读1.2k次。我们都知道,以往的CSR(客户端浏览器渲染)多多少少会有一点点SEO问题,不只是SPA(单页面应用程序),只不过SPA的SEO问题比较严重,一般的前端项目有很多个页面,渲染的压力是分散的,所以页面渲染速度很快,基本够爬虫抓到很多内容,但SPA只有一个页面。而我们的SSR(服务器渲染)可以弥补像SPA项目的SEO(搜索引擎优化) 不友好问题。但是它本身对比CSR也是有不足的。所以,为什么不可以结合它们两个的优点去进行使用呢?_next同时使用ssr与csr

2022-IOS-For-Fun_um-ios 2022-程序员宅基地

文章浏览阅读503次。2022 IOS Developer for funBasic stuffComputer Science fundamentalsMain parts of a computer system - CPU, memory, storageHow Operating System worksWhat is a databaseHow Internet worksGit version controlObject Oriented ProgrammingThe setupMacOSHomeb_um-ios 2022

PHP中的循环描述错误有哪些_PHP关于while循环中修改选取条件出现的错误-程序员宅基地

文章浏览阅读109次。业务需求是:读取某个表中每一行的的字段A、B、C的值如果C的值是0,就改成1或者2代码大概是这么写的:$query = "SELECT * FROM table WHERE C = 0";$result = mysqli_query($link, $query);if($result){while ($rows = mysqli_fetch_array($result)){if (判断条件为tru..._while循环报错php

ionic介绍-程序员宅基地

文章浏览阅读3.4k次。最近公司在使用ionic做混合APP,虽然是最后端,但是也查一下东西,介绍一下吧这是菜鸟教程的Ionic一.介绍ionic是一种老式的使用H5开发iOS和Android应用的方式,也可以使用新的语言React Native开发,当然对于H5实现复杂的或者交互性没有那么好的,就可以使用iOS和Android的插件实现;二.Ionic特点a.开发方面:1.ionic 基于Angular..._ionic

随便推点

vue基于element-ui的Select选择器实现的动态多级联动下拉选择_element-ui select 级联-程序员宅基地

文章浏览阅读2w次,点赞6次,收藏26次。demo地址代码如下:Html<div id="app"> <el-select v-for="(arrItem,key) in selectList" :key="key" v-model="selectArr[key]" filterable placeholder="请选择" value-key="value" @change="selected" @focu..._element-ui select 级联

LeetCode——76. 最小覆盖子串_leetcode最小覆盖子串-程序员宅基地

文章浏览阅读123次。76. 最小覆盖子串_leetcode最小覆盖子串

Oracle 常用语句_oracle查询导入目录常用语句-程序员宅基地

文章浏览阅读112次。https://download.csdn.net/download/u014096024/21109113oracle练习1.如何查询一个角色包括的权限 a.一个角色包含的系统权限 select * from dba_sys_privs where grantee='DBA'; b.一个角色包含的对象权限2.oracle究竟有多少种角色 (查询oracle中所有的角色,一般是dba) select * from dba_roles;3.查询o..._oracle查询导入目录常用语句

数据可视化之美:经典案例与实践解析_数据可视化经典-程序员宅基地

文章浏览阅读9.3k次,点赞25次,收藏93次。随着DT时代的到来,传统的统计图表很难对复杂数据进行直观地展示。这几年数据可视化作为一个新研究领域也变得越来越火。成功的可视化,如果做得漂亮,虽表面简单却富含深意,可以让观测者一眼就能洞察事实并产生新的理解。可视化(visualization)和可视效果(visual)两个词是等价的,表示所有结构化的信息表现方式,包括图形、图表、示意图、地图、故事情节图以及不是很正式的结构化插图。基本的可视化展..._数据可视化经典

8086汇编4位bcd码_[走近FPGA]之二进制转BCD码-程序员宅基地

文章浏览阅读1.3k次。注:本文由不愿透露姓名的 @Bulingxx 撰写。以下为正文。在上一篇文章中介绍了数码管如何在FPGA开发板上实现动态显示,其文章链接如下:人生状态机:[走近FPGA]之数码管动态显示​zhuanlan.zhihu.com本文的所有实例都使用硬木课堂Xilinx Aritx 7 FPGA板实现,且附有上板演示视频,该开发板的链接如下:硬木课堂 Xilinx Aritx 7 FPGA板 Arm C..._8086汇编语言 实现二进制数到bcd码的转换

使用nfs之后初始化mysql失败_influxdb数据库 nfs存储初始化失败-程序员宅基地

文章浏览阅读1.7k次。将nfs作为mysql的数据目录输出后,在另一台主机上启动mysql进程时,会出现如下这样的错误,究其原因,其实还是nfs自身设计的缺陷。 初始化就是使用特定的用户,去特定的目录去更新mysql,虽然说添加mysql用户之后,所有的对数据的修改权限都是以mysql用户执行的,而且nfs的数据目录也都设计成了mysql,常理是没有问题的。但是,执行mysql_ins_influxdb数据库 nfs存储初始化失败

推荐文章

热门文章

相关标签