ZOJ 1610 Count the Color(线段树)

描述

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 }

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

时间: 2024-10-02 01:54:00

ZOJ 1610 Count the Color(线段树)的相关文章

ZOJ 1610 Count the Colors (线段树成段更新)

题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以对于区间(L, R)我们只要让左端点+1即可按照正常的线段树操作来做. #include<bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 using namespace std; co

ZOJ 1610 Count the Colors(线段树,但暴力未必不行)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 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 color

ZOJ - 1610 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. InputThe first line of each data set contains exactly o

ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)

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 s

ZOJ 1610 Count the Colors (线段树区间更新)

题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头输出. //ZOJ 1610 #include <cstdio> #include <cstring> #include <iostream> using namespace std ; int p[8010*4],lz[8010*4] ,hashh[8010*4],has

POJ 2777 Count Color(线段树)

题目地址:POJ 2777 我去..延迟标记写错了.标记到了叶子节点上....这根本就没延迟嘛...怪不得一直TLE... 这题就是利用二进制来标记颜色的种类.然后利用或|这个符号来统计每个区间不同颜色种数. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h

POJ2777 Count Color 线段树区间更新

题目描述: 长度为L个单位的画板,有T种不同的颜料,现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B",查询[A,B]区域内出现的颜色种类 出现操作2时,请输出答案 PS:初始状态下画板颜色为1 一开始没有想那么好,用int整型位移来代替颜色,还是使用了最传统的bool color[来记录,可是不知道错在了哪里, #include<iostream> #include<cstdio>

POJ 2777 Count Color (线段树+位运算)

题意很简单了,对一个区间有两种操作: 1. "C A B C" Color the board from segment A to segment B with color C. //A~B涂上颜色C 2. "P A B" Output the number of different colors painted between segment A and segment B (including). //输出A~B间颜色的种类数 题目链接:http://poj.o

zoj 1610 Count the Colors

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610 先用线段树维护区间颜色的覆盖,然后在把区间的颜色映射到数组,再从数组统计颜色. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 100000 5 using namespace std; 6 7 int n; 8 int min1; 9