Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意:
有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 (线段树+区间更新+暴力计数)的更多相关文章
- 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 ...
随机推荐
- pip命令自动补全功能;设置代理;使用国内源
这是pip自带的功能 执行的脚本 把脚本写入.zshrc或者profile等里面,执行source立即生效 设置代理: pip --proxy=http://username:password@pro ...
- 使用HDP快速搭建Hadoop开发环境 | Debugo
本文简单记录了一下使用VMware workstation 10.CentOS和HDP 2.0.6(Hadoop 2.2)发行版构建Hadoop开发测试环境的全部流程.这个过程中我遇到了不少问题,也耽 ...
- 7.1 itertools--高效循环的创建函数
7. 函数式编程库 本库主要提供了支持函数式编程的函数和类,以及提供通用调用对象. 7.1 itertools--高效循环的创建函数 本模块主要提供了迭代器方面的操作函数,跟语言API.Haskell ...
- hdu1873 看病要排队(结构体优先队列)
看病要排队 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- C# LINQ Unity 单例
C# LINQ 1. 自定义 Master,Kongfu 类 1 class Master 2 { 3 4 public int Id { get; set; } 5 public string ...
- 【iOS系列】-iOS查看沙盒文件图文教程(真机+模拟器)
[iOS系列]-iOS查看沙盒文件图文教程(真机+模拟器) 1:模拟器 1.1 方法1: 程序中打印一下的地址,能直接前往沙盒路径. NSString *path = [NSSearchPathFor ...
- js实现加密(?!)
<script src="yourUrl/md5.min.js"></script> 或者: <script src="http://cdn ...
- ABAP 邮件
function zint_send_email.*"-------------------------------------------------------------------- ...
- HEX文件格式学习笔记
这也是一篇学习摘抄:原文地址:http://blog.csdn.net/syrchina/article/details/7004998 为了编写一个可以按照自己的要求进行ISP的程序, ...
- YTU 2955: A改错题--销售部的打印机
2955: A改错题--销售部的打印机 时间限制: 1 Sec 内存限制: 128 MB 提交: 61 解决: 47 题目描述 销售部新进了一台快速打印机,使用频率很高.为了能够对打印情况进行统计 ...