线段树的入门加简单应用(POJ 2528 Mayor's posters )

线段树的实现原理还有一些基础的板子在下面这篇博客里写的非常清楚,(我完全通过看这篇博客学会线段树的)

https://www.cnblogs.com/TheRoadToTheGold/p/6254255.html

给几个基础的题目可以对比着来看

http://nyoj.top/problem/108

http://nyoj.top/problem/116

http://nyoj.top/problem/119

http://nyoj.top/problem/123

http://nyoj.top/problem/228

这几个是士兵杀敌系列,有些用线段树可以过,有些卡时间,卡空间的会过不了,用树状数组可以过(现在不讲树状数组),但是足以让我们理解线段树的运用,单点查询,单点修改,区间查询,区间修改。

懒标记是区间修改的重要运用,也是线段树中的重点难点。

这上面的懒标记我看了很久才看懂,这里我着重说明一下。

懒标记存储的是,这段区间的每个士兵的杀敌数,

懒标记就像他的名字一样,因为懒,所以用不到的就不更新,一旦更新,把懒标记传给子节点,自身变为零,等待下一次的更新。

重新定义一个变量f来表示懒标记,当更新到某一个节点时,如果不需要往下面更新,就停止,懒标记也到此不往下面传,如果需要往下面更新,就把自身的懒标记传给子节点(左右都要,不管下面用到了那个节点),自身的f变为0,同时也要更新此节点的权值w。

也不知道我说明白没有,语言组织能力有点差。。。。

 

下面说一道线段树应用的题目。

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 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


题目分析:

再分享一篇博客,https://blog.csdn.net/obsorb_knowledge/article/details/80514996

我再补充下我的想法,

1,这道题输入的n就代表有几种颜色只不过后面涂上去的会把之前涂得颜色给盖住,显现出来的是最上面那种颜色,此时就可以用1到n来表示每种颜色,区间修改的时候,没有颜色就用0表示,下面的stu数组就是构造的线段树,和之前的存储数据不一样,这里存储的是颜色,所以不能让节点的权值等于子节点的权值和。

2,离散化是很重要的应用,避免的数据过大而超时的问题,以后的很多线段树题也经常会和离散化一起运用,

离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。(百度上的)

通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小。

思路是:先排序,再删除重复元素,最后就是索引元素离散化后对应的值,(不过还可以排序后运用公式直接找到离散化后得值)。

例如:原数据:1,999,100000,15,先把他们给排下序为1,15,999,100000,离散化就变成了1,2,3,4,

这组数据就可以用1,3,4,2来表示,

再来举个例子,你们推一下试试

原数据:{100,200},{20,50000},{1,400};

处理后:{3,4},{2,6},{1,5};

运用lower_bound函数或是upper_bound函数进行离散化,这两个函数的区别在于

lower_bound是返回大于等于val的第一个值

upper_bound是返回大于val的第一个值

其实我也只会一些简单的用法,在我的代码里会有展现,如果你们不太懂的话可以自己再去百度。

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 10010
struct node
{
    int x,y;
}s[maxn];//区间数据的存储
int tree[8*maxn],n;
int book[maxn];//标记数组
int a[2*maxn];//为了排序而建的
void build(int k,int l,int r)//建树
{
    tree[k]=0;
    if(l==r)
        return ;
    int m=(l+r)/2;
    build(2*k,l,m);
    build(2*k+1,m+1,r);
}
void down(int k)
{
    tree[2*k]=tree[2*k+1]=tree[k];
    tree[k]=0;
}
void updat(int k,int l,int r,int x,int y,int val)//区间更新
{
    if(l>=x&&r<=y)
    {
        tree[k]=val;
        return ;
    }
    if(tree[k]) down(k);
    int m=(l+r)/2;
    if(x<=m) updat(k*2,l,m,x,y,val);
    if(y>m) updat(k*2+1,m+1,r,x,y,val);
}
void query(int k,int l,int r)//区间查询
{
    if(l==r&&tree[k]==0)
        return ;
    if(tree[k])
    {
        book[tree[k]]=1;
        return ;
    }
    int  m=(l+r)/2;
    query(2*k,l,m);
    query(2*k+1,m+1,r);
}
int main()
{
    int i,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(book,0,sizeof(book));
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&s[i].x,&s[i].y);
            a[2*i]=s[i].x;
            a[2*i+1]=s[i].y;
        }
        sort(a,a+2*n);
        for(i=0;i<n;i++)
        {
            s[i].x=lower_bound(a,a+2*n,s[i].x)-a+1;
            s[i].y=lower_bound(a,a+2*n,s[i].y)-a+1;
        }
        build(1,1,2*n);
        for(i=0;i<n;i++)
            updat(1,1,2*n,s[i].x,s[i].y,i+1);
        query(1,1,2*n);
        int sum=0;
        for(i=1;i<=n;i++)
            if(book[i])
            sum++;
        printf("%d\n",sum);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值