题目大意:

  有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. java大文件的分割和合并

    原文:http://www.open-open.com/code/view/1441679313430 import java.io.File; import java.io.FileInputStr ...

  2. B/S(WEB)系统中使用Activex插件调用扫描仪实现连续扫描并上传图像(IE文件扫描并自动上传)

    IE浏览器下使用Activex插件调用客户端扫描仪扫描文件并山传,可以将纸质档案(如合同.文件.资料等)扫描并将扫描图像保存到服务器,可以用于合同管理.档案管理等. 通过插件方式调用扫描仪扫描并获取图 ...

  3. kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置

    KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51 ...

  4. 关于Android Service真正的全然具体解释,你须要知道的一切

    转载请注明出处(万分感谢! ): http://blog.csdn.net/javazejian/article/details/52709857 出自[zejian的博客]   Service全部内 ...

  5. POJ 1436 Horizontally Visible Segments(线段树)

    POJ 1436 Horizontally Visible Segments 题目链接 线段树处理染色问题,把线段排序.从左往右扫描处理出每一个线段能看到的右边的线段,然后利用bitset维护枚举两个 ...

  6. axis client tomcat jsp调用最少jar

    tomcat调用和main方法调用不同在于引入的jar不一样.tomcat引入的jar是全部lib以下的jar包,main是project引入的jar.假设直接进行公布lib下的全部jar都会引入到p ...

  7. cocos2d-x CCSrollView 源代码,可循环的SrollView代码

    项目须要.写一个类似于iPhone上面时钟选择的可拉动式循环选择列表,通过集成CCScrollView并更改部分代码.实现了该功能. 假设想充分了解代码,请先阅读源码分析http://blog.csd ...

  8. icvSetWeightsAndClasses

    /* *icvSetWeightsAndClasses *作用:给训练样本的权重和类别赋值 */ static void icvSetWeightsAndClasses( CvHaarTraining ...

  9. 3.2.1 配置构建Angular应用——简单的笔记存储应用——展示功能

    本节我们会通过构建一个简单的笔记存储应用(可以载入并修改一组简单的笔记)来学习如何应用Angular的特性.这个应用用到的特性有: 在JSON文件中存储笔记 展示.创建.修改和删除笔记 在笔记中使用M ...

  10. cmd下复制粘贴

    cmd下复制粘贴的快捷操作方式 工具/原料 系统cmd 步骤/方法 1 如右图,右键命令提示符窗口的标题栏,选择属性. 2 选择“编辑选项”里的“快速编辑模式”,并确定之: 3 在弹出的应用选择提示框 ...