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

 C++版本一

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

const int N = 20000+5;
int tree[N<<2], lazy[N<<2];
int a[N];
bool vis[N];
int ans, n, t;
pair<int,int> P[N];

void Pushdown(int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1|1] = lazy[rt<<1] = lazy[rt];
        lazy[rt] = 0;
    }
}
void Revise(int L, int R, int  C ,int l, int r, int rt)
{
    if(L <= l && r <= R)
    {
        lazy[rt] = C;
        return ;
    }
    Pushdown(rt);
    int m = l+r >> 1;
    if(L <= m) Revise(L,R,C,l,m,rt<<1);
    if(m < R)  Revise(L,R,C,m+1,r,rt<<1|1);
}
void build(int l, int r, int rt)
{
    if(lazy[rt])
    {
        if(!vis[lazy[rt]])
        {
            vis[lazy[rt]] = 1;
            ans++;
        }
        return ;
    }
    int m = l+r >> 1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
}
int main()
{

    cin >> t;
    while(t--)
    {
        ans = 0;
        cin >> n;
        for(int i = 1; i <= 2*n; i++)
        {
            cin >> a[i];
            P[i].first = a[i], P[i].second = i;
        }
        sort(P+1,P+2*n+1);
        int last = 0, cnt = 0;
        for(int i = 1; i <= 2*n; i++) //离散化操作
        {
            if(P[i].first == last)
                a[P[i].second] = cnt;
            else a[P[i].second] = ++cnt, last = P[i].first;
        }
        memset(lazy, 0, sizeof(lazy));
        for(int i = 1; i <= 2*n; i+=2)
            Revise(a[i],a[i+1],(i+1)/2,1,cnt,1);//由于每次的海报不同所以直接给海报一个编号
        memset(vis, 0, sizeof(vis));
        build(1,cnt,1);
        cout << ans << endl;
    }
    return 0;
}

C++版本二 

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
const int N = 20000+10;
using namespace std;
int c,n,q;
int lazy[N<<2];
int a[10010];
int ans=0;
int x[N];
pair<int,int> P[N];

void PushDown(int rt){
    if(lazy[rt]){
        lazy[rt*2] = lazy[rt];
        lazy[rt*2+1] = lazy[rt];
        lazy[rt] = 0;
    }
}
void Build(int l,int r,int rt){
    if(l==r){
        a[lazy[rt]]=1;
        return ;
    }
    int m=(l+r)/2;
    PushDown(rt);
    Build(l,m,rt*2);//左边有一部分需要查询的区域。
    Build(m+1,r,rt*2+1);//右边有一部分。

}

void build(int l, int r, int rt)
{
    if(lazy[rt])
    {
        if(!a[lazy[rt]])
        {
            a[lazy[rt]] = 1;
            ans++;
        }
        return ;
    }
    int m = l+r >> 1;
    build(l,m,rt*2);//左边有一部分需要查询的区域。
    build(m+1,r,rt*2+1);//右边有一部分。
}

void Updata(int l,int r,int rt,int L,int R,int C){// l,r,rt 与前面的定义一样, L代表的是要更新区间的位置,C代表的是修改后的值
    if(L <= l && r <= R){// 这里不能写成 if(l == L) 因为有可能左端点恰好是要更新的位置, 但是还有右端点, 我们直接更新的只有区间 [L,L]。
        lazy[rt] = C;

        return ;
    }
    int m=(l+r)/2;
    PushDown(rt);
    if(L<=m) Updata(l,m,rt*2,L,R,C);//要更新的区间在左边部分,所以往左边走,更新左边
    if(m < R) Updata(m+1,r,rt*2+1,L,R,C);//要更新的区间在右边部分, 往右边走,更新右边
}
void init(){
    memset(lazy,0,sizeof(lazy));
    memset(a,0,sizeof(a));
    memset(x,0,sizeof(x));
}
int main()
{
    scanf("%d",&c);
    for(int j = 1; j <= c; j++){
        init();
        scanf("%d",&n);
        for(int i = 1; i <= 2*n; i++){
            scanf("%d",&x[i]);
            P[i].first = x[i], P[i].second = i;

        }
        sort(P+1,P+2*n+1);
        int last = 0, cnt = 0;
        for(int i = 1; i <= 2*n; i++){ //离散化操作

                if(P[i].first == last)
                    x[P[i].second] = cnt;
                else
                    x[P[i].second] = ++cnt, last = P[i].first;
        }
        for(int i = 1; i <= 2*n; i+=2)
            Updata(1,cnt,1,x[i],x[i+1],(i+1)/2);//由于每次的海报不同所以直接给海报一个编号


        ans=0;
        build(1,cnt,1);
        cout << ans << endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本三

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 10005;
#define dd puts("haha");
int li[N],ri[N];
int x[N<<3];
int col[N<<4];
bool Hash[N];
void PushDown(int rt){
    col[rt<<1] = col[rt];
    col[rt<<1|1] = col[rt];
    col[rt] = -1;
    return;
}
void Update(int L, int R, int l, int r, int c, int rt)
{
    if( L <= l && R >= r ) {
        col[rt] = c;
        return;
    }
    if(col[rt]!=-1) PushDown(rt);
    int m = (l+r)>>1;
    if(L <= m ) Update(L,R,l,m,c,rt<<1);
    if(R > m) Update(L,R,m+1,r,c,rt<<1|1);
}
int ans;
void query(int l, int r, int rt)
{
    if(l==r||~col[rt]){//~col[rt]相当于col[rt]!=-1;因为~是按位取反的意思
        if(!Hash[col[rt]]&&(col[rt]!=-1)){
            ans++;
            Hash[col[rt]] = 1;
        }
        return;
    }
    if(~col[rt])PushDown(rt);
    int m = (l+r)>>1;
    query(l,m,rt<<1);
    query(m+1,r,rt<<1|1);
}
int BSearch(int l, int r, int c)
{
    int m;
    while(l<=r){
        m =(l+r)>>1;
        if(x[m]==c) return m;
        else if(x[m]<c) l = m+1;
        else if(x[m]>c) r = m-1;
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(col,-1,sizeof(col));
        memset(Hash,0,sizeof(Hash));
        int n;
        scanf("%d",&n);
        int cnt = 1;
        for(int i = 1; i <=n; i++)
        {
            scanf("%d %d",&li[i],&ri[i]);
            x[cnt++] = li[i];
            x[cnt++] = ri[i];
            x[cnt++] = ri[i]+1;
        }
        sort(x+1,x+cnt+1);
        int m = unique(x+1,x+cnt+1)-x-1;
        for(int i = 1; i <= n; i++){
            int ll = BSearch(1,m,li[i]);
            int rr = BSearch(1,m,ri[i]);
            //printf("(%d %d)\n",ll,rr);
            Update(ll,rr,1,m,i,1);
        }
        //dd;
        ans = 0;
        query(1,m,1);
        printf("%d\n",ans);
    }
    return 0;
}

 

全部评论

相关推荐

11-27 17:08
已编辑
牛客_产品运营部_私域运营
腾讯 普通offer 24k~26k * 15,年包在36w~39w左右。
点赞 评论 收藏
分享
11-24 19:04
已编辑
湖南工商大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
正在热议
# 25届秋招总结 #
440737次浏览 4493人参与
# 春招别灰心,我们一人来一句鼓励 #
41537次浏览 524人参与
# 阿里云管培生offer #
119909次浏览 2219人参与
# 地方国企笔面经互助 #
7930次浏览 18人参与
# 同bg的你秋招战况如何? #
75684次浏览 552人参与
# 虾皮求职进展汇总 #
114355次浏览 884人参与
# 北方华创开奖 #
107320次浏览 599人参与
# 实习,投递多份简历没人回复怎么办 #
2454094次浏览 34848人参与
# 实习必须要去大厂吗? #
55687次浏览 960人参与
# 提前批简历挂麻了怎么办 #
149836次浏览 1977人参与
# 投递实习岗位前的准备 #
1195754次浏览 18547人参与
# 你投递的公司有几家约面了? #
33181次浏览 188人参与
# 双非本科求职如何逆袭 #
661934次浏览 7394人参与
# 如果公司给你放一天假,你会怎么度过? #
4734次浏览 55人参与
# 机械人春招想让哪家公司来捞你? #
157604次浏览 2267人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11381次浏览 271人参与
# 发工资后,你做的第一件事是什么 #
12431次浏览 61人参与
# 工作中,努力重要还是选择重要? #
35621次浏览 384人参与
# 参加完秋招的机械人,还参加春招吗? #
20091次浏览 240人参与
# 我的上岸简历长这样 #
451933次浏览 8088人参与
# 实习想申请秋招offer,能不能argue薪资 #
39241次浏览 314人参与
# 非技术岗是怎么找实习的 #
155852次浏览 2120人参与
牛客网
牛客企业服务