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

ZOJ 1610 Count the Color(线段树)

时间:2018-07-08 23:40:15      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:names   mes   seq   ons   延迟   RoCE   ace   sizeof   scanf   

描述

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

题意

有一个线段,然后有n个操作,每个操作[X,Y]染成C颜色,最后问你线上的每个颜色有几段

题解

线段树区间延迟标记,最底层所有节点查询操作

代码

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int N=8000+5;
 7 
 8 int lazy[N<<2],dif[N<<2],col[N],tot;
 9 
10 void PushDown(int rt)
11 {
12     if(lazy[rt]!=-1)
13     {
14         lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
15         lazy[rt]=-1;
16     }
17 }
18 void Update(int L,int R,int C,int l,int r,int rt)
19 {
20     if(L<=l&&r<=R)
21     {
22         lazy[rt]=C;
23         return;
24     }
25     int mid=(l+r)>>1;
26     PushDown(rt);
27     if(L<=mid)Update(L,R,C,l,mid,rt<<1);
28     if(R>mid)Update(L,R,C,mid+1,r,rt<<1|1);
29 }
30 void Query(int l,int r,int rt)
31 {
32     if(l==r)
33     {
34         dif[tot++]=lazy[rt];
35         return;
36     }
37     int mid=(l+r)>>1;
38     PushDown(rt);
39     Query(l,mid,rt<<1);
40     Query(mid+1,r,rt<<1|1);
41 }
42 int main()
43 {
44     int n,x,y,z;
45     while(scanf("%d",&n)!=EOF)
46     {
47         tot=0;
48         memset(lazy,-1,sizeof(lazy));
49         memset(col,0,sizeof(col));
50         for(int i=0;i<n;i++)
51         {
52             scanf("%d%d%d",&x,&y,&z);
53             Update(x+1,y,z,1,8000,1);
54         }
55         Query(1,8000,1);
56         for(int i=0;i<tot;)
57         {
58             if(dif[i]==-1)
59             {
60                 i++;continue;
61             }
62             int j=i;
63             while(dif[i]==dif[j])j++;
64             col[dif[i]]++;
65             i=j;
66         }
67         for(int i=0;i<=8000;i++)
68             if(col[i])
69                 printf("%d %d\n",i,col[i]);
70         printf("\n");
71     }
72     return 0;
73 }

 

ZOJ 1610 Count the Color(线段树)

标签:names   mes   seq   ons   延迟   RoCE   ace   sizeof   scanf   

原文地址:https://www.cnblogs.com/taozi1115402474/p/9281752.html

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