Count the Colors 数颜色 离散化+线段树

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.

Input

The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:

x1 x2 c

x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.

All the numbers are in the range [0, 8000], and they are all integers.

Input may contain several data set, process to the end of file.

Output

Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.
If some color can’t be seen, you shouldn’t print it.

Print a blank line after every dataset.

Sample Input

5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1

Sample Output

1 1
2 1
3 1
1 1

0 2
1 1

给定n个颜色区间 可能会产生覆盖。让你最后统计有多少种颜色,有多少块。

正确姿势:离散化+线段树

#include <algorithm>  
#include <iostream>  
#include <string.h>  
#include <cstdio>  
#include <set>  
#define lson l, m, rt<<1  
#define rson m+1, r, rt<<1|1  
using namespace std;  
const int maxn = 10005;  
int col[maxn<<4], lazy[maxn<<4];  
int inp[maxn][2], chg[maxn<<2];  
set<int> hash;    //记录访问过与否   
void pushDown(int rt)  
{  
    if(lazy[rt])  
    {  
        lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];  
        col[rt<<1] = col[rt<<1|1] = lazy[rt];  
        lazy[rt] = 0;  
    }  
}  
void build(int l, int r, int rt)  
{  
    lazy[rt] = 0;  
    col[rt] = 0;  
    if(l == r) return;  
    int m = (l+r) >> 1;  
    build(lson);  
    build(rson);  
}  
void update(int k, int L, int R, int l, int r, int rt)  
{  
    if(L <= l && R >= r)  
    {  
        col[rt] = k;  
        lazy[rt] = k;  
        return;  
    }  
    pushDown(rt);  
    int m = (l+r) >> 1;  
    if(L <= m) update(k, L, R, lson);  
    if(R > m) update(k, L, R, rson);   
}  
int query(int l, int r, int rt)  
{  
    if(l == r)   
    {  
        if(col[rt] && !hash.count(col[rt]))  
        {  
            hash.insert(col[rt]);  
            return 1;  
        }  
        return 0;  
    }  
    pushDown(rt);  
    int m = (l+r) >> 1;  
    return query(lson) + query(rson);  
}  
int main()  
{  
    int t, n, l, r, cnt;  
    scanf("%d", &t);  
    for(int i = 1; i <= t; ++i)  
    {  
        hash.clear(); cnt = 0;  
        scanf("%d", &n);  
        for(int j = 1; j <= n; ++j)  
        {  
            scanf("%d %d", &inp[j][0], &inp[j][1]);  
            chg[cnt++] = inp[j][0];  
            chg[cnt++] = inp[j][1];  
        }  
        sort(chg, chg+cnt);  
        cnt = unique(chg, chg+cnt) - chg;  
        int tmp = cnt;  
        for(int j = 1; j < tmp; ++j)  
        {  
            if(chg[j] - chg[j-1] > 1) chg[cnt++] = chg[j-1]+1;  
        }  
        sort(chg, chg+cnt);  
        build(1, cnt, 1);  
        for(int j = 1; j <= n; ++j)  
        {  
            int a = lower_bound(chg, chg+cnt, inp[j][0]) - chg;  
            int b = lower_bound(chg, chg+cnt, inp[j][1]) - chg;  
            ++a, ++b;  
            update(j, a, b, 1, cnt, 1);  
        }  
        printf("%d\n", query(1, cnt, 1));  
    }  
    return 0;  
}  

大小为1~8000 存区间 查询的时候如果区间有laze标记(col),然后再用一个数组存真实区间 vis每次查询的时候对于有标记的区间,填满vis就好,最后统计一下

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 8115;
int col[N<<2],ll[N<<2],rr[N<<2];
int ans[N<<2];
int vis[N<<2];
void pushdown(int rt)
{
    if(col[rt]!=-1)
    {
        col[rt<<1]=col[rt<<1|1]=col[rt];
        col[rt]=-1;
    }
}
void build(int l,int r,int rt){
    ll[rt]=l;
    rr[rt]=r;
    if(l==r)
    {
        col[rt]=-1;
        vis[rt]=-1;
        return ;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
}

void update(int l,int r,int c,int rt)
{
    if(l<=ll[rt]&&r>=rr[rt])
    {
        col[rt]=c;
        return ;
    }
    if(col[rt]==c) return ;
    if(col[rt]!=-1) pushdown(rt);
    int m=(ll[rt]+rr[rt])>>1;
    if(r<=m) update(l,r,c,rt<<1);
    else if(l>m) update(l,r,c,rt<<1|1);
    else 
    {
        update(l,m,c,rt<<1);
        update(m+1,r,c,rt<<1|1);
    }
}
void query(int rt)
{
    if(col[rt]>=0)
    {
        for(int i=ll[rt];i<=rr[rt];i++)
            vis[i]=col[rt];
        return ;
    }
    if(col[rt]==-1&&ll[rt]!=rr[rt])
    {
        query(rt<<1);
        query(rt<<1|1);
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        memset(col,-1,sizeof(col));
        build(1,8010,1);
        for(int i=1;i<=n;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            update(a+1,b,c,1);
        }
        memset(vis,-1,sizeof(vis));
        memset(ans,0,sizeof(ans));
        query(1);
        int index=1;
        while(index<=8010)
        {
            if(vis[index]!=-1)
            {
                int sum=0 ;
                int color=vis[index];
                while(vis[index]!=-1&&vis[index]==color&&index<=8010) index++;
                ans[color]++;
                index--;
            }
            index++;
        }
        for(int i=0;i<=8010;i++)
        {
            if(ans[i])
            {
                printf("%d %d\n",i,ans[i] );
            }
        }
        puts("");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值