Count the Colors(线段树染色)
Count the Colors
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Description
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
线段树区间更新的变形
题意:
在一条长度为8000的线段上染色,每次把区间[a,b]染成c颜色。显然,后面染上去的颜色会覆盖掉之前的颜色。
求染完之后,每个颜色在线段上有多少个间断的区间。
用区间更新的方式,对于区间内的先不更新,当出现新的线段覆盖的时候在pushdown,mark[]遍历一下离根最近的color不为-1(为染色)的就行了,最后通过sum统计出从到大的就好了。
PS:开始的时候由于用来之前的部分代码,结果在pushdown的时候错误成了
tree[tmp].color += tree[x].color;
结果出了个Segmentation Fault 从来没有见过的错,结果是ZOJ越界(RE)的错误,查了好久。。。
//线段树区间更新
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define INF 0x3f3f3f3f
#define MAX 8010
#define LL long long
using namespace std; struct Tree
{
int l,r;
int color;
};
Tree tree[MAX*4]; void pushdown(LL x) ///用于更新color数组
{
LL tmp = x<<1 ;
tree[tmp].color = tree[x].color; ///由子节点通过增加
tree[tmp+1].color = tree[x].color;
tree[x].color=-1;
}
void build(int l,int r,int x)
{
tree[x].l=l , tree[x].r=r , tree[x].color=-1;
if(l==r) return ;
int tmp=x<<1;
int mid=(l+r)>>1;
build(l,mid,tmp);
build(mid+1,r,tmp+1);
} void update(int l,int r,int c,int x) ///分别表示区间的左 , 右 , 增加的值 ,当前父亲节点
{
if(r<tree[x].l||l>tree[x].r) return ;
if(l<=tree[x].l&&r>=tree[x].r) ///该区间为需要更新区间的子区间
{
tree[x].color = c;
return ;
}
if(tree[x].color!=-1) pushdown(x); ///更新从上向下更新color
update(l,r,c,x<<1);
update(l,r,c,(x<<1)+1);
} int mark[MAX<<2],coun=0; ///注意大小
void query(int l ,int r ,int x )
{
if((l==tree[x].l&&r==tree[x].r && tree[x].color!=-1) || tree[x].l == tree[x].r){
mark[coun++] = tree[x].color; ///要计算的区间包括了该区间
return ;
} LL tmp=x<<1;
LL mid=(tree[x].l+tree[x].r)>>1; if(r<=mid) return query(l,r,tmp);
else if(l>mid) return query(l,r,tmp+1);
else{
query(l,mid,tmp) ;
query(mid+1,r,tmp+1);
}
} int main()
{
int x1[MAX<<2],x2[MAX<<2],c[MAX<<2];
int sum[MAX<<2];
int mmax=-1,m;
while(~scanf("%d",&m))
{
coun = 0;
mmax=-1;
memset(mark,-1,sizeof(mark));
memset(sum,0,sizeof(sum));
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&x1[i],&x2[i],&c[i]);
if(mmax<x1[i]) mmax = x1[i];
if(mmax<x2[i]) mmax = x2[i];
}
build(1,mmax,1);
for( int i = 0;i<m; i++ ){
if(x1[i]+1>x2[i]) continue;
update(x1[i]+1,x2[i],c[i],1);
}
if(mmax<0) continue;
query(1,mmax,1);
for(int i=0;i<coun;){
if(mark[i]==-1){i++ ;continue;}
int x = mark[i];
while(x == mark[++i] && i<coun);
sum[x]++;
}
for(int i=0;i<8010;i++)
if(sum[i])
printf("%d %d\n",i,sum[i]);
printf("\n");
}
return 0;
}
Count the Colors(线段树染色)的更多相关文章
- [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)
题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有n段被染色,问染 ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
- POJ 2777 Count Color(线段树 + 染色问题)
传送门:Count Color Description Chosen Problem Solving and Program design as an optional course, you are ...
- 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 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
- 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 Colors(线段树,区间覆盖,单点查询)
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- ZOJ1610 Count the Colors —— 线段树 区间染色
题目链接:https://vjudge.net/problem/ZOJ-1610 Painting some colored segments on a line, some previously p ...
- ZOJ-1610 Count the Colors ( 线段树 )
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Description Painting some co ...
- Count the Colors 线段树
题目 参考博客地址 题意: n范围[1,8000] , li 和 ri 的范围[0,8000]. n个操作,每个操作是把 [li , ri]内的点修改成一个颜色c. n个操作过后,按颜色从小到大 ...
随机推荐
- Hadoop数据传输工具:Sqoop
Apache Sqoop(SQL-to-Hadoop) 项目旨在协助 RDBMS 与 Hadoop 之间进行高效的大数据交流.用户可以在 Sqoop 的帮助下,轻松地把关系型数据库的数据导入到 Had ...
- 轻量级开源内存数据库SQLite性能测试
[IT168 专稿]SQLite是一款轻型的数据库,它占用资源非常的低,同时能够跟很多程序语言相结合,但是支持的SQL语句不会逊色于其他开源数据库.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品 ...
- 剑指offer系列55---最小的k个数
[题目] 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. *[思路]排序,去除k后的数. package com.exe11 ...
- 重绘Winform窗体
本文转载自:http://www.cnblogs.com/encoding/p/5603080.html 按照惯例,先来几张样例图(注:为了展示窗口阴影效果,截图范围向外扩展了些,各位凭想象吧). 还 ...
- 51nod 1411 矩阵取数问题 V3
给定一个m行n列的矩阵,你可以从任意位置开始取数,到达任意位置都可以结束,每次可以走到的数是当前这个数上下左右的邻居之一,唯一的限制是每个位置只能经过一次,也就是说你的路径不自交.所经过的数的总作为你 ...
- Oracle11g中Exp命令空表不能导出的问题
http://www.jb51.net/article/43894.htm 一.不能导出空表的原因 1.Oracle11g默认对空表不分配segment,故使用exp导出Oracle11g数据库时,空 ...
- [系统开发] FileMaker进销存系统
一.简介 这是我用 FileMaker 编写的进销存系统: FileMaker 是一种在欧美流行的桌面型数据库:它使用非常方便,功能也很强大,用户可以在它上面开发自己的系统: 开发时间:2008年 二 ...
- 3. c的输入输出
putchar与getchar操作输入输出通道 #include <stdio.h> #include <ctype.h> main(){ int c; while((c = ...
- JavaScript正则详谈
JavaScript RegExp 基础详谈 前言: 正则对于一个码农来说是最基础的了,而且在博客园中,发表关于讲解正则表达式的技术文章,更是数不胜数,各有各的优点,但是就是这种很基础的东西,如果 ...
- 避免HTML5六种错误用法
一.不要使用section作为div的替代品 人们在标签使用中最常见到的错误之一就是随意将HTML5的<section>等价于<div>--具体地说,就是直接用作替代品(用于样 ...