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 ...
随机推荐
- iOS 常用随机数
比如:获取一个随机整数范围在:[0,100)包括0,不包括100 ; 参考:https://www.jianshu.com/p/106475cbd3da
- 详解如何定义SQL Server外关键字约束
SQL Server外关键字约束定义了表之间的关系.当一个表中的一个列或多个列的组合和其它表中的主关键字定义相同时,就可以将这些列或列的组合定义为外关键字,并设定它适合哪个表中哪些列相关联.这样,当在 ...
- 分布式架构下的会话追踪实践【基于Cookie和Redis实现】
分布式架构下的会话追踪实践[基于Cookie和Redis实现] 博客分类: NoSQL/Redis/MongoDB session共享rediscookie分布式架构session 在单台Tomcat ...
- Django token 学前小知识
1,base64 '防君子不防小人' 方法 作用 参数 返回值 b64encode 将输入的参数转化为base64规则的串 预加密的明文,类型为bytes:例:b‘guoxiaonao’ base64 ...
- Visual C++中error spawning cl.exe解决办法
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 今天安装Vc6.0的时候出现了一个error spawning cl.exe的错误,在网上找了一些资料,才知道这是因为路径设置的问题引起的 ...
- Loadrunner实现多台测试机负载时可能遇到的难题
压测机安装的loadrunner版本为11.0 压测机器的操作系统是windows server 2016 所有的负载机器操作系统和压侧机器是一样的,都是windows server 2016 Err ...
- v8引擎的优化
1.编译优化 V8采用JIT即使编译技术. 例如JAVA是先编译成字节码,再由JVM编译成机器码,V8则没有中间的字节码,直接由源码生成语法树,然后编译成机器码. 2.隐藏类 当定义一个构造函数,使用 ...
- jumpserver4.0centos7安装步骤
一步一步安装(CentOS) 测试推荐环境 CPU: 64位双核处理器 内存: 4G DDR3 数据库:mysql 版本大于等于 5.6 mariadb 版本大于等于 5.5.6 环境 系统: Cen ...
- 在$CF$水题の记录
CF1158C CF1163E update after CF1173 很好,我!expert!掉rating了!! 成为pupil指日可待== 下次要记得合理安排时间== ps.一道题都没写的\(a ...
- cf:c题
题目: 代码: #include<iostream> #include<algorithm> #include<vector> #include<string ...