Count the Colors-ZOJ1610(线段树区间求)
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开始,所以应该用区间
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
#define N 8005
#define Lson r<<1
#define Rson r<<1|1
int sum[N],temp; struct node
{
int L,R,e;
int mid()
{
return (L+R)/;
}
int len()
{
return R-L+;
}
}a[N*]; void BuildTree(int r,int L,int R)
{
a[r].L=L;
a[r].R=R;
a[r].e=-;
if(L==R-)
{
return;
}
BuildTree(Lson,L,a[r].mid());
BuildTree(Rson,a[r].mid(),R);
}
void Qurry(int r)
{
if(a[r].e!=-)
{
if(a[r].e!=temp)
{//temp表示这个区间的颜色没有被加过
sum[a[r].e]++;
}
temp=a[r].e;
return;
}
if(a[r].L==a[r].R-)
{
temp=-;
return;
}
Qurry(Lson);
Qurry(Rson);
}
void Update(int r,int L,int R,int e)
{ if(a[r].L==L && a[r].R==R)
{
a[r].e=e;
return;
}
if(a[r].e!=-)
{
a[Lson].e=a[Rson].e=a[r].e;
a[r].e=-;
}
if(R<=a[r].mid())
Update(Lson,L,R,e);
else if(L>=a[r].mid())
Update(Rson,L,R,e);
else
{
Update(Lson,L,a[r].mid(),e);
Update(Rson,a[r].mid(),R,e);
}
}
int main()
{
int n,x,y,e;
while(scanf("%d",&n)!=EOF)
{
memset(sum,,sizeof(sum));
BuildTree(,,N);
int Max=;
for(int i=;i<n;i++)
{
scanf("%d %d %d",&x,&y,&e);
Max=max(Max,e);
Update(,x,y,e); }
temp=-;
Qurry();
for(int i=;i<=Max;i++)
{
if(sum[i])
{
printf("%d %d\n",i,sum[i]);
}
}
printf("\n");
}
return ;
}
Count the Colors-ZOJ1610(线段树区间求)的更多相关文章
- ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)
1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
- ZOJ - 1610 Count the Colors(线段树区间更新)
https://cn.vjudge.net/problem/ZOJ-1610 题意 给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000. ...
- hdu4521-小明系列问题——小明序列(线段树区间求最值)
题意:求最长上升序列的长度(LIS),但是要求相邻的两个数距离至少为d,数据范围较大,普通dp肯定TLE.线段树搞之就可以了,或者优化后的nlogn的dp. 代码为 线段树解法. #include ...
- hdu 1754 I Hate It(线段树区间求最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Count the Colors(线段树染色)
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- F - Count the Colors(线段树)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
随机推荐
- STM32 (基础语法)笔记
指针遍历: char *myitoa(int value, char *string, int radix){ int i, d; int flag = 0; char *ptr = string; ...
- spring framework 第一章数据库管理(data access)
spring data access 的网址:https://docs.spring.io/spring/docs/current/spring-framework-reference/index.h ...
- MYSQL 查询方法 统计查询 链接查询 子查询
mysql表格查询方法: 查询: 1.简单查询 select * from Info --查所有数据select Code,Name from Info --查指定列的数据select Code as ...
- Matlab中size、numel、length、fix函数的使用
size():获取矩阵的行数和列数 (1)s=size(A), 当只有一个输出参数时,返回一个行向量,该行向量的第一个元素时矩阵的行数,第二个元素是矩阵的列数. (2)[r,c]=size ...
- vs code 用户代码片段 html.json
{ // Place your snippets for html here. Each snippet is defined under a snippet name and has a p ...
- CAD绘制固定矩形标注(网页版)
js中实现代码说明: function DoFixRectComment() { var ent = mxOcx.DrawCustomEntity("TestMxCustomEntity&q ...
- CAD参数绘制实心圆弧填充(网页版)
js中实现代码说明: function DrawPathToHatch1() { //把路径的开始位置移动指定的点 //参数一为点的X坐标 ,参数二为点的Y坐标,参数三为该点处开始宽度,对Polyli ...
- 怎样让Oracle的存储过程返回结果集
Oracle存储过程: CREATE OR REPLACE PROCEDURE getcity ( citycode IN VARCHAR2, ref_cursor OUT sys_refcursor ...
- react-native 0.58版本打包图片问题 task ':app:mergeReleaseResources' Error: Duplicate resources
debug没问题,在生成正式apk的时候就如下: google了一下在github上找到了解决方案: github问题指向 在node_modules/react-native/react.gradl ...
- 零基础入门学习Python(32)--异常处理:你不可能总是对的
知识点 即便Python程序的语法是正确的,在运行它的时候,也有可能发生错误.运行期检测到的错误被称为异常. 大多数的异常都不会被程序处理,都以错误信息的形式展现在这里: Python标准异常总结 序 ...