ZOJ 1610 Count the Colors (线段树区间更新与统计)
Your task is counting the segments of different colors you can see at last.
Input
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
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
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
ZOJ挂了,我还没交没不知道对不对。应该没问题,按照kuangbin大神的模板改的。这个题又是区间更新,不过看数据范围不用离散化。
因为每个节点都代表区间的端点,kuangbin大神的处理方法在建树的时候与普通线段树模板做了微小的改动。
代码如下:
#include <bits/stdc++.h> using namespace std;
#define M 8010
struct segTree
{
int l,r,col;
}tree[M<<];
int color[M],temp,n;
void buildTree (int now,int l,int r)
{
tree[now].l=l,tree[now].r=r;
tree[now].col=-;//-1代表没有颜色
if (l+==r)
return ;
int m=((l+r)>>);
buildTree(now<<,l,m);
buildTree(now<<|,m,r);//因为节点代表端点,二分建树时是[l,m],[m,r],而不是[m+1,r]。
}
void upDate (int now,int l,int r,int c)
{
if (l==r||l>r)
return ;
if (tree[now].col==c)
return ;
if (l<=tree[now].l&&tree[now].r<=r)
{
tree[now].col=c;
return ;
}
if (tree[now].col>=)
{
tree[now<<].col=tree[now].col;
tree[now<<|].col=tree[now].col;
tree[now].col=-;
}
int mid=(tree[now].l+tree[now].r)>>;
if (r<=mid)
upDate(now<<,l,r,c);
else if (l>=mid)
upDate(now<<|,l,r,c);
else
{
upDate(now<<,l,mid,c);
upDate(now<<|,mid,r,c);
}
tree[now].col=-;//-2代表有多种颜色
}
void Count (int now)
{
if (tree[now].col==-)
{
temp=-;//temp是前一段的颜色
return ;
}
if (tree[now].col!=-)
{
if (tree[now].col!=temp)
{
color[tree[now].col]++;
temp=tree[now].col;
}
return ;
}
if (tree[now].l+!=tree[now].r)
{
Count(now<<);
Count(now<<|);
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d",&n))
{
int x,y,z,maxn=;
buildTree(,,);
for (int i=;i<n;++i)
{
scanf("%d%d%d",&x,&y,&z);
upDate(,x,y,z);
maxn=max(maxn,z);
}
temp=-;
memset(color,,sizeof color);
Count();
for (int i=;i<=maxn;++i)
{
if (color[i])
printf("%d %d\n",i,color[i]);
}
printf("\n");
}
return ;
}
ZOJ 1610 Count the Colors (线段树区间更新与统计)的更多相关文章
- zoj 1610 Count the Colors 线段树区间更新/暴力
Count the Colors Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- ZOJ 1610 Count the Colors (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- ZOJ 1610.Count the Colors-线段树(区间染色、区间更新、单点查询)-有点小坑(染色片段)
ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting s ...
- ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- poj 2777 Count Color(线段树 区间更新)
题目:http://poj.org/problem?id=2777 区间更新,比点更新多一点内容, 详见注释, 参考了一下别人的博客.... 参考博客:http://www.2cto.com/kf/ ...
- ZOJ1610 Count the Colors —— 线段树 区间染色
题目链接:https://vjudge.net/problem/ZOJ-1610 Painting some colored segments on a line, some previously p ...
随机推荐
- 第一次用angularJS做后台管理点滴
很早以前就大概看过一点angualrjs,但是没有项目,一直没有进行下去,就是干巴巴的看着,过了一段时间发现什么也不记得了. 来yulebaby我的第一个后台管理是用easyui做的,做完那个以后发现 ...
- Python基础教程(017)--执行Python的方式解释器运行及其他几种解释器简介
前言 了解Python的解释器 内容 Python的解释器乳交有多个语言的实现 cPython---官方版本的C语言实现 Jython--可以运行在java平台 IronPython--可以运行在.n ...
- speike
speike 题目描述 众所周知,Speike 狗是一条特别喜欢追着Tom 打的狗. 现在,Tom 又把Speike 惹生气了,现在Speike 需要跨越千山万水找Tom 报仇. Speike 所在的 ...
- linux 给指定用户分配文件夹权限
1.更改目录所有者命令:chown -R 用户名称 目录名称2.更改目录权限命令:chmod -R 755 目录名称3.查看文件夹的权限ls -la 目录
- XScreenSaver强大的锁屏工具
source install: https://www.jwz.org/xscreensaver/ XScreenSaver Related articles DPMS Xresources ...
- 三线SWD模式Jlink
三线SWD模式Jlink 在公司实习,部门经理让我做一个USB-CAN的适配器. 在网上找资料,找到一个开源的USB-CAN的适配器的资料. 采用的是CP2102芯片实现USB转串口.STM32作 ...
- MVC路由解析---IgnoreRoute
MVC路由解析---IgnoreRoute 文章引导 MVC路由解析---IgnoreRoute MVC路由解析---MapRoute MVC路由解析---UrlRoutingModule Are ...
- nginx 虚拟主机+反向代理+负载均衡
nginx是一款免费.开源的http服务器,它是由俄罗斯程序设计师开发的,官方测试,nginx能支撑5万的并发量,主要功能有虚拟主机.反向代理和负载均衡等. nginx配置 # 全局块 ... # e ...
- 嵌入式C语言3.2 关键字---自定义数据类型
1. struct 结构体 基本语法 struct myabc{ unsigned int a; unsigned int b; unsigned int c; unsigned int d; } 调 ...
- Android深度探索-卷1第六章心得体会
这章主要介绍了第一个linux驱动程序:统计单词个数.Linux系统将每一个驱动都映射成一个文件,这些文件称为设备文件或驱动文件,都保存在/dev目录中.大多数Linux驱动都有与其对应的设备文件,因 ...