ZOJ1610(经典线段树涂色问题)
Description
Your task is counting the segments of different colors you can see at last.
Input
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
If some color can't be seen, you shouldn't print it.
Print a blank line after every dataset.
Sample Input
Sample Output
1 1
0 2
1 1
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
struct node{
int r,l;
int color;
}a[MAXN*]; void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].color=-;//-1表示没有涂色
if(l==r) return ;
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
} void update(int rt,int l,int r,int val)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].color=val;
return ;
} if(a[rt].color>=)//color>=0表示子树均为一个颜色,lazy思想
{
a[rt<<].color=a[(rt<<)|].color=a[rt].color;
} int mid=(a[rt].l+a[rt].r)>>; if(r<=mid) update(rt<<,l,r,val);
else if(mid<l) update((rt<<)|,l,r,val);
else{
update(rt<<,l,mid,val);
update((rt<<)|,mid+,r,val);
}
a[rt].color=-;//-2表示子树为多种颜色
} int Color[MAXN];
int temp;
void query(int rt)
{
if(a[rt].color==-)
{
temp=-;
return ;
}
if(a[rt].color!=-)
{
if(temp!=a[rt].color)
{
Color[a[rt].color]++;
temp=a[rt].color;
}
return ;
} if(a[rt].l==a[rt].r) return ;
int mid=(a[rt].l+a[rt].r)>>;
query(rt<<);
query((rt<<)|);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int maxn=-;
temp=-;
memset(Color,,sizeof(Color));
build(,,);
while(n--)
{
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
update(,l+,r,c);
if(c>maxn) maxn=c;
}
query();
for(int i=;i<=maxn;i++)
{
if(Color[i]) printf("%d %d\n",i,Color[i]);
}
printf("\n");
} }
ZOJ1610(经典线段树涂色问题)的更多相关文章
- POJ2777(线段树涂色问题)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42828 Accepted: 12973 Des ...
- ZOJ - 1610 经典线段树染色问题
这个是一个经典线段树染色问题,不过题目给的是左右左右坐标,即[0,3]包含0-1这一段 1-2这一段 2-3这一段,和传统的染色不太一样,不过其实也不用太着急. 我们把左边的坐标+1,即可,那么[0, ...
- zoj1610(线段树)
题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 题意:在0-8000长的线段里面,按先后次序依次覆盖颜色, ...
- pku 2777(经典线段树染色问题)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41202 Accepted: 12458 Des ...
- 经典线段树 UVALive 3938/UVA 1400
题意:就是相当于动规里面的求最大连续子串,不同的是,这里需要读入一个区间x,y,输出的区间 a,b 且x<=a<=b<=y,使得a b的连续子串最长,而且询问次数达到了10的五次方. ...
- poj-2828 Buy Tickets(经典线段树)
/* Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10207 Accepted: 4919 Descr ...
- poj 2528 poster经典线段树+lazy+离散化
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; #def ...
- 【ACM/ICPC2013】线段树题目集合(一)
前言:前一段时间在网上找了一个线段树题目列表,我顺着做了一些,今天我把做过的整理一下.感觉自己对线段树了解的还不是很深,自己的算法能力还要加强.光练代码能力还是不够的,要多思考.向队友学习,向大牛学习 ...
- 树链剖分+线段树 HDOJ 5029 Relief grain(分配粮食)
题目链接 题意: 分粮食我就当成涂色了.有n个点的一棵树,在a到b的路上都涂上c颜色,颜色可重复叠加,问最后每一个点的最大颜色数量的颜色类型. 思路: 首先这题的输出是每一个点最后的情况,考虑离线做法 ...
随机推荐
- Powerdesigner Name与Comment的互相转换
使用说明: 在[Tools]-[Execute Commands]-[Edit/Run Script] 下.输入下面你要选择的语句即可: 1.Name填充Comment '把pd中那么name想自动添 ...
- VS2017生成类库选择Release失效的问题
VS的生成可以选择Debug模式或者Release模式,但是我发现在配置里面选择Release无效. 后来发现应该 在 生成->配置管理器 里面设置.
- php生成条形码: barcodegen
实例结构: 1. index.html <!DOCTYPE html> <html> <head> <title>Test with embedded ...
- Js自定义异常
function MyError(msg){ this.name="MyError"; this.message = msg || "自定义异常的默认消息";} ...
- Struts详解
1.什么是MVC? MVC是Model,View,Controller的缩写,MVC是Application开发的设计模式, 也就是大家所知道的Model2.在MVC的设计模式中,它包括三类对象:(1 ...
- NeurIPS2018: DropBlock: A regularization method for convolutional networks
NIPS 改名了!改成了neurips了... 深度神经网络在过参数化和使用大量噪声和正则化(如权重衰减和 dropout)进行训练时往往性能很好.dropout 广泛用于全连接层的正则化,但它对卷积 ...
- UML建模:学习笔记(1)
UML:学习笔记(1) 事物 结构事物 类: 接口: 协作:(定义元素之间的相互作用) 用例:(在系统外部和系统交互的人) 组件:(描述物理系统的一部分) 节点:(一个节点可以被定义为运行时存在的物理 ...
- session不能使用 ASP.NET MVC
在web.coonfig中添加 <sessionState mode=" /> 如: <system.web> //... <sessionState mode ...
- UI组件之Button
UIButton:按钮,可以实现用户和app的交互,父类是UIControl,事件驱动型的组件的父类都是UIControl.一般使用类方法创建一个对象,创建时指定button的类型, iOS7.0后采 ...
- 剑指offer——和为s的两个数字VS和为s的连续正数序列
两种方法都类似于快排的变形. #include <iostream> #include <string> using namespace std; bool FindNumbe ...