zoj 1610 Count the Colors

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610

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


有一块很长的画布,现在想在这块画布上画一些颜色,不过后面画的颜色会把前面画的颜色覆盖掉,现在想知道画完后这块画布的颜色分布,比如 1号颜色有几块,2号颜色有几块。。。。
***********************************************************************
分析:基本上跟帖海报是一样的,不过最后要求输出的是这种颜色的画有几块,可以按照贴海报的方式先做出来,然后对每个点进行查询,不晓得复杂度会不会太高。不过还是先试一下吧。
注意:如果出现 
Segmentation Fault
可以看看是不是树建的小了,不能拿N建树
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>

using namespace std;

#define N 80005
#define INF 0xfffffff
#define PI acos (-1.0)
#define EPS 1e-8
#define Lson rt<<1, l, tree[rt].mid ()
#define Rson rt<<1|1, tree[rt].mid ()+1, r

struct node1 {int l, r, e;}post[N];
struct node2
{
    int l, r, color;
    int mid (){return (l + r) / 2;}
}tree[N * 4];

int color[N], ans[N];

void build (int rt, int l, int r);
void Insert (int rt, int l, int r, int e);
void update (int rt);

int main ()
{
    int n;
    while (~scanf ("%d", &n))
    {
        for (int i=1; i<=n; i++)
            scanf ("%d%d%d", &post[i].l, &post[i].r, &post[i].e);
        build (1, 0, N-1);

        memset (color, -1, sizeof (color));
        memset (ans, 0, sizeof (ans));

        for (int i=n; i>0; i--)
            Insert (1, post[i].l+1, post[i].r, post[i].e);

        for (int i=0; i<N; i++)
            if (color[i] != -1 && (!i || color[i] != color[i-1]))
                ans[color[i]]++;

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

void build (int rt, int l, int r)
{
    tree[rt].l = l, tree[rt].r = r;
    tree[rt].color = 0;

    if (l == r) return;

    build (Lson), build (Rson);
}

void Insert (int rt, int l, int r, int e)
{
    if (tree[rt].color == 1) return;

    if (!tree[rt].color && tree[rt].l == l && tree[rt].r == r)
    {
        tree[rt].color = 1;
        for (int i=l; i<=r; i++)
            color[i] = e;
        return;
    }
    else tree[rt].color = 2;

    if (r <= tree[rt].mid ()) Insert (rt<<1, l, r, e);
    else if (l > tree[rt].mid ()) Insert (rt<<1|1, l, r, e);
    else Insert (Lson, e), Insert (Rson, e);

    update (rt);
}

void update (int rt)
{
    if (tree[rt].l != tree[rt].r)
        if (tree[rt<<1].color == 1 && tree[rt<<1|1].color == 1)
            tree[rt].color = 1;
}



你可能感兴趣的:(zoj 1610 Count the Colors)