zoj 1610 Count the Colors

OJ Problem Set - 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 <string.h>
#include <stdlib.h>
#define maxn 8033
struct node
{
  int l,r;
  int col;
}t[4*maxn];
int flag[maxn];
int color[maxn];
void built( int s,int e,int n)
{
 t[n].l=s,t[n].r=e,t[n].col=-2;//注意t[n].col可初始赋值为-2,-3,...
 if(s==e-1) return ;//注意s=e-1;
 int mid=(s+e)>>1;
 built(s,mid,2*n);
 built(mid,e,2*n+1);
}
void modify( int s,int e,int w,int n)
{
  //if(s>t[n].r||e<t[n].l) return ;
  if(s<=t[n].l&&t[n].r<=e)
  {
 t[n].col=w;
 return ;
  }
  if(t[n].l==t[n].r-1) return ;
  if(t[n].col>=0)
  {
 t[2*n].col=t[2*n+1].col=t[n].col;
 t[n].col=-1;
  }
  int mid=(t[n].l+t[n].r)>>1;
  if(e<=mid) modify(s,e,w,2*n);
  else if(s>=mid) modify(s,e,w,2*n+1);
  else
  {
 modify(s,mid,w,2*n);
 modify(mid,e,w,2*n+1);
  }
}
void solve( int s,int e,int n)
{
  if(t[n].col>=0)
  {
 for(int i=t[n].l;i<t[n].r;i++)
  color[i]=t[n].col;
 return ;
  }
  if(t[n].l==t[n].r-1) return ;
  int mid=(t[n].l+t[n].r)>>1;
  if(e<=mid) solve(s,e,2*n);
  else if(s>=mid) solve(s,e,2*n+1);
  else
  {
 solve(s,mid,2*n);solve(mid,e,2*n+1);
  }
}
int main( )
{
 int n,i;
 int x,y,v;
 while(scanf("%d",&n)!=EOF)
 {
  
   built(0,maxn,1);//system("pause");
   memset(flag,0,sizeof(flag));
   memset(color,-1,sizeof(color));
   for( i=1;i<=n;i++)
   {
     scanf("%d%d%d",&x,&y,&v);
     modify(x,y,v,1);
   }
    solve(0,maxn,1);
   
   for( i=0;i<maxn;i++)
   {
  if(color[i]!=color[i+1]&&color[i]!=-1)
  {
  ++flag[color[i]];
  }
   }
   for( i=0;i<maxn;i++)
   {
  if(flag[i])
  {
    printf("%d %d\n",i,flag[i]);
  }
   }
   puts("");
 }
 return 0;
}
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610

 

posted @ 2012-07-26 22:11  jiai  Views(158)  Comments(0Edit  收藏  举报