链接:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/F

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610

 

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

 

一个涂色问题,我看到了好几种不同的解法, 感觉太神奇,太奇妙了,先附上自己的代码,再学习一下别人的,写了好几题了,不能追速度,我需要好好琢磨琢磨

 

代码:

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

#define Lson r<<1
#define Rson r<<1|1

const int N = 8500;

struct Node
{
    int l, r, c;
} s[N<<2];

struct node
{
    int L, R;
    int color;
    int Mid()
    {
        return (L+R)>>1;
    }
} a[N<<2];

int Color[N];

void CoverColor(int L, int R, int e)
{
    for(int i=L; i<=R; i++)
        Color[i]=e;
}

void UpDate(int r)
{
    if(a[r].L != a[r].R)
    if(a[Lson].color==1 && a[Rson].color==1)
        a[r].color = 1;
}

void BuildTree(int r, int L, int R)
{
    a[r].L = L, a[r].R = R;
    a[r].color = 0; // 0代表没有颜色覆盖, 1代表未覆盖, 2代表子树有被别的颜色覆盖

    if(L==R) return ;

    BuildTree(Lson, L, a[r].Mid());
    BuildTree(Rson, a[r].Mid()+1, R);
}

void Insert(int r, int L, int R, int e)
{
    if(a[r].color == 1) return ;

    if(a[r].L==L && a[r].R==R && !a[r].color)
    {
        a[r].color=1;
        CoverColor(L, R, e);
        return ;
    }

    a[r].color = 2;

    if(R<=a[r].Mid())
        Insert(Lson, L, R, e);
    else if(L>a[r].Mid())
        Insert(Rson, L, R, e);
    else
    {
        Insert(Lson, L, a[r].Mid(), e);
        Insert(Rson, a[r].Mid()+1, R, e);
    }

    UpDate(r);
}

int main()
{
    int n;

    while(scanf("%d", &n)!=EOF)
    {
        int i, ans[N]={0};

        memset(s, 0, sizeof(s));
        for(i=1; i<=n; i++)
            scanf("%d%d%d", &s[i].l, &s[i].r, &s[i].c);

        BuildTree(1, 0, N-1);
        memset(Color, -1, sizeof(Color));

        for(i=n; i>0; i--)
            Insert(1, s[i].l+1, s[i].r, s[i].c);


        for(i=0; i<N; i++)
        {
            if(Color[i]!=-1 && (!i || Color[i]!=Color[i-1]))
                ans[Color[i]]++;
        }

        for(i=0; i<N; i++)
        if(ans[i])  printf("%d %d\n", i, ans[i]);

        printf("\n");
    }
    return 0;
}

队友暴力过的代码, 没用线段树

 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define N 8006
int cnt[N],a[N];
int main()
{
    int n, p, q, c;
    while(scanf("%d", &n) != EOF)
    {
        int Max = 0;
        memset(a, 0, sizeof(a));
        memset(cnt, 0, sizeof(cnt));
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d", &p, &q, &c);
            for(int j=p; j<q; j++)
                a[j] = c+1;
            Max = max(Max, q);
        }
        for(int i=0; i<Max; i++)
        {
            while(i!=0 && a[i]!=0 && a[i]==a[i-1])
                i++;
            if(a[i] != 0)
                cnt[ a[i]-1 ]++;
        }
        for(int i=0; i<N; i++)
        {
            if(cnt[i]!=0)
                printf("%d %d\n", i, cnt[i]);
        }
        printf("\n");
    }
    return 0;
}
View Code

 

posted on 2015-07-31 16:52  栀蓝  阅读(198)  评论(0编辑  收藏  举报

levels of contents