Count the Colors---zoj1610线段树
题意:
求每种颜色有几段线段;
模拟数组:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define N 8006
int cnt[N],a[N];
int main()
{
int n, p, q, c;
while(scanf("%d", &n) != EOF)
{
int Max = ;
memset(a, , sizeof(a));
memset(cnt, , sizeof(cnt));
for(int i=; i<n; i++)
{
scanf("%d%d%d", &p, &q, &c);
for(int j=p; j<q; j++)
a[j] = c+;
Max = max(Max, q);
}
for(int i=; i<Max; i++)
{
while(i!= && a[i]!= && a[i]==a[i-])
i++;
if(a[i] != )
cnt[ a[i]- ]++;
}
for(int i=; i<N; i++)
{
if(cnt[i]!=)
printf("%d %d\n", i, cnt[i]);
}
printf("\n");
}
return ;
}
#include<stdio.h>
#include<string.h>
#define N 8005 struct SegTree
{
int L,R,c;
int mid()
{
return (L+R)>>;
}
}a[N*]; int cnt[N], vis[N]; void BuildSegTree(int r, int L, int R)
{
a[r].L = L;
a[r].R = R;
a[r].c = -;
if(L+==R)//一条线段的两个端点相差1,
return ;
BuildSegTree(r*, L, a[r].mid());
BuildSegTree(r*+, a[r].mid(), R);//题目给的是端点值,对于线段不能用a[r].mid()+1;
}
void Update(int r, int L, int R, int c)
{
if(a[r].c == c)return ;//如果说这条线段的颜色和要涂的相同,返回;
if(a[r].L == L && a[r].R == R)
{
a[r].c = c;//指定范围内改成颜色c;
return ;
}
if(a[r].c != -)
{
a[*r].c = a[*r+].c = a[r].c;//更新左右子树;
a[r].c = -;
} if(R<=a[r].mid())
Update(*r, L, R, c);
else if(L>=a[r].mid())//等于号不能省;原因是这是线段不是点;
Update(*r+, L, R, c);
else
{
Update(*r, L, a[r].mid(), c);
Update(*r+, a[r].mid(), R, c);
}
}
void Query(int r, int L, int R)
{
if(a[r].c != -)//到达叶子节点;
{
if(vis[L] != a[r].c)
cnt[ a[r].c ]++;
vis[R] = a[r].c;
return ;
}
if(R-L == )
return ;
Query(*r, L, a[r].mid());
Query(*r+, a[r].mid(), R);
} int main()
{
int n, p, q, c;
while(scanf("%d", &n) != EOF)
{
memset(cnt, , sizeof(cnt));
memset(vis, -, sizeof(vis)); BuildSegTree(, , ); while(n--)
{
scanf("%d %d %d", &p, &q, &c);
if(p == q)continue;
Update(, p, q, c);
}
Query(, , ); for(int i=; i<N; i++)
{
if(cnt[i] != )
printf("%d %d\n", i, cnt[i]);
}
printf("\n");
}
return ;
}
Count the Colors---zoj1610线段树的更多相关文章
- Count the Colors(线段树染色)
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- 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 ...
- F - 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(线段树区间更新,单点查询)
1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...
- Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- zoj 1610 Count the Colors(线段树延迟更新)
所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...
- ZOJ 1610 Count the Colors 【线段树】
<题目链接> 题目大意: 在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每 ...
- ZOJ-1610 Count the Colors(线段树染色,求染色段)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 https://vjudge.net/contest/318019# ...
随机推荐
- Greenplum-cc-web安装
第一章 文档概述 1. 本安装手册描述适用于Greenplum4.0以上版本的安装Greenplum-cc-web操作 第二章 安装介质 针对Greenplum版本下载对应Greenplum-cc-w ...
- FFmpeg X264的preset和tune
鉴于x264的参数众多,各种参数的配合复杂,为了使用者方便,x264建议如无特别需要可使用preset和tune设置.这套开发者推荐的参数较为合理,可在此基础上在调整一些具体参数以符合自己需要,手动设 ...
- 带有ZLIB_LIBRARY_DEBUG的FindZLIB.cmake文件
CMake自带的FindZLIB.cmake只有ZLIB_LIBRARY,而没有ZLIB_LIBRARY_DEBUG 将下面的代码保存成FindZLIB.cmake,替换掉D:\Program Fil ...
- tomcat端口被占用的两个解决方法
tomcat 的 8080 端口经常会被占用,解决办法两个: 1.关闭占用8080端口的进程:8080端口被占用的话执行startup.bat会报错,可在cmd下执行netstat -ano命令查看8 ...
- 在MyEclipse(2015)中上传项目到github的步骤(很详细)
(图文)在MyEclipse(2015)中上传项目到github的步骤(很详细) git|smartGit使用详解 SmartGit使用教程
- Contain的使用
- 【MySQL8】 安装后的简单配置(主要解决navicat等客户端登陆报错问题)
一.navicat等客户端登陆报错的原因 使用mysql,多数我们还是喜欢用可视化的客户端登陆管理的,个人比较喜欢用navicat.一般装好服务器以后,习惯建一个远程的登陆帐号,在mysql8服务器上 ...
- c++ auto 理解
for (auto i : b) Fuck(i); 是 for (auto bitch = std::begin(b); bitch != std::end(b); bitch++) { auto t ...
- css方法 - 移动端h5在iphonex的适配
@media only screen and (device-width:375px) and (device-height:812px) and (-webkit-device-pixel-rati ...
- JavaScript 中的 Map
很多编程语言中都有类似Map这种 键-值对 的数据结构. 可惜,JavaScript没有. 幸运的是,可以自己构建一个Map对象. 对象的定义 <script type="text/j ...