题目大意:

  有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分?

解题思路:

  这个题目建树的时候有些不同,并不是以点为对象,而是以区间为对象,很明显是对线段树的区间进行操作,更新的时候要以区间为单位,还有就是计算每个区间出现几次的时候可以根据线段树的建树特征对树进行遍历求解。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ;
struct node
{
int l, r, c;
int Mid()
{
return (r + l) / ;
}
};
node tree[maxn*];
int cnt[maxn], temp; void Build (int root, int l, int r)
{
tree[root].l = l;
tree[root].r = r;
tree[root].c = -;
if (l == r - )
return ;
Build (*root+, l, tree[root].Mid());
Build (*root+, tree[root].Mid(), r);
}
void update (int root, int l, int r, int c)
{//区间更新,
if (l==tree[root].l && tree[root].r==r)
{
tree[root].c = c;
return ;
}
if (tree[root].c != -)
{
tree[*root+].c = tree[*root+].c = tree[root].c;
tree[root].c = -;
}
if (l >= tree[root].Mid())
update (*root+, l, r, c);
else if (r <= tree[root].Mid())
update (*root+, l, r, c);
else
{
update (*root+, l, tree[root].Mid(), c);
update (*root+, tree[root].Mid(), r, c);
}
}
void Count (int root)
{
if (tree[root].c != -)
{//当前区间裸露在外面
if (tree[root].c != temp)//当前区间没有被统计过
cnt[tree[root].c] ++;
temp = tree[root].c;
return ;
}
if (tree[root].l == tree[root].r-)
{//返回未被覆盖的区间,否则会栈溢出
temp = -;
return ;
}
Count (*root+);
Count (*root+);
} int main ()
{
int n, m;
while (scanf ("%d", &n) != EOF)
{
Build (, , maxn); while (n --)
{
int x, y, c;
scanf ("%d %d %d", &x, &y, &c);
update (, x, y, c);
}
temp = -;
memset (cnt, , sizeof(cnt));
Count();
for (int i=; i<maxn; i++)
if (cnt[i])
printf ("%d %d\n", i, cnt[i]);
printf ("\n");
}
return ;
}

Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)的更多相关文章

  1. zoj 1610 Count the Colors 线段树区间更新/暴力

    Count the Colors Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...

  2. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

  3. ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  4. ZOJ 1610 Count the Colors (线段树成段更新)

    题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...

  5. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  6. ZOJ 1610.Count the Colors-线段树(区间染色、区间更新、单点查询)-有点小坑(染色片段)

    ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting s ...

  7. ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  8. poj 2777 Count Color(线段树 区间更新)

    题目:http://poj.org/problem?id=2777 区间更新,比点更新多一点内容, 详见注释,  参考了一下别人的博客.... 参考博客:http://www.2cto.com/kf/ ...

  9. ZOJ1610 Count the Colors —— 线段树 区间染色

    题目链接:https://vjudge.net/problem/ZOJ-1610 Painting some colored segments on a line, some previously p ...

随机推荐

  1. idea、jdk、eclispe中空main方法的线程数量不一样,why?

    測试代码: public class Test {     public static void main(String[] args) {         System.out.println(Th ...

  2. markdown 插入latex公式练习

    markdown 插入latex公式 $$公式$$表示行间公式,本来Tex中使用\(公式\)表示行内公式,但因为Markdown中\是转义字符,所以在Markdown中输入行内公式使用\\(公式\\) ...

  3. elasticsarch5.4集群安装

    越来越多的企业已经采用ELK解决方案来对其公司产生的日志进行分析,笔者最近着手在生产环境部署自己的ELK stack,本文介绍ELK中elasticsearch5.2集群的实现. 一.环境准备 1.系 ...

  4. mac下配置phonegap(cordova)5.1.1开发环境

    眼下最新的cordova的版本号是5.1.1,在mac下搭建开发环境过程例如以下: 1)首先安装NODEJS环境 进入官网: http://nodejs.org/ .眼下的版本号: v0.12.7 点 ...

  5. 推荐系统(1)--splitting approaches for context-aware recommendation

    开篇语: 大一的时候.在实验室老师和师兄的带领下.我開始接触推荐系统.时光匆匆,转眼已是大三,因为大三课甚是少.于是便有了时间将自己所学的东西做下总结. 第一篇博客.献给过去三年里带我飞的老师和师兄们 ...

  6. Hibernate 之 二级缓存

    在上篇文章中我们对缓存以及Hibernate的一级缓存进行了介绍,接下来的主要内容将是Hibernate的二级缓存. 二级缓存也称为进程级的缓存或SessionFactory级的缓存,二级缓存可以被所 ...

  7. high-level operations on files and collections of files

    11.10. shutil — High-level file operations — Python 3.6.5 documentation https://docs.python.org/3/li ...

  8. log4j_自定义样式参数意义

    #自定义样式 %c 输出所属的类目,通常就是所在类的全名 %C 输出Logger所在类的名称,通常就是所在类的全名 %d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比 ...

  9. 一些linux嵌入式资源下载地址

    linux内核源代码情景分析 非扫描版 上下册合订版 字清楚 带书签 1575页 pdfhttp://download.csdn.net/source/2002579***************** ...

  10. 安装APK报错解决方法【转】

    本文转载自:http://blog.csdn.net/zy1235678/article/details/38122827 adb install xxx.apk 报错,安装APK报错:INSTALL ...