首页 > 代码库 > ZOJ1610_Count the Colors(线段树/成段更新)

ZOJ1610_Count the Colors(线段树/成段更新)

解题报告

题意:

一根长度8000的线段上染色,求染完之后,每个颜色在线段上有多少个间断的区间。

思路:

区间问题用线段树,成段的更新区间,最后把所有的区间下压到叶子结点,统计叶子结点的颜色。

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;
int lz[32000],_hash[10000],color[10000],cnt;
void push_down(int rt)
{
    if(lz[rt])
    {
        lz[rt*2]=lz[rt*2+1]=lz[rt];
        lz[rt]=0;
    }
}
void update(int rt,int l,int r,int ql,int qr,int v)
{
    if(ql>r||qr<l)return ;
    if(ql<=l&&r<=qr)
    {
        lz[rt]=v;
        return ;
    }
    push_down(rt);
    int mid=(l+r)/2;
    update(rt*2,l,mid,ql,qr,v);
    update(rt*2+1,mid+1,r,ql,qr,v);
}
void bin(int rt,int l,int r)
{
    if(l==r)
    {
        color[cnt++]=lz[rt];
        return ;
    }
    push_down(rt);
    bin(rt*2,l,(l+r)/2);
    bin(rt*2+1,(l+r)/2+1,r);
}
int main()
{
    int n,m,i,j,ql,qr,a;
    while(~scanf("%d",&n))
    {
        cnt=0;
        memset(lz,0,sizeof(lz));
        memset(_hash,0,sizeof(_hash));
        m=8000;
        int cmax=-1;
        for(i=0; i<n; i++)
        {
            scanf("%d%d%d",&ql,&qr,&a);
            update(1,1,m,ql+1,qr,a+1);
        }
        bin(1,1,m);
        for(i=0; i<cnt;)
        {
            j=i+1;
            _hash[color[i]]++;
            while(color[j]==color[i]&&j<cnt)
                j++;
            i=j;
        }
        for(i=1; i<=m+1; i++)
        {
            if(_hash[i])
                printf("%d %d\n",i-1,_hash[i]);
        }
        printf("\n");
    }
    return 0;
}
附手动随机数据

input:
10
2 4 6 
4 6 2
1 9 3
4 6 2
1 20 3
2 4 3 
6 7 1
3 7 9
4 6 9
2 6 4
10
43 54 8000
323 4342 123
234 2332 321
2 6 23
54 546 1
2843 8888 8000
3000 8000 0
23 4329 9
923 2323 8
2390 3293 1
10
1 34 8000 
43 343 99
341 3414 8000
7999 8000 8000
344 345 1
434 3455 0
34 45 8000
43 56 45
56 64 0
898 4599 8000

output:
3 2
4 1
9 1

0 1
1 1
8 1
9 3
23 1

0 2
1 1
45 1
99 1
8000 5

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