Count the Colors

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

 
错误点:在刚开始以为n就是树的最大范围了,然后一直报段错误(RE),后来发现maxn才是,解决后又wa了几次。还有就是(2,3,0)和(4,5,0)其实是两段,需要注意。
 
解题思路:区间替换、用线段树平常意义上的点代替长度为1的单位线段,统计段数。这里用到线段树只是为了保存哪些线段是什么颜色的,树中保存了一段一段的颜色,我们需要通过一次询问,把树上的颜色段拿出来,然后放在一条线上,某些区间什么颜色,这就已经不再跟线段树有关系了,就是平常的统计了。
 
#include<bits/stdc++.h>
using namespace std;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 10000;
int colseg[maxn];
int cnt[maxn];
struct SegTree{
int col;
}segs[maxn*4];
void buildtree(int rt,int L,int R){
segs[rt].col = 0;
if(L == R){
return;
}
buildtree(lson);
buildtree(rson);
}
void PushDown(int rt){
if(segs[rt].col){
segs[rt*2].col = segs[rt*2+1].col = segs[rt].col;
segs[rt].col = 0;
}
}
void Update(int rt,int L,int R,int l_ran,int r_ran,int _col){
if(l_ran <= L && R <= r_ran){
segs[rt].col = _col;
return;
}
PushDown(rt);
if(l_ran <= mid){
Update(lson,l_ran,r_ran,_col);
}
if(r_ran > mid){
Update(rson,l_ran,r_ran,_col);
}
}
void query(int rt,int L,int R){ //把颜色段从树中拿出来,放入colseg数组中
if(segs[rt].col){
for(int i = L; i <= R; i++){
colseg[i] = segs[rt].col;
}
segs[rt].col = 0;
return ;
}
if(L == R) return;
query(lson);
query(rson);
}
int main(){
int n, m;
n = 8001;
while(scanf("%d",&m)!=EOF){
memset(cnt,0,sizeof(cnt));
memset(colseg,0,sizeof(colseg));
buildtree(1,1,n);
int u, v, w;
for(int i = 1; i <= m; i++){
scanf("%d%d%d",&u,&v,&w);
w++;
u++; //把点都看成线段
Update(1,1,n,u,v,w);
}
query(1,1,n);
for(int i = 1; i <= n+10; i++){
if(!colseg[i]) continue;
int j;
for(j = i; j <= n+10 && colseg[i]==colseg[j]; j++) ;
cnt[colseg[i]]++;
i = j - 1; //这里的点都是代表长度为1的单位线段
}
for(int i = 1; i <= n+10; i++){
if(cnt[i]){
printf("%d %d\n",i-1,cnt[i]);
}
}puts("");
}
return 0;
}

  

 
 

ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】的更多相关文章

  1. zoj 1610 Count the Colors 线段树区间更新/暴力

    Count the Colors Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...

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

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

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

    题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...

  4. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

  5. ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

    任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...

  6. ZOJ 1610 Count the Colors (线段树区间更新与统计)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

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

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  8. ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

    1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...

  9. Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)

    题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...

随机推荐

  1. 以太坊系列之十六:golang进行智能合约开发

    以太坊系列之十六: 使用golang与智能合约进行交互 以太坊系列之十六: 使用golang与智能合约进行交互 此例子的目录结构 token contract 智能合约的golang wrapper ...

  2. vs2015+opencv3.3.1 +Eigen 3.3.4 c++ 实现 泊松图像编辑(无缝融合)

    #define EIGEN_USE_MKL_ALL #define EIGEN_VECTORIZE_SSE4_2 #include <iostream> #include "co ...

  3. 【10.2.3】ArcGIS Runtime for Android搭建开发环境过程中问题具体解释

    一.Visual Studio Ultimate2012安装过程问题 1.问题描写叙述 安装完毕后,您将看到一条消息,指示安装程序已完毕.但并非全部的功能具有已正确安装.以及下面警告消息: Micro ...

  4. kali linux之本地提权

    已实现本地低权限帐号登录,希望获取更高权限,实现对目标进一步控制 系统帐号之间权限隔离(操作系统安全的基础,用户空间,内核空间) 系统帐号(用户帐号登录时获取权限令牌,服务帐号无需用户登录已在后台启动 ...

  5. 流水的算法,铁打的损失函数/MLE

    机器学习算法可以说是不少的,如果死记硬背的话,只能当时记得推导过程和步骤,过一段时间就又想不起来了,只能依稀记得一些影子.所以,应该找到算法的一些通用的方法来理解算法的思路以及推导过程. 我认为,最大 ...

  6. 详细解读Python中的__init__()方法

    init()方法意义重大的原因有两个.第一个原因是在对象生命周期中初始化是最重要的一步:每个对象必须正确初始化后才能正常工作.第二个原因是init()参数值可以有多种形式. 因为有很多种方式为init ...

  7. 运用CSS高斯模糊添加图片加载效果

    <!DOCTYPE html> <html> <head> <title>大图片加载从模糊到清晰</title> </head> ...

  8. Spring学习笔记(二)——Spring相关配置&属性注入&Junit整合

    一.Spring的相关配置 1.1 Bean元素 class属性:被管理对象的完整类名 name属性:给Bean起个名字,能重复,能使用特殊字符.后来属性 id属性:给Bean起个名字,不能重复,不能 ...

  9. php 缓冲函数

    php.ini中有两个关键参数会影响到php的缓存输出控制: output_buffering :on/off 或者整数 .设置为 on 时,将在所有脚本中使用输出缓存控制,不限制缓存的大小.而设置为 ...

  10. python之函数(二)

    上一篇中我们在函数体中的写的都是打印语句,用print来输出打印结果.但是在实际使用的时候,我们并不需要将结果输出在控制台上.这时候该如何解决呢? 1. return返回值. 我们可以将函数的结果通过 ...