题目大意:

  有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. ArcGIS Engine中的Symbols详解

    转自原文ArcGIS Engine中的Symbols详解 本文由本人翻译ESRI官方帮助文档.尊重劳动成果,转载请注明来源. Symbols ArcObjects用了三种类型的Symbol(符号样式) ...

  2. Win7中你所应该知道的强制计划关机操作

    有时候更新系统补丁时,需要很长时间,为了能让电脑在你晚上睡觉后,扔然能做些枯燥费时类的这种工作,你可以用到强制计划关机.cmd命令是: shutdown -f -s -t 3600 上面的意思是,强制 ...

  3. maven的超级pom

    对于 Maven3,超级 POM 在文件 %MAVEN_HOME%/lib/maven-model-builder-x.x.x.jar 中的 org/apache/maven/model/pom-4. ...

  4. BZOJ 1055 HAOI2008 玩具取名 动态规划

    题目大意:给定一个由'W','I','N','G'构成的字符串.给定一些规则.这些规则能够将两个字符合成为一个,比如"II"能够合成为'W',"WW"能够合成为 ...

  5. HDU 1201 18岁生日 【日期】

    18岁生日 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  6. #define中的#和##作用

    #define语句中的#是把参数字符串化,##是连接两个参数成为一个整体. #define FACTORY_REF(name) { #name, Make##name } 中#name就是将传入的na ...

  7. MongoDB中对象反序列化的一个小问题

    今天在mongoDB存取对象数据的时候,碰到一个小问题:对象的某一个字段类型是抽象类或者接口.在存入的时候没有问题.可是在读取的时候,因为没有详细类的信息,无法完毕对象的又一次构建.就会报错: Can ...

  8. 全局最小割模版 n^3

    //点标从0-n-1, 開始时先init 复杂度n^3 //对于边(u,v,flow): //g[u][v]+=flow; //g[v][u]+=flow; typedef long long ll; ...

  9. 【iOS系列】-iOS开发常用库文件总结

    这里是列举出得一部分,更多内容可参考https://github.com/darren90/Gather_iOS 码农周刊的总结 - 覆盖很广 调调的 - 很多开发相关内容都有体现 右滑返回的解决 - ...

  10. 【iOS系列】-UIWebView加载网页禁止左右滑动

    [iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...