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

P r o b l e m D e s c r i p t i o n \mathfrak{Problem Description} ProblemDescription

The citizens of Bytetown, A B AB 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:

∙ \bullet Every candidate can place exactly one poster on the wall.

∙ \bullet 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).

∙ \bullet The wall is divided into segments and the width of each segment is one byte.

∙ \bullet Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 10000000 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.

I n p u t \mathfrak{Input} 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 &lt; = n &lt; = 10000 1 &lt;= n &lt;= 10000 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 i i and r i ri ri which are the number of the wall segment occupied by the left end and the right end of the i − t h i-th ith poster, respectively. We know that for each 1 &lt; = i &lt; = n , 1 &lt; = l i &lt; = r i &lt; = 10000000 1 &lt;= i &lt;= n, 1 &lt;= l i &lt;= ri &lt;= 10000000 1<=i<=n,1<=li<=ri<=10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i , l i + 1 , . . . , r i l i, l i+1 ,... , ri li,li+1,...,ri.

O u t p u t \mathfrak{Output} 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. 在这里插入图片描述

S a m p l e I n p u t \mathfrak{Sample Input} SampleInput

1
5
1 4
2 6
8 10
3 4
7 10

S a m p l e O u t p u t \mathfrak{Sample Output} SampleOutput

4 {\color{Red} 4} 4

题 目 大 意 {\color{Magenta}题目大意} :给出T组数据有每组数据有N张海报以此先后给出海报所占墙面的区间范围A → \rightarrow B;问按这种先后顺序贴上海报最终能看见几张海报;
思 路 {\color{Magenta}思路} :不妨这样想:我们把面前的海报一张一张的撕下(贴着墙面的那种撕哦)也就是不管这张海报下面还有没有其他海报,就把这部分区间(指的是你面前的这张海报的区间)上的海报全部撕掉,最终看看能撕几次,也就是我们能看到的海报数量了(可以比着题中的图自己动手试试看);线段树,由后向前更新。 u n i q u e unique unique函数:去重,但是重复的数并没有去掉,而是放在了数组的后面;用 0 , 1 0,1 01记录墙上是否含有海报;把输入的海报区间标记为1,其他区间标记为0,撕掉一次把撕掉的区间标记为0;之后经行判断撕掉几次即可;
温 馨 提 示 {\color{Magenta}温馨提示} :由于需开空间特大,不能直接进行,由此需要你用一个小技巧下面来介绍:我们这有一张表看看

ABCDEFGHIJK
124768117218219323425537761909
1234567891011

从表中看 A → K A\rightarrow K AK的数目大小虽然改变了但是他们的大小顺序还是没变,就用这种方法我们把题中给的区间缩短但是顺序不给它改变,就轻易的得到小的模拟序列,这样我们的数据就剪短了许多,具体操作,看代码:

#include<cstdio>
#include<bitset>
#include<cstring>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define ll long long
const int N=1e6+10;
bitset<N<<2>bit;
int book[100000100];
int sum[N<<4],a[N],b[N],z[N<<1],add[N<<2],w[100000010];
void build_tree(int l,int r,int o)
{
    if(l==r)
    {
        sum[o]=1;
        return ;
    }
    int mid=(l+r)>>1;
    build_tree(l,mid,o<<1);
    build_tree(mid+1,r,o<<1|1);
    sum[o]=sum[o<<1]+sum[o<<1|1];
}
void pushdown(int o,int l)
{
    if(add[o]!=-1)
    {
        add[o<<1]=add[o];
        add[o<<1|1]=add[o];
        sum[o<<1]=add[o]*(l-(l>>1));
        sum[o<<1|1]=add[o]*(l>>1);
        add[o]=-1;
    }
}
void update(int s,int L,int R,int l,int r,int c)
{
    if(l<=L&&r>=R)
    {
        sum[s]=c*(R-L+1);
        add[s]=c;
        return ;
    }
    pushdown(s,R-L+1);
    int mid=(L+R)>>1;
    if(l<=mid)update(s<<1,L,mid,l,r,c);
    if(r>mid)update(s<<1|1,mid+1,R,l,r,c);
    sum[s]=sum[s<<1]+sum[s<<1|1];
}
ll query(int s,int L,int R,int l,int r)
{
    if(L>=l&&R<=r)
        return sum[s];
    pushdown(s,R-L+1);
    int mid=(L+R)>>1;
    ll Sum=0;
    if(l<=mid) Sum+=query(s<<1,L,mid,l,r);
    if(r>mid) Sum+=query(s<<1|1,mid+1,R,l,r);
    return  Sum;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        memset(add,-1,sizeof(add));
        scanf("%d",&n);
        int k=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&a[i],&b[i]);
            book[k++]=a[i];
            book[k++]=b[i];
        }
        sort(book,book+k);
        int l=unique(book,book+k)-book;
        for(int i=0; i<l; i++)
            w[book[i]]=i+1;
        build_tree(1,l,1);
        int ans=0;
        for(int i=n-1; i>=0; i--)
            if(query(1,1,l,w[a[i]],w[b[i]]))
            {
                update(1,1,l,w[a[i]],w[b[i]],0);
                ans++;
            }
        printf("%d\n",ans);
    }
    return 0;
}

实践是检验真理的唯一标准

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值