码迷,mamicode.com
首页 > 其他好文 > 详细

Count the Colors (zoj 1610 线段树 区间颜色覆盖)

时间:2015-02-17 15:20:18      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:count the colors   zoj 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



Author: Standlove
Source: ZOJ Monthly, May 2003
Submit    Status


题意:在一条线段上画颜色,画n次,每次使x1到 x2区间颜色变为 c。求表面上能看到的颜色种类和该颜色的段数。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 8005
#define MAXN 40005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct tree
{
    int l,r,color;
}tree[maxn<<2],TREE[maxn<<2];

int n,num;
int ans[maxn];

void pushdown(int rt)
{
    if (tree[rt].color>-1)
    {
        tree[rt<<1].color=tree[rt].color;
        tree[rt<<1|1].color=tree[rt].color;
        tree[rt].color=-1;
    }
}

void build(int rt,int l,int r)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].color=-1;
    if (l==r)return ;
    int mid=(tree[rt].l+tree[rt].r)>>1;
    build(lson);
    build(rson);
}

void update(int rt,int l,int r,int color)
{
    if (l<=tree[rt].l&&tree[rt].r<=r)
    {
        tree[rt].color=color;
        return ;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(mid>=r) update(rt<<1,l,r,color);
    else if (mid<l) update(rt<<1|1,l,r,color);
    else{
        update(lson,color);
        update(rson,color);
    }
}

void Count(int rt,int l,int r)
{
    if (l==tree[rt].l&&tree[rt].r==r&&tree[rt].color>-1)
    {
        TREE[num].l=l;
        TREE[num].r=r;
        TREE[num++].color=tree[rt].color;
        return ;
    }
    if (l==r) return ;
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if (mid>=r) Count(rt<<1,l,r);
    else if(mid<l) Count(rt<<1|1,l,r);
    else{
        Count(lson);
        Count(rson);
    }
}

int main()
{
    int i,j;
    int l,r,color;
    while (~sf(n))
    {
        num=0;
        build(1,1,maxn);  //注意是maxn,不是n!!!
        FRL(i,0,n)
        {
            sfff(l,r,color);
            update(1,l+1,r,color);
        }
        Count(1,1,maxn);  //注意是maxn,不是n!!!
        mem(ans,0);
        ans[TREE[0].color]++;
        FRL(i,1,num)
        {
            //前后两个线段颜色不一样或者不相邻
            if (TREE[i].color!=TREE[i-1].color||TREE[i].l-1>TREE[i-1].r)
                ans[TREE[i].color]++;
        }
        FRL(i,0,maxn)
        {
            if (ans[i])
                pf("%d %d\n",i,ans[i]);
        }
        pf("\n");
    }
    return 0;
}
/*
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
*/



Count the Colors (zoj 1610 线段树 区间颜色覆盖)

标签:count the colors   zoj 1610   线段树区间覆盖   

原文地址:http://blog.csdn.net/u014422052/article/details/43866347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!