ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)

来源:互联网 发布:网络推广工作好不好干 编辑:程序博客网 时间:2024/04/27 19:38

Count the Colors

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]染成5,然后把[3,4]染成5,那么此时其实是有两段颜色为5的,为什么?因为这是成段染色啊(注意代码更新时的区间),。。。

接着查询时,只需要在查询线段树的过程中把每一个位置上的颜色先记录下来,然后再对总区间进行计数就好了(具体详见代码)

代码:

#include<stdio.h>#include<string.h>#define mem(a,b) memset(a,b,sizeof(a))#define maxn 8010const int N=8000;int record[maxn<<2],color[maxn<<2],vis[maxn<<2];void Pushdown(int rt){    color[rt<<1]=color[rt<<1|1]=color[rt];    color[rt]=-1;}void Updata(int rt,int le,int ri,int co,int Left,int Right){    if(Left<=le&&Right>=ri)    {        color[rt]=co;        return ;    }    if(color[rt]==co) return ;    if(color[rt]!=-1) Pushdown(rt);    int mid=(le+ri)>>1;    if(Left<=mid)        Updata(rt<<1,le,mid,co,Left,Right);    if(Right>mid)        Updata(rt<<1|1,mid+1,ri,co,Left,Right);}void Query(int rt,int le,int ri){    if(color[rt]!=-1)    {        for(int i=le; i<=ri; ++i)//记录每一个位置上的颜色            vis[i]=color[rt];        return ;    }    if(le==ri)        return ;    int mid=(le+ri)>>1;    Query(rt<<1,le,mid);    Query(rt<<1|1,mid+1,ri);}int main(){    int n,x,left,right;    while(~scanf("%d",&n))    {        mem(record,0);//记录每种颜色出现的次数        mem(color,-1);//-1表示没有颜色        mem(vis,-1);//记录每个位置上的颜色        int maxx=-1;        for(int i=1; i<=n; ++i)        {            scanf("%d%d%d",&left,&right,&x);            Updata(1,1,N,x,left+1,right);            if(x>maxx) maxx=x;        }        Query(1,1,N);        int tmp=-1;        for(int i=1; i<=N; ++i)        {            if(vis[i]!=tmp)            {                if(vis[i]!=-1)                    ++record[vis[i]];                tmp=vis[i];            }        }        for(int i=0; i<=maxx; ++i)        {            if(record[i])                printf("%d %d\n",i,record[i]);        }        printf("\n");    }    return 0;}



总结:首先做这道题时连题意都没有读明白,对成段染色和对点染色的差别没有了解清楚,结果弄了半天样例都没过。。。

然后是对于记录成段的颜色没有方法,并没有想到可以先记录下每个点的颜色接着再查询,,,归根结底还是对线段树的递归过程不了解

好吧,起码有所收获,现在紧需多多扩宽思维啊


噗,一周后在比赛场上重新写这道题又被坑了。。。
一是坑在了所有的数的范围都在8000以内,也就是每次树都要建到8000;
二是在遍历有多少个颜色的段的时候,对没有染色的段并没有改变last。。;

还有,最坑的是xjb直接暴力的竟然能过?。。。!


第三次写,再贴一个

代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define ll rt<<1#define rr rt<<1|1#define lson ll,le,mid#define rson rr,mid+1,riconst int maxn=1e4;const int N=8000;int color[maxn<<2],lazy[maxn<<2],a[maxn];int n,last;void Pushdown(int rt){    color[ll]=lazy[rt];    color[rr]=lazy[rt];    lazy[ll]=lazy[rt];    lazy[rr]=lazy[rt];    lazy[rt]=-1;}void Update(int rt,int le,int ri,int l,int r,int val){    if(l<=le&&ri<=r)    {        color[rt]=val;        lazy[rt]=val;        return ;    }    if(lazy[rt]!=-1)        Pushdown(rt);    int mid=(le+ri)>>1;    if(l<=mid)        Update(lson,l,r,val);    if(r>mid)        Update(rson,l,r,val);}void Query(int rt,int le,int ri){    if(lazy[rt]!=-1)        Pushdown(rt);    if(le==ri)    {        if(color[rt]!=last)            ++a[color[rt]],last=color[rt];        return ;    }    int mid=(le+ri)>>1;    Query(lson);    Query(rson);}int main(){    while(~scanf("%d",&n))    {        memset(a,0,sizeof(a));        memset(color,-1,sizeof(color));        memset(lazy,-1,sizeof(lazy));        int le,ri,val;        for(int i=1; i<=n; ++i)        {            scanf("%d%d%d",&le,&ri,&val);            Update(1,0,N,le,ri-1,val);        }        last=-1;        Query(1,0,N);        for(int i=0; i<=N; ++i)            if(a[i])                printf("%d %d\n",i,a[i]);        printf("\n");    }    return 0;}
1 0
原创粉丝点击