ZOJ 1610 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



题意:

多组输入

给一个长度为8000的木板,求n次操作后,能看到哪些颜色,和该种颜色出现次数,连续出现算一次。

第一行 ,一个n表示染色次数

接下来n行每次给a,b,c,表示a点到b点内的区间 染成c色。


思路:

看了下别人的题解,O(n^2)竟然都过了。

这里采用线段树优化一下。

注意给的数据是点,染色是区间,所以可以令左边界+1 或 右边界-1。0+1比8000-1,看着爽我选择前者。

染色过程,详见代码。

最后答案的统计,可以开个last,记录前一个区间的颜色,因为线段树是先访问左儿子所以是从左到右顺序遍历。

每当遇到一个与前一个区间颜色不同的就使该颜色所在区间答案+1(类似桶排)


详见代码,初学线段树,代码较丑请多见谅,反正也没人看 (ಥ_ಥ)  

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define For(a,b,c) for(int a = b; a <= c; a++)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson rt<<1 , l, m
#define rson rt<<1|1, m+1, r
#define ll long long

int N, last;
int col[32005], ans[8005];

void pushdown(int rt)
{
    if(col[rt] == -1) return;
    col[rt<<1] = col[rt];
    col[rt<<1|1] = col[rt];
    col[rt] = -1;
}

void update(int rt, int l, int r, int ql, int qr, int num)
{
    if(ql <= l && r <= qr)
    {
        col[rt] = num;
        return;
    }
    pushdown(rt);
    int m = (l+r)>>1;
    if(ql <= m) update(lson,ql,qr,num);
    if(qr > m) update(rson,ql,qr,num);
}

void Query(int rt, int l, int r, int ql, int qr)
{
    if(l == r)
    {
        if(col[rt] == -1)
        {
            last = col[rt];//这个忘记加了 wa了一发 ORZ
            return;
        }
        if(col[rt] != last) ans[col[rt]]++;
        last = col[rt];
        return;
    }
    pushdown(rt);
    int m = (l+r)>>1;
    if(ql <= m) Query(lson,ql,qr);
    if(qr > m) Query(rson,ql,qr);
}

int main()
{
    int l, r, a;
    while(~scanf("%d",&N))
    {
        mem(col,-1);
        mem(ans,0);
        last = -1;
        For(i,1,N)
        {
            scanf("%d%d%d",&l,&r,&a);
            update(1,1,8000,l+1,r,a);
        }
        Query(1,1,8000,1,8000);
        For(i,0,8000) if(ans[i]) printf("%d %d\n",i,ans[i]);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/j2_o2/article/details/80164168
今日推荐