Count the Colors(两种)

本文深入探讨了一种使用线段树或数组实现的算法,用于计算在一系列彩色线条段中可见的不同颜色段的数量。通过输入指定数量的线条段及其颜色属性,算法能够高效地更新并最终输出每个可见颜色及其对应的段数。

Description

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.线段树,更新查找:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=8100;
int color[maxn],su[maxn];
struct node
{
  int l,r,cover;       
}tree[4*maxn];
void build(int u,int l,int r)
{
  tree[u].l=l;
  tree[u].r=r;
  tree[u].cover=-1;  
  if(l==r) return ;
  int mid=(l+r)/2;
  build(u<<1,l,mid);
  build((u<<1)+1,mid+1,r);
}
void get_down(int u)
{
  tree[u<<1].cover=tree[u].cover;
  tree[(u<<1)+1].cover=tree[u].cover;
  tree[u].cover=-1;     
}
void update(int u,int l,int r,int co)
{
  if(tree[u].cover==co) return ;
  if(l<=tree[u].l&&tree[u].r<=r)
  {
    tree[u].cover=co;
    return ;                              
  }
  if(tree[u].cover!=-1) 
      get_down(u);
  int mid=(tree[u].l+tree[u].r)>>1;
  if(r<=mid) update(u<<1,l,r,co);
  else if(l>=mid+1) update((u<<1)+1,l,r,co);
  else 
    {
      update(u<<1,l,mid,co);
      update((u<<1)+1,mid+1,r,co);             
    }
    if((tree[u<<1].cover==tree[(u<<1)+1].cover)&&(tree[u<<1].cover!=-1))
      tree[u].cover=tree[u<<1].cover;
}
void qerty(int u)
{
  if(tree[u].cover!=-1)
  {
    for(int i=tree[u].l;i<=tree[u].r;i++)
        { color[i]=tree[u].cover;
        } 
    return ;                
  }  
  if(tree[u].l==tree[u].r) return ; 
  qerty(u<<1);
  qerty((u<<1)+1);
}
int main()
{
 int t,x,y,co;
 while(scanf("%d",&t)!=EOF)
   {
      build(1,0,maxn);
      memset(color,-1,sizeof(color));
      memset(su,0,sizeof(su));
     while(t--)
     {
       scanf("%d%d%d",&x,&y,&co);
       update(1,x,y-1,co);          
     }                  
       qerty(1);
       int px=-1;
       for(int i=0;i<maxn;i++)
       {
         if(px!=color[i]) 
          {
            px=color[i];
            su[px]++;                   
          }       
       }       
       for(int i=0;i<maxn;i++)
        if(su[i]) printf("%d %d\n",i,su[i]);
        printf("\n");
   } 
return 0;
}


2.数组:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=8100;
int color[maxn],cot[maxn];
int main()
{
 int t,i,n,x,y,c;
  while(scanf("%d",&t)!=EOF)
    {
      memset(color,-1,sizeof(color));
      memset(cot,0,sizeof(cot)); 
      int  max=-1;
      while(t--)
      {
        scanf("%d%d%d",&x,&y,&c);
          for(i=x;i<y;i++)
             color[i]=c;
           if(max<y) max=y;          
      }      
                    
       for(i=0;i<max;i++)
       {
          while(i!=0&&color[i]!=-1&&color[i]==color[i-1]) i++;
          if(color[i]!=-1) cot[color[i]]++;                 
       }    
       for(i=0;i<maxn;i++)
         if(cot[i]) printf("%d %d\n",i,cot[i]);
         printf("\n");
    } 
return 0;
} 


* This program demonstrates the effect of using the * generic parameter 'recompute_score' of the operator * find_shape_model_3d. * * By setting 'recompute_score' to 'true', the score * is recomputed after the pose refinement, which * may lead to slightly different (but more consistent) * score values at the cost of a slightly larger * execution time. * * For this, a 3D shape model of a cube is searched in * a sequence of artificially created images by using * find_shape_model_3d. The images are created by * changing the longitude of the camera in the object * centered spherical coordinate system. * * The matching is performed twice. In the first run, * 'recompute_score' is set to 'false', while in the * second run, 'recompute_score' is set to 'true'. The * score is stored for each of the two runs over all * images. Finally, the score values are plotted with * respect to the camera longitude. * * Note that the variation of the score values is * significantly higher when setting 'recompute_score' * to 'false'. Also note that in real images typically * the variation of the score is lower. * gen_cam_par_area_scan_division (0.01, 0, 7e-6, 7e-6, 320, 240, 640, 480, CamParam) get_cam_par_data (CamParam, 'image_width', Width) get_cam_par_data (CamParam, 'image_height', Height) dev_close_window () dev_open_window (0, 0, Width, Height, 'black', WindowHandle) dev_set_part (0, 0, Height - 1, Width - 1) set_display_font (WindowHandle, 14, 'mono', 'true', 'false') dev_update_off () * * Generate a cube X := [-1,-1,1,1,-1,-1,1,1] Y := [1,-1,-1,1,1,-1,-1,1] Z := [-1,-1,-1,-1,1,1,1,1] Polygons := [4,0,1,2,3,4,4,5,6,7,4,0,1,5,4,4,3,2,6,7,4,0,3,7,4,4,1,2,6,5] gen_object_model_3d_from_points (X, Y, Z, ObjectModel3DID) set_object_model_3d_attrib_mod (ObjectModel3DID, 'polygons', [], Polygons) * * Prepare the 3D object model for matching prepare_object_model_3d (ObjectModel3DID, 'shape_based_matching_3d', 'true', [], []) * * Display the 3D object model disp_object_model_3d_safe (WindowHandle, ObjectModel3DID, CamParam, [0,0,11,30,40,0,0], [], []) disp_message (WindowHandle, '3D Object Model of a Cube', 'window', 12, 12, 'black', 'true') disp_continue_message (WindowHandle, 'black', 'true') stop () * * * Generate the 3D shape model create_cube_shape_model_3d (WindowHandle, ObjectModel3DID, CamParam, ShapeModel3DID) clear_object_model_3d (ObjectModel3DID) * Scores1 := [] Scores2 := [] Times1 := [] Times2 := [] Longitudes := [] Latitude := rad(45) Distance := 11.5 dev_set_color ('green') dev_set_line_width (2) for Longitude := rad(30) to rad(60) by rad(0.25) convert_point_3d_spher_to_cart (Longitude, Latitude, Distance, '-y', '-z', CamX, CamY, CamZ) create_cam_pose_look_at_point (CamX, CamY, CamZ, 0, 0, 0, '-y', 0, CamPose) * Prevent flickering set_system ('flush_graphic', 'false') * * Generate an artificial image of the cube project_cube_image (Image, CamPose, CamParam, X, Y, Z, Polygons) * * Find the cube by using 3D shape-based matching * ('recompute_score' = 'false') count_seconds (Seconds1) find_shape_model_3d (Image, ShapeModel3DID, 0.7, 0.9, 0, [], [], Pose1, CovPose1, Score1) count_seconds (Seconds2) Time1 := (Seconds2 - Seconds1) * 1000.0 * Find the cube by using 3D shape-based matching * ('recompute_score' = 'true') count_seconds (Seconds1) find_shape_model_3d (Image, ShapeModel3DID, 0.7, 0.9, 0, 'recompute_score', 'true', Pose2, CovPose2, Score2) count_seconds (Seconds2) Time2 := (Seconds2 - Seconds1) * 1000.0 * * Display the matching result dev_display (Image) if (|Score1| == 1 and |Score2| == 1) * Display the match project_shape_model_3d (ModelContours, ShapeModel3DID, CamParam, Pose1, 'true', rad(30)) dev_display (ModelContours) * Display the matching time and score disp_message (WindowHandle, 'Matching at Longitude: ' + deg(Longitude)$'3.1f' + ' deg (from 30 to 60 deg)', 'window', 12, 12, 'white', 'false') gen_matching_result_message (Time1, Time2, Score1, Score2, Message) disp_message (WindowHandle, Message, 'window', 420, 12, ['white','yellow','green'], 'false') * Remember the score values Longitudes := [Longitudes,deg(Longitude)] Scores1 := [Scores1,Score1] Scores2 := [Scores2,Score2] Times1 := [Times1,Time1] Times2 := [Times2,Time2] endif * Prevent flickering set_system ('flush_graphic', 'true') * Show what we painted in the loop by drawing a invisible line disp_line (WindowHandle, -1, -1, -1, -1) endfor * * Display the score values dev_clear_window () Title := ['\'recompute_score\'=\'false\'','\'recompute_score\'=\'true\''] Colors := ['yellow','green'] plot_tuple (WindowHandle, Longitudes, [Scores1,Scores2], 'Longitude [deg]', 'Matching score', Colors, ['ticks_x','ticks_y','end_y'], [6,.01,1]) disp_message (WindowHandle, Title, 'window', 12, 350, Colors, 'false') disp_continue_message (WindowHandle, 'black', 'true') stop () dev_clear_window () plot_tuple (WindowHandle, Longitudes, [Times1,Times2], 'Longitude [deg]', 'Matching time [ms]', Colors, 'ticks_x', 6) disp_message (WindowHandle, Title, 'window', 12, 350, Colors, 'false') * * Clear the 3D shape model clear_shape_model_3d (ShapeModel3DID)
最新发布
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值