1. 程式人生 > >zoj 1610 Count the Colors 【區間覆蓋 求染色段】

zoj 1610 Count the Colors 【區間覆蓋 求染色段】

ble article 之前 n) 讓我 family define first main

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



Author: Standlove

Source: ZOJ Monthly, May 2003



題意:在一條長度為8000的線段上染色,每次把區間[a,b]染成c顏色。

顯然,後面染上去的顏色會覆蓋掉之前的顏色。求染完之後,每種顏色在線段上有多少個間斷的區間。



線段樹Lazy區間改動。


讓我無語的是——輸出要求是按顏色編號從0到8000輸出的。

而我傻傻的按插入順序輸出了1個小時。。

。o(╯□╰)o




AC代碼:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 8000+10
using namespace std;
int color[MAXN<<2];//對節點染色
void PushDown(int o)
{
    if(color[o] != -1)
    {
        color[o<<1] = color[o<<1|1] = color[o];
        color[o] = -1;
    }
}
void update(int o, int l, int r, int L, int R, int v)
{
    if(L <= l && R >= r)
    {
        color[o] = v;
        return ;
    }
    PushDown(o);
    int mid = (l + r) >> 1;
    if(L <= mid)
        update(o<<1, l, mid, L, R, v);
    if(R > mid)
        update(o<<1|1, mid+1, r, L, R, v);
}
int rec[MAXN];//存儲i節點的顏色
int top = 0;
void query(int o, int l, int r)
{
    if(l == r)
    {
        rec[top++] = color[o];//記錄節點的顏色
        return ;
    }
    int mid = (l + r) >> 1;
    PushDown(o);
    query(o<<1, l, mid);
    query(o<<1|1, mid+1, r);
}
int num[MAXN];//記錄顏色出現的 段數
int main()
{
    int N;
    while(scanf("%d", &N) != EOF)
    {
        memset(color, -1, sizeof(color));
        int a, b, c;
        for(int i = 1; i <= N; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            update(1, 1, 8000, a+1, b, c);//相應顏色數 先加一
        }
        memset(rec, -1, sizeof(rec));
        top = 0;//初始化
        query(1, 1, 8000);
        memset(num, 0, sizeof(num));//初始化
        int i, j;
        for(i = 0; i < top;)
        {
            if(rec[i] == -1)
            {
                i++;
                continue;
            }
            num[rec[i]]++;
            for(j = i + 1; j < top; j++)
            {
                if(rec[j] != rec[i] || rec[j] == -1)
                    break;
            }
            i = j;
        }
        for(int i = 0; i <= 8000; i++)
        {
            if(num[i])
                printf("%d %d\n", i, num[i]);
        }
        printf("\n");
    }
    return 0;
}


直接模擬也能夠過:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (10000+10)
#define MAXM (50000000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int num[MAXN];
int color[MAXN];
int main()
{
    int n;
    while(Ri(n) != EOF)
    {
        int Max = -INF;
        CLR(color, -1);
        for(int i = 0; i < n; i++)
        {
            int x, y, c;
            Ri(x); Ri(y); Ri(c);
            Max = max(Max, c);
            for(int j = x; j < y; j++)
                color[j] = c;
        }
        CLR(num, 0);
        for(int i = 0; i <= 8000; )
        {
            int temp = color[i];
            if(temp == -1)
            {
                i++;
                continue;
            }
            i++;
            while(temp == color[i])
                i++;
            num[temp]++;
        }
        for(int i = 0; i <= Max; i++)
            if(num[i])
                printf("%d %d\n", i, num[i]);
        printf("\n");
    }
    return 0;
}


zoj 1610 Count the Colors 【區間覆蓋 求染色段】