Count the Colors

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

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就是树的最大范围了,然后一直报段错误(RE),后来发现maxn才是,解决后又wa了几次。还有就是(2,3,0)和(4,5,0)其实是两段,需要注意。
 
解题思路:区间替换、用线段树平常意义上的点代替长度为1的单位线段,统计段数。这里用到线段树只是为了保存哪些线段是什么颜色的,树中保存了一段一段的颜色,我们需要通过一次询问,把树上的颜色段拿出来,然后放在一条线上,某些区间什么颜色,这就已经不再跟线段树有关系了,就是平常的统计了。
 
#include<bits/stdc++.h>
using namespace std;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 10000;
int colseg[maxn];
int cnt[maxn];
struct SegTree{
int col;
}segs[maxn*4];
void buildtree(int rt,int L,int R){
segs[rt].col = 0;
if(L == R){
return;
}
buildtree(lson);
buildtree(rson);
}
void PushDown(int rt){
if(segs[rt].col){
segs[rt*2].col = segs[rt*2+1].col = segs[rt].col;
segs[rt].col = 0;
}
}
void Update(int rt,int L,int R,int l_ran,int r_ran,int _col){
if(l_ran <= L && R <= r_ran){
segs[rt].col = _col;
return;
}
PushDown(rt);
if(l_ran <= mid){
Update(lson,l_ran,r_ran,_col);
}
if(r_ran > mid){
Update(rson,l_ran,r_ran,_col);
}
}
void query(int rt,int L,int R){ //把颜色段从树中拿出来,放入colseg数组中
if(segs[rt].col){
for(int i = L; i <= R; i++){
colseg[i] = segs[rt].col;
}
segs[rt].col = 0;
return ;
}
if(L == R) return;
query(lson);
query(rson);
}
int main(){
int n, m;
n = 8001;
while(scanf("%d",&m)!=EOF){
memset(cnt,0,sizeof(cnt));
memset(colseg,0,sizeof(colseg));
buildtree(1,1,n);
int u, v, w;
for(int i = 1; i <= m; i++){
scanf("%d%d%d",&u,&v,&w);
w++;
u++; //把点都看成线段
Update(1,1,n,u,v,w);
}
query(1,1,n);
for(int i = 1; i <= n+10; i++){
if(!colseg[i]) continue;
int j;
for(j = i; j <= n+10 && colseg[i]==colseg[j]; j++) ;
cnt[colseg[i]]++;
i = j - 1; //这里的点都是代表长度为1的单位线段
}
for(int i = 1; i <= n+10; i++){
if(cnt[i]){
printf("%d %d\n",i-1,cnt[i]);
}
}puts("");
}
return 0;
}

  

 
 

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 Colors(线段树,区间覆盖,单点查询)

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

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

    题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...

  4. ZOJ 1610 Count the Color(线段树区间更新)

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. WebStrom-JS编程小技巧

    快速打印某个名为***的对象:***.log回车效果如下:

  2. dos窗口运行java文件需要jar依赖

    执行java文件时候,有些里面依赖了java之外的jar,这是识别不到的,运行java命令的时候,带上jar路径: java -cp .;jar路径(加上jar名字)  java文件名 例:java ...

  3. 关于Java中SQL语句的拼接规则

    实现目标语句是这个注意,这里的java变量是idd int idd; String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEE ...

  4. 有关git的使用,和git的一些提交冲突。

    git 的一些基本用法 git init :初始化文件(创建文件夹). git add . :监控工作区的状态树(将被修改的文件提交到暂存区) git status :未跟踪状态(Untracked) ...

  5. ubtuntu 如何查看内存用量 mongostat详解

    free -h top free或者top或者cat /proc/meminfo mongostat是mongdb自带的状态检测工具,在命令行下使用.它会间隔固定时间获取mongodb的当前运行状态, ...

  6. Reviewing notes 2.1 of Mathematical analysis

    Chapter2 Numerical sequence and function Cartesian product set If S and T are sets,then the cartesia ...

  7. SDUT OJ 数据结构实验之链表八:Farey序列

    数据结构实验之链表八:Farey序列 Time Limit: 10 ms Memory Limit: 600 KiB Submit Statistic Discuss Problem Descript ...

  8. Domoticz 接入苹果的 HomeKit 实现 Siri 控制

    前言 接上次的折腾,这次尝试将 Domoticz 接入到苹果的 HomeKit,也就是在 iPhone 的 Siri 中可以语音控制.参考官方文档 步骤 安装 nodejs curl -sL http ...

  9. centos上安装theano和Lasagne

    1.安装theano所需的包 sudo yum install python-devel python-nose python-setuptools gcc gcc-gfortran gcc-c++ ...

  10. UESTC - 1147 求最短路方案数

    这道题很是说明了记忆化搜索的重要性 瞎bfs递推半天发现没卵用(也许是姿势不对,但我认为树形或图形dfs明显好写并且很好正确地递推) 参考了别人的写法,总感觉自己的实现能力太弱了 还有题目是1e9+9 ...