Count the Colors


Time Limit: 2 Seconds     
Memory Limit: 65536 KB


Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.

Your task is counting the segments of different colors you can see at last.

Input



The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:



x1 x2 c



x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.

All the numbers are in the range [0, 8000], and they are all integers.

Input may contain several data set, process to the end of file.

Output



Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.

If some color can't be seen, you shouldn't print it.

Print a blank line after every dataset.

Sample Input



5

0 4 4

0 3 1

3 4 2

0 2 2

0 2 3

4

0 1 1

3 4 1

1 3 2

1 3 1

6

0 1 0

1 2 1

2 3 1

1 2 0

2 3 0

1 2 1

Sample Output



1 1

2 1

3 1

1 1

0 2

1 1


Author: Standlove

Source: ZOJ Monthly, May 2003





题意:在一条长度为8000的线段上染色,每次把区间[a,b]染成c颜色。

显然,后面染上去的颜色会覆盖掉之前的颜色。求染完之后,每种颜色在线段上有多少个间断的区间。

线段树Lazy区间改动。

让我无语的是——输出要求是按颜色编号从0到8000输出的。

而我傻傻的按插入顺序输出了1个小时。。

。o(╯□╰)o



AC代码:


#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 8000+10
using namespace std;
int color[MAXN<<2];//对节点染色
void PushDown(int o)
{
if(color[o] != -1)
{
color[o<<1] = color[o<<1|1] = color[o];
color[o] = -1;
}
}
void update(int o, int l, int r, int L, int R, int v)
{
if(L <= l && R >= r)
{
color[o] = v;
return ;
}
PushDown(o);
int mid = (l + r) >> 1;
if(L <= mid)
update(o<<1, l, mid, L, R, v);
if(R > mid)
update(o<<1|1, mid+1, r, L, R, v);
}
int rec[MAXN];//存储i节点的颜色
int top = 0;
void query(int o, int l, int r)
{
if(l == r)
{
rec[top++] = color[o];//记录节点的颜色
return ;
}
int mid = (l + r) >> 1;
PushDown(o);
query(o<<1, l, mid);
query(o<<1|1, mid+1, r);
}
int num[MAXN];//记录颜色出现的 段数
int main()
{
int N;
while(scanf("%d", &N) != EOF)
{
memset(color, -1, sizeof(color));
int a, b, c;
for(int i = 1; i <= N; i++)
{
scanf("%d%d%d", &a, &b, &c);
update(1, 1, 8000, a+1, b, c);//相应颜色数 先加一
}
memset(rec, -1, sizeof(rec));
top = 0;//初始化
query(1, 1, 8000);
memset(num, 0, sizeof(num));//初始化
int i, j;
for(i = 0; i < top;)
{
if(rec[i] == -1)
{
i++;
continue;
}
num[rec[i]]++;
for(j = i + 1; j < top; j++)
{
if(rec[j] != rec[i] || rec[j] == -1)
break;
}
i = j;
}
for(int i = 0; i <= 8000; i++)
{
if(num[i])
printf("%d %d\n", i, num[i]);
}
printf("\n");
}
return 0;
}

直接模拟也能够过:


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (10000+10)
#define MAXM (50000000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int num[MAXN];
int color[MAXN];
int main()
{
int n;
while(Ri(n) != EOF)
{
int Max = -INF;
CLR(color, -1);
for(int i = 0; i < n; i++)
{
int x, y, c;
Ri(x); Ri(y); Ri(c);
Max = max(Max, c);
for(int j = x; j < y; j++)
color[j] = c;
}
CLR(num, 0);
for(int i = 0; i <= 8000; )
{
int temp = color[i];
if(temp == -1)
{
i++;
continue;
}
i++;
while(temp == color[i])
i++;
num[temp]++;
}
for(int i = 0; i <= Max; i++)
if(num[i])
printf("%d %d\n", i, num[i]);
printf("\n");
}
return 0;
}

zoj 1610 Count the Colors 【区间覆盖 求染色段】的更多相关文章

  1. ZOJ 1610 Count the Colors(区间染色)

    题目大意:多组数据,每组给一个n(1=<n<=8000),下面有n行,每行有l,r,color(1=<color<=8000),表示将l~r颜色变为color,最后求各种颜色( ...

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

    题目链接 题意:成段染色,初始为0,每次改变一个区间的颜色,求最后每种颜色分别有多少段.颜色按照从 小到大输出. 分析:改变了代码的风格,因为看了学长的博客.直接用数组,可以只是记录节点的编号,因为节 ...

  3. ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

    任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...

  4. ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】

    Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

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

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

  6. ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

    1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...

  7. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

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

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

  9. zoj 1610 Count the Colors

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610  Count the Colors Time Limit:2000MS   ...

随机推荐

  1. docloud后台管理项目(开篇)

    最近朋友做app需要web做后台管理,所以花了一周时间做了这个项目. 废话不多说,开发环境是nginx+php5.3,使用thinkphp框架.是一个医疗器械数据统计的后台,业务功能很简单就是查看用户 ...

  2. 17Web应用乱码问题

    Web应用乱码问题 Web应用乱码问题 简介 每个国家(或区域)都规定了本国家(或地区)计算机信息交换用的字符编码集,如美国的扩展ASCII码, 中国的GB2312-80,日本的JIS 等,作为该国家 ...

  3. java jvm eclipse 性能调优

    低配配置 -Dfile.encoding=UTF-8-Xms960m-Xmx960m-Xmn384m-Xverify:none-Xss256k-XX:MaxTenuringThreshold=2-XX ...

  4. kvm--virsh命令行下管理虚拟机

    virsh 既有命令行模式,也有交互模式,在命令行直接输入 virsh 就进入交互模式, virsh 后面跟命令参数,则是命令行模式: (1)基础操作 --- 命令行下管理虚拟机 virsh list ...

  5. 微服务网关从零搭建——(一)创建测试api以及api自动注入consul

    本系列编写目的纯属个人开发记录  以下代码均为demo级 如有需要 请自行优化 代码完整包由于公司电脑加密 无法上传整包的demo文件 consul 开发环境简易处理 consul 下载地址 : ht ...

  6. 洛谷——P2734 游戏 A Game

    P2734 游戏 A Game 题目背景 有如下一个双人游戏:N(2 <= N <= 100)个正整数的序列放在一个游戏平台上,游戏由玩家1开始,两人轮流从序列的任意一端取一个数,取数后该 ...

  7. 关于React.PropTypes的废除,以及新版本下的react的验证方式

    React.PropTypes是React用来typechecking的一个属性.要在组件的props上运行typechecking,可以分配特殊的propTypes属性: class Greetin ...

  8. nginx 4 win10

    去下载文件 http://nginx.org/en/download.html 然后释放文件到一目录 最后执行nginx.exe.到浏览器查看localhost,界面: 在最后,别忘了,修改其80端口 ...

  9. unigui导出TMS.Flexcel【5】

    参考代码 procedure TUniFrmeWebEmbedBase.ExportData; //导出到excel var FlexCelImport1: TExcelFile; i, rowind ...

  10. 【12】AngularJS 事件

    AngularJS 事件 AngularJS 有自己的 HTML 事件指令. ng-click 指令 ng-click 指令定义了 AngularJS 点击事件. <div ng-app=&qu ...