任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610

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

题意概括:

多测试样例,每个有 N 次操作,对区间 [a, b] 涂色,是涂区间是涂区间,不是涂区间的点!

是很别扭,举个栗子:涂 1-2 和 3-4,如果是涂点那么 1、2、3、4都涂了,如果是涂区间那么 2-3 区间没有涂。

解题思路:

线段树更新修改区间(把原来的数组看成一个点,两点之间的差是区间),暴力单点查询每点的颜色值,统计每种可以看得见的颜色有几段。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long int
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
using namespace std;
const int MAXN = ;
struct date{
int L, R, val;
}node[MAXN];
int lyt[MAXN<<];
int cnt[MAXN];
int N, max_R, max_C; void PushDown(int root)
{
if(lyt[root] >= ){
lyt[root<<] = lyt[root<<|] = lyt[root];
lyt[root] = -;
}
}
void Update(int L, int R, int l, int r, int root, int val)
{
if(L <= l && r <= R){lyt[root] = val;return;}
int mid = (l+r)>>;
PushDown(root);
if(L <= mid) Update(L, R, lson, val);
if(R > mid) Update(L, R, rson, val);
}
int Query(int pos, int l, int r, int root)
{
if(l == r) return lyt[root];
int ret;
PushDown(root);
int mid = (l+r)>>;
if(pos <= mid) ret = Query(pos, lson);
if(pos > mid) ret = Query(pos, rson);
return ret;
}
void init()
{
max_R = ; max_C = ;
memset(cnt, , sizeof(cnt));
memset(lyt, -, sizeof(lyt));
}
int main()
{
while(~scanf("%d", &N)){
init(); //初始化
for(int i = ; i < N; i++){ //输入
scanf("%d%d%d", &node[i].L, &node[i].R, &node[i].val);
node[i].L++;
max_R = max(node[i].R, max_R);
max_C = max(max_C, node[i].val);
}
for(int i = ; i < N; i++){ //建树
if(node[i].R >= node[i].L)
Update(node[i].L, node[i].R, , max_R, , node[i].val);
}
int last_color=-, now_color;
for(int i = ; i <= max_R; i++)
{
now_color = Query(i, , max_R, );
if(now_color == -){last_color = -; continue;} ///没有染色
if(now_color != last_color) cnt[now_color]++; ///该颜色断层
last_color = now_color;
}
for(int i = ; i <= max_C; i++){
if(cnt[i]>) printf("%d %d\n", i, cnt[i]);
}
puts("");
}
return ;
}

ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】的更多相关文章

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

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

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

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

  3. ZOJ 1610 Count the Colors (线段树区间更新与统计)

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

  4. Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)

    题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...

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

    https://cn.vjudge.net/problem/ZOJ-1610 题意 给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000. ...

  6. zoj 1610 Count the Colors(线段树延迟更新)

    所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...

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

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

  8. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  9. ZOJ 1610 Count the Colors 【线段树】

    <题目链接> 题目大意: 在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每 ...

随机推荐

  1. linux 输出 之 nl 命令

    1.命令格式:nl [选项]... [文件]... 2.命令参数: -b  :指定行号指定的方式,主要有两种: -b a :表示不论是否为空行,也同样列出行号(类似 cat -n): -b t :如果 ...

  2. shell基础优化脚本

    #!/bin/bash ######################################################### #Created Time: Tue Aug 7 01:29 ...

  3. Docker的镜像迁移

    [root@localhost ~]# mkdir /opt/soft/ [root@localhost ~]# docker save c3987965c15d > /opt/soft/pos ...

  4. CF 304B——Calendar——————【年月日计算】

    B - Calendar Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  5. PowerDesigner从SqlServer数据库导入数据模型

    Powerdesigner 从数据库导入数据 第一步, 打开 powerdesigner, 在菜单选择 [File] 选项, 然后在弹出的下拉单中选择 [Reverse Engineer]选项,再选择 ...

  6. css-css简介

    CSS:层叠样式表 ** 层叠:一层一层的 ** 样式表:很多的属性和属性值 * 使页面显示效果更好 * CSS将网页内容和显示样式进行分离,提高了显示功能.

  7. vue+axios+easy-mock+element-ui实现表格分页功能

    废话不多,效果如图: LineSource.vue文件内代码如下: <template> <div class="c-main auth userControl" ...

  8. 转:ITopologicalOperator Buffer调用异常的解决方法(来源网络)

      /// <summary>   /// 用拓扑分析求出缓冲区范围.   /// 由于ArcGIS的问题,有时调用会出异常,因此需要循环调用   /// </summary> ...

  9. Android 自定义View实现SegmentControlView(自定义多样式tablayout)

    偷懒一下,不做过多阐述 参考资源: Android 自定义View实现SegmentControlView : https://blog.csdn.net/a512337862/article/det ...

  10. mysql的sql_mode介绍和修改

    原文链接: http://blog.csdn.net/wulantian/article/details/8905573   mysql目录下有一个配置文件my.conf. mysql数据库有一个环境 ...