题目链接: 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 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

在[0, 8000]涂色,后涂的会覆盖前面涂的,求每种颜色最后能分辨的的区块有多少个。

建树时,树的单位是区间,而不是点 如[0, 8000]的左右儿子应该分别是[0, 4000], [4000,8000];

询问时,需要判断相邻区间的颜色是否相同 用temp记录前一区间所记录的颜色来解决这个问题。

 #include<iostream>
#include<cstring>
using namespace std; int color[];
int mark[];
int temp; void Build( int i, int l, int r ){
mark[i] = -;//-1表示没有涂 if( l + == r ) return; int mid = ( l + r ) >> ;
Build( i << , l , mid );
Build( ( i << ) | , mid, r );
} void Insert( int i, int l, int r, int z, int y, int c ){
if( l == r ) return;
if( z <= l && y >= r ){
mark[i] = c;
return;
}
if( mark[i] == c ) return;
if( mark[i] >= ){
mark[i << ] = mark[i];
mark[( i << ) | ] = mark[i];
mark[i] = -;//该区间含有多个颜色
}
int mid = ( l + r ) >> ;
if( y <= mid ) Insert( i << , l, mid, z, y, c );
else if( z >= mid ) Insert( ( i << ) | , mid, r, z, y, c );
else{
Insert( i << , l, mid, z, mid, c );
Insert( ( i << ) | , mid, r, mid, y, c );
}
mark[i] = -;
} void Queue( int i, int l, int r){
if( mark[i] == - ){
temp = -;
return;
}
if( mark[i] >= ){
if( temp != mark[i] ){
temp = mark[i];//记录前一段的颜色
color[mark[i]]++;
}
return;
}
if( l + != r ){
int mid = ( l + r ) >> ;
Queue( i << , l, mid );
Queue( ( i << ) | , mid, r );
}
} int main(){
ios::sync_with_stdio( false ); int n;
while( cin >> n ){ Build( , , );
memset( color, , sizeof( color ) ); int a, b, c, max = ;
for( int i = ; i <= n; i++ ){
cin >> a >> b >> c;
Insert( , , , a, b, c );
max = c > max ? c : max;
} temp = -;
Queue( , , ); for( int i = ; i <= max ; i++ )
if( color[i] ) cout << i << " " << color[i] << endl; cout << endl;
} return ;
}

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 (线段树成段更新)

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

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

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

  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-线段树(区间染色、区间更新、单点查询)-有点小坑(染色片段)

    ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting s ...

  7. ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】

    Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

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

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

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

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

  10. zoj 1610 Count the Colors(线段树延迟更新)

    所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...

随机推荐

  1. selenium定时签到程序

    selenium定时签到程序 定时任务 # -*- coding: utf-8 -*- import time import os import sched import datetime from ...

  2. 【Java例题】1.2计算n的m次方

    package study; import java.util.*; import java.math.*; public class study { public static void main( ...

  3. Viper-Go一站式配置管理工具

    什么是Viper Viper是一个方便Go语言应用程序处理配置信息的库.它可以处理多种格式的配置.它支持的特性: 设置默认值 从JSON.TOML.YAML.HCL和Java properties文件 ...

  4. 利用hash或history实现单页面路由

    目录 html代码 css代码 JavaScript代码 hash方式 history 方式 浏览器端代码 服务器端 在chrome(版本 70.0.3538.110)测试正常 编写涉及:css, h ...

  5. 【0806 | Day 9】异常处理/基本的文件操作

    一.异常处理 异常即报错,可分为语法异常和逻辑异常 1. 语法异常 举个栗子 if #报错 syntaxerror 0 = 1 #报错 syntaxerror ... 正经地举个栗子 print(1) ...

  6. SQL获取客户端网卡电脑名称等信息

    Select SYSTEM_USER 当前用户名, USER_NAME() 当前所有者,db_Name() 当前数据库,@@SPID 当前进程号,(select top 1 FileName from ...

  7. JMeter的JavaRequest探究

    1.背景 最近笔者的一位老朋友咨询了一个问题:在自定义的Java请求中如何编写多个请求?老朋友反应他们发送请求只能基于这种Java请求形式(代码调需用三方封装的jar包).这个问题恰巧不久前在笔者所在 ...

  8. SpringDataJpa在一对多、多对多关系映射时出现StackOverflowError

    在使用spring-data-jpa时,进行一对多配置后,在调用save方法时,出现内存溢出. 产生原因一:为了方便看信息,在两类中分别重写了 toString 方法,导致查询加载时两类在互相调用对方 ...

  9. js 事件发布订阅销毁

    在vue中 通过$on订阅事件,通过$emit触发事件以此可用来事件跨组件传值等功能,但是有个弊端就是通过这种方式订阅的事件可能会触发多次. 特别是通过$on订阅的事件中如果有http请求,将会造成触 ...

  10. 表结构查询 Sql

    select row_number() over(order by a.column_id) rownumber, a.name, case ),a.),a.scale) +')' ),a.max_l ...