ZOJ - 1610 F - Count the Colors 线段染色 间断计数

Count the Colors

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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条线段,线段的概念是[0,4]表示 0-1 , 1-2, 2-3, 3-4  共4个单位长度

将这n条线段按照输入顺序染色,问每个颜色在整个线段上有多少个间断的线段.


  题解:

由于是线段的更新,我们可以转化成区间,比如[0,4]上有5个点,4个单位线段.

将单位线段从1开始排好序,于是[0,4]就可以用 [1,4]的区间来表示.

根据区间染色的思路,用color[]数组表示区间的颜色. 由于0也是颜色的一种,所以我们用-1来表示没有颜色或者有很多颜色.

那么怎么判断每个颜色间断区间个数呢.

我们知道线段树的遍历顺序是先序遍历.于是可以知道的前后两个遍历到的区间一定是相连续的.

就可以暴力标记下前一个区间的颜色和当前比较,如果不一样的话是另一个颜色的间断区间,统计即可.


代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e4+10;
int le[maxn],ri[maxn],co[maxn];
struct SegTree
{
    ll col[maxn<<2];
    int cnt[maxn];
    int temp;
    bool vis[maxn];
    void init() {
        memset(col,-1,sizeof(col));
        memset(cnt,0,sizeof(cnt));
        temp = -1;
    }
    void push_down(int rt) {
        if(col[rt] == -1) return;
        col[rt<<1] = col[rt<<1|1] = col[rt];
        col[rt] = -1;
    }
    void update(int ql,int qr,int val,int l,int r,int rt) {
        if(ql == l && qr == r) {
            col[rt] = val;
            return;
        }
        push_down(rt);
        int mid = (l+r) >> 1;
        if(qr <= mid) update(ql,qr,val,l,mid,rt<<1);
        else if(ql > mid) update(ql,qr,val,mid+1,r,rt<<1|1);
        else {
            update(ql,mid,val,l,mid,rt<<1);
            update(mid+1,qr,val,mid+1,r,rt<<1|1);
        }
    }
    void query(int l,int r,int rt) {
        if(col[rt] != -1) {
            if(col[rt] != temp) 
                cnt[col[rt]]++;
            temp = col[rt];
            return;
        }
        if(l == r) {
            temp = -1;
            return;
        }
        int mid = (l + r) >> 1;
        query(l,mid,rt<<1);
        query(mid+1,r,rt<<1|1);
    }
}seg;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        seg.init();
        int l,r,c,ma = 0;
        for(int i=0;i<n;i++) { 
            scanf("%d%d%d",&le[i],&ri[i],&co[i]);
            ma = max(ri[i],ma);
        }
        for(int i=0;i<n;i++) {
            if(le[i] == ri[i]) continue;
            seg.update(le[i]+1,ri[i],co[i],1,ma,1);
        }
        seg.query(1,ma,1);
        for(int i=0;i<maxn;i++) if(seg.cnt[i]) 
            printf("%d %d\n",i,seg.cnt[i]);
        printf("\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值