线段树- 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 x 2 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

5

0 3 1

1 4 2

2 5 1

0 3 3

1 3 2


Sample Output

1 1
2 1
3 1

1 1

0 2
1 1

1 1

2 1

3 1

解题方案:

采用线段树将线段进行划分,为线段上的每个指定部分着色,最后遍历线段树进行统计。

1)建立线段树

因为线段的最大长度为8000,因此采用静态数组进行存储,静态数组大小为2*8000。(8000+8000/2+..+2+1的等比数列求和,共log8000+1项)。每个节点的数据结构为:

typedef struct

{

   int left,right; //线段树节点对应区间的左端点和右端点

   int lchild,rchild; //节点的左儿子和右儿子在数组中的下标

   int color; //该节点对应区间的颜色

   bool cover;//该节点的颜色是否完全覆盖该节点对应的区间

}treeNode;

 

2)根据输入为线段上的指定部分着色

 每一个节点有一个cover属性,表示该节点颜色是否完全覆盖节点表示的区域。从根开始查找节点表示区域与指定区域重合的节点,然后将该节点的cover属性设置为true,否则将从上往下查找路径中所有经过的节点的cover属性设置为false。如果节点原来的cover属性为true,需要先将该节点的左儿子和右儿子的cover属性设置为true,颜色属性设置为该节点的颜色。假设节点表示区间的左端点为a,右端点为b,区间中点为(a+b/2,如果指定区域的右端点不大于(a+b/2,则选取左子树进行查找;如果指定区域的左端点不小于(a+b/2,则选取右子树进行查找;否则需要同时在两个子树上进行查找。算法表达形式为:

 f(node,left,right,c)

 begin

   m = (node. lchild +node. lchild)/2;

  if(right<=m)

  {

       f (node.lchild,left,right,c);

  }

  else if(left>=m)

  {

      f (node.rchild,left,right,c);

  }

  else

  {

      f (node.lchild,left,m,c);

      f (node.rchild,m,right,c);

  }

end

 

 

3)统计

 从根节点开始遍历线段树。如果节点的cover属性为true,表示该节点对应的区间被该节点的颜色完全覆盖,因此直接对该节点的颜色进行累加;否则,递归对该节点的左子树和右子树进行统计,然后对左右子树区间的交界部分的重复颜色进行合并。方法是:找到左子树中cover属性为true的最靠右的节点,找到右子树中cover属性为true的最靠左的节点。如果两个节点的颜色相同,将该颜色的统计值减一。

 

  代码:

#include <stdio.h>
#include <string.h>

#define MAXNUM 8000

typedef struct
{
 int left,right;
 int lchild,rchild;
 int color;
 bool cover;
}treeNode;


treeNode tarray[MAXNUM*2];
int leafNode[MAXNUM];
int carray[MAXNUM+2];
int colors[MAXNUM+1];
int ls[MAXNUM+1];
int rs[MAXNUM+1];

static int n;

static void buildSegmentTree(int ,int ,int );
static void printColor(int, int ,int ,int );
static void merge(int );

int main(int argc,char **argv)
{
 int i;
 int xmin,xmax,minc,maxc;

 while(scanf("%d", &n) != EOF)
 {
     memset(carray,0,sizeof(carray));
  xmin = MAXNUM;
  xmax = 0;
  minc = MAXNUM+2;
  maxc = 0;

  for(i=0;i<n;i++)
  {
   scanf("%d %d %d",&ls[i],&rs[i],&colors[i]);
   if(xmax<rs[i])
    xmax = rs[i];
   if(ls[i]<xmin)
    xmin = ls[i];
   if(colors[i]+1<minc)
    minc = colors[i]+1;
   if(colors[i]+1>maxc)
    maxc = colors[i]+1;
  }
  buildSegmentTree(0,xmin,xmax);
  for(i=0;i<n;i++)
   printColor(0,ls[i],rs[i],colors[i]);
  merge(0);
  for(i=minc;i<=maxc;i++)
  {
   if(carray[i]>0)
    printf("%d %d/n",i-1,carray[i]);
  }
  printf("/n");
 }
 return 0;
}
static void buildSegmentTree(int cnode,int left,int right)
{
 tarray[cnode].left = left;
 tarray[cnode].right = right;
 tarray[cnode].cover = false;

 if(left+1==right)
 {
  tarray[cnode].lchild = tarray[cnode].rchild = -1;
  leafNode[left] = cnode;
  return;
 }

 int m = (left+right)/2;
 tarray[cnode].lchild = cnode*2+1;
 tarray[cnode].rchild = cnode*2+2;
 buildSegmentTree(tarray[cnode].lchild,left,m);
 buildSegmentTree(tarray[cnode].rchild,m,right);
 
}

static void printColor(int cnode,int left,int right,int c)
{
 if(cnode==-1)
  return;

 bool temp = tarray[cnode].cover;
 tarray[cnode].cover = false;

 int m = (tarray[cnode].left+tarray[cnode].right)/2;

 if(tarray[cnode].left==left&&tarray[cnode].right==right)
 {
  tarray[cnode].color = c+1;
  tarray[cnode].cover = true;
  return;
 }

 if(temp&&tarray[cnode].lchild!=-1)
 {
  tarray[tarray[cnode].lchild].cover = true;
  tarray[tarray[cnode].lchild].color = tarray[cnode].color;
 }

 if(temp&&tarray[cnode].rchild!=-1)
 {
  tarray[tarray[cnode].rchild].cover = true;
  tarray[tarray[cnode].rchild].color = tarray[cnode].color;
 }
  
 if(right<=m)
 {
  printColor(tarray[cnode].lchild,left,right,c);
 }
 else if(left>=m)
 {
  printColor(tarray[cnode].rchild,left,right,c);
 }
 else
 {
  printColor(tarray[cnode].lchild,left,m,c);
  printColor(tarray[cnode].rchild,m,right,c);
 }
}

static void merge(int cnode)
{
 if(cnode==-1)
  return;
 
 if(!tarray[cnode].cover)
 {
  merge(tarray[cnode].lchild);
  merge(tarray[cnode].rchild);
  int l = tarray[cnode].lchild;
  while(l!=-1&&!tarray[l].cover)
   l = tarray[l].rchild;
  int r = tarray[cnode].rchild;
  while(r!=-1&&!tarray[r].cover)
   r = tarray[r].lchild;
  if(l!=-1&&r!=-1&&tarray[l].color==tarray[r].color)
   carray[tarray[l].color]--;
 }
 else
 {
  carray[tarray[cnode].color]++;
 }
}

然后贴一个别人的解法,也是用线段树做的

代码:

/*zju 1610*/
#include <stdio.h>
#include <string.h>
#define MAXN 8010
typedef struct
{ int l, r;
   int lc, rc;
   int color, cover;
} treetype;

int tree_create(int l, int r);
void tree_cover(int cod, int x1, int x2, int c);
void bfirst(int cod);

treetype tree[MAXN*2];
int trlen, precolor, counter[MAXN];

main()
{ int n, minc, maxc, le, re, i;
   int x1[MAXN], x2[MAXN], c[MAXN];
   while (scanf("%d", &n) != EOF)
     { for (minc = le = MAXN+1, maxc = re = -1, i = 0; i < n; i ++)
        { scanf("%d%d%d", &x1[i], &x2[i], &c[i]);
         if (x1[i] < le) le = x1[i];
         if (x2[i] > re) re = x2[i];
         if (c[i] < minc) minc = c[i];
         if (c[i] > maxc) maxc = c[i];
        }
       trlen = 0;
       memset(tree, 0, sizeof(tree));
       tree_create(le, re);
       tree[1].cover = 1;
      for (i = 0; i < n; i ++)
         tree_cover(1, x1[i], x2[i], c[i]);
       memset(counter, 0, sizeof(counter));
       precolor = -1;
       bfirst(1);
      if (precolor != -1) counter[precolor] ++;
      for (i = minc; i <= maxc; i ++)
        if (counter[i]) printf("%d %d/n", i, counter[i]);
       putchar('/n');
     }
   return 0;
}
int tree_create(int l, int r)
{ int curr = ++ trlen;
    tree[curr].l = l;
    tree[curr].r = r;
    tree[curr].color = -1;
   if (r - l > 1)
     { int mid = (l + r) / 2;
       tree[curr].lc = tree_create(l, mid);
       tree[curr].rc = tree_create(mid, r);
     }
   return curr;
}
void tree_cover(int cod, int x1, int x2, int c)
{ if (tree[cod].l == x1 && tree[cod].r == x2)
     { tree[cod].cover = 1;
       tree[cod].color = c;
      return;
     }
   if (tree[cod].cover)
     { if (tree[cod].color == c) return;
       tree[cod].cover = 0;
       tree[tree[cod].lc].cover = 1;
       tree[tree[cod].rc].cover = 1;
       tree[tree[cod].lc].color = tree[cod].color;
       tree[tree[cod].rc].color = tree[cod].color;
     }
   if (x2 <= tree[tree[cod].lc].r)
     { tree_cover(tree[cod].lc, x1, x2, c);
      return;
     }
   if (x1 >= tree[tree[cod].rc].l)
     { tree_cover(tree[cod].rc, x1, x2, c);
      return;
     }
    tree_cover(tree[cod].lc, x1, tree[tree[cod].lc].r, c);
    tree_cover(tree[cod].rc, tree[tree[cod].rc].l, x2, c);
}
void bfirst(int cod)
{ if (tree[cod].cover)
     { if (tree[cod].color == precolor) return;
      if (precolor != -1) counter[precolor] ++;
       precolor = tree[cod].color;
      return;
     }
    bfirst(tree[cod].lc);
    bfirst(tree[cod].rc);
}

刚开始做时,采用的方法是将线段分解为单位长度的小区间,根据输入变换每个小区间的颜色,最后遍历所有区间进行统计.但是事实证明该种方法的运行效率不高. 贴在这以进行比较.

代码:

#include <stdio.h>

#define MAXNUM 8001

int tarray[MAXNUM+1];
int carray[MAXNUM+2];

static int n;

static void printColor(int ,int ,int );
static void merge(int ,int );

int main(int argc,char **argv)
{
 int i,x1,x2,c;
 int xmin,xmax,minc,maxc;

 while(scanf("%d", &n) != EOF)
 {
  for(i=0;i<MAXNUM+2;i++)
  {
   if(i<MAXNUM+1)
    tarray[i] = 0;
   carray[i] = 0;
  }
  xmin = MAXNUM;
  xmax = 0;
  minc = MAXNUM+2;
  maxc = 0;

  for(i=0;i<n;i++)
  {
   scanf("%d %d %d",&x1,&x2,&c);
   printColor(x1,x2,c);
   if(xmax<x2)
    xmax = x2;
   if(x1<xmin)
    xmin = x1;
   if(c+1<minc)
    minc = c+1;
   if(c+1>maxc)
    maxc = c+1;
  }
  merge(xmin,xmax);
  for(i=minc;i<=maxc;i++)
  {
   if(carray[i]>0)
    printf("%d %d/n",i-1,carray[i]);
  }
  printf("/n");
 }
 return 0;
}

static void printColor(int left,int right,int c)
{
 for(int i=left;i<right;i++)
 {
  tarray[i] = c+1;
 }
}

static void merge(int left,int right)
{
/*
 int m;
 if(left+1==right)
 {
  carray[tarray[left]]++;
     return;
 }
 m = (left+right)/2;
 merge(left,m);
 merge(m,right);
 if(tarray[m] == tarray[m-1])
  carray[tarray[m]]--;
*/
 
 for(int i=left;i<right;i++)
 {
  if(i>left&&tarray[i]==tarray[i-1])
   continue;
     carray[tarray[i]]++;
 }

  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值