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 ...
随机推荐
- mybatis源码分析之03SqlSession的创建
在上一篇中,说到了mybatis是如何构造一个SqlSessionFactory实例的,顾名思意,SqlSessionFactory就是用于创建SqlSession的工厂类. 好,现在我们接着昨天的来 ...
- CodeForces - 1038D (线性DP)
题目:https://codeforces.com/problemset/problem/1038/D 题意:给你n个数字,每个数字可以吃左右两边的数,然后吃完后自己变成 a[i]-a[i+1]或者a ...
- mysql 一条sql完成saveOrUpdate 存在即更新
关键字 on duplicate key update <pre name="code" class="sql"> insert into tabl ...
- python中模块介绍
一,模块概念 在计算机程序开发的过程当中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编码更加容易维护,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码 ...
- 测开之路四十八:Django之重定向与cookie
基础配置与上一篇一致 404错误 定义一个error页面 <!DOCTYPE html><html lang="en"><head> <m ...
- 测开之路二十三:python常用模块
os模块 sys模块 hashlib shutil对文件和目录进行操作 random和随机相关 json
- 同步架构OR异步架构
把智能系统比喻成KFC营业厅,处理器是窗口和窗口后面的服务员(把一个窗口当作一个核心),指令集是后面排队的人,窗口是数据吞吐量.当中午就餐人多的时候,一个窗口肯定忙不过来,这时候可以增加窗口,有两种方 ...
- VMWARE ESXI 虚拟硬盘的格式:精简置备、厚置备延迟置零、厚置备置零
精简置备(thin): 精 简配置就是无论磁盘分配多大,实际占用存储大小是现在使用的大小,即用多少算多少.当客户机有输入输出的时候,VMkernel首先分配需要的空间并进行 清零操作,也就是说如果使用 ...
- Charles重发请求
1.如下图 2.选中某个接口,右键--选择 Repeat Advanced选项,设置请求多次 3.
- byte与base64string的相互转化以及加密算法
//在C#中 //图片到byte[]再到base64string的转换: Bitmap bmp = new Bitmap(filepath); MemoryStream ms = new Memory ...