ZOJ1610_Count the Colors(段树/为段更新)
解决报告
意甲冠军:
一定长度8000段染。寻求染色完成后,。。
思路:
区间问题用线段树。成段的更新区间。最后把全部的区间下压到叶子结点,统计叶子结点的颜色。
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std;
int lz[32000],_hash[10000],color[10000],cnt;
void push_down(int rt)
{
if(lz[rt])
{
lz[rt*2]=lz[rt*2+1]=lz[rt];
lz[rt]=0;
}
}
void update(int rt,int l,int r,int ql,int qr,int v)
{
if(ql>r||qr<l)return ;
if(ql<=l&&r<=qr)
{
lz[rt]=v;
return ;
}
push_down(rt);
int mid=(l+r)/2;
update(rt*2,l,mid,ql,qr,v);
update(rt*2+1,mid+1,r,ql,qr,v);
}
void bin(int rt,int l,int r)
{
if(l==r)
{
color[cnt++]=lz[rt];
return ;
}
push_down(rt);
bin(rt*2,l,(l+r)/2);
bin(rt*2+1,(l+r)/2+1,r);
}
int main()
{
int n,m,i,j,ql,qr,a;
while(~scanf("%d",&n))
{
cnt=0;
memset(lz,0,sizeof(lz));
memset(_hash,0,sizeof(_hash));
m=8000;
int cmax=-1;
for(i=0; i<n; i++)
{
scanf("%d%d%d",&ql,&qr,&a);
update(1,1,m,ql+1,qr,a+1);
}
bin(1,1,m);
for(i=0; i<cnt;)
{
j=i+1;
_hash[color[i]]++;
while(color[j]==color[i]&&j<cnt)
j++;
i=j;
}
for(i=1; i<=m+1; i++)
{
if(_hash[i])
printf("%d %d\n",i-1,_hash[i]);
}
printf("\n");
}
return 0;
}
附手动随机数据
input:
10
2 4 6
4 6 2
1 9 3
4 6 2
1 20 3
2 4 3
6 7 1
3 7 9
4 6 9
2 6 4
10
43 54 8000
323 4342 123
234 2332 321
2 6 23
54 546 1
2843 8888 8000
3000 8000 0
23 4329 9
923 2323 8
2390 3293 1
10
1 34 8000
43 343 99
341 3414 8000
7999 8000 8000
344 345 1
434 3455 0
34 45 8000
43 56 45
56 64 0
898 4599 8000 output:
3 2
4 1
9 1 0 1
1 1
8 1
9 3
23 1 0 2
1 1
45 1
99 1
8000 5
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
ZOJ1610_Count the Colors(段树/为段更新)的更多相关文章
- ZOJ 1610 Count the Colors (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
- NYOJ 1068 ST(段树 为段更新+间隔总和)
ST 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描写叙述 "麻雀"lengdan用随机数生成了后台数据.可是笨笨的他被妹纸的问题给难住了. .. 已知 ...
- poj-3468-A Simple Problem with Integers-线段树入门+区间更新
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34522 Accepted: 16224 ...
- ccnu-线段树联系-单点更新2-B
B - 单点更新2 Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
- CodeForces 52C Circular RMQ(间隔周期段树,间隔更新,间隔总和)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://codeforces.com/problemset/problem/52/C You are g ...
- HDU 1698.Just a Hook-线段树(成段替换、输出总和tree[1])
HDU1698.Just a Hook 这个题是最最基础的成段更新的线段数的题目,直接贴代码吧. 代码: #include<iostream> #include<cstring> ...
- POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)
POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...
- POJ 3225.Help with Intervals-线段树(成段替换、区间异或、简单hash)
POJ3225.Help with Intervals 这个题就是对区间的各种操作,感觉这道题写的一点意思都没有,写到后面都不想写了,而且更神奇的是,自己的编译器连结果都输不出来,但是交上就过了,也是 ...
随机推荐
- PKI系统深入的介绍
公钥基础设施(Public Key Infrastructure,缩写PKI)的基础与核心.是电子商务安全实施的基本保障.因此.对PKI技术的研究和开发成为眼下信息安全领域的热点. 本文对PKI技术进 ...
- Android开发有用技巧:推断当前系统语言版本号
Locale locale = getResources().getConfiguration().locale; String language = locale.getLanguage(); 以上 ...
- fedora linux平台下搭建lighttpd+php+sqlite
(一)lighttpd 1. 安装 yum install lighttpd 安装完毕后,系统中应该多了一个用户lighttpd和组lighttpd.这个用户,默认是不同意登陆的. 我们改动/etc/ ...
- quartz.net持久化和集群
首先你应该使用的是持久化的quartz,所有定时任务的情况都是保存在数据库表总的,每次启动时,scheduler容器都是按照qrtz_triggers等表内存储的信息来执行定时任务(主要包括cron表 ...
- "Cannot find entry symbol nable-stdcll-fixup; defaulting to 00401000" 解决方案
在使用Qt 4.7.3, Qt 4.7.2 ,Qt 4.7.1(mingw 4.6.2 )的时候都会有一个问题无法编译通过,即用Qt Creator 编译的时候会发生一个错误 "Cannot ...
- Unix编程艺术札记版本
这本书是一位面试官告诉我,.非常感谢他的忠告.面试的说明.我写后认为,足.一方面是面试的技巧.另一方面就是学习的方法. 阅读这本书非常享受,加上之前的积累,一些疑惑,另一些基础的不足,在这本书里找 ...
- 《TCP/IP详细解释》札记(23章)-TCP该保活定时器
可能有这样的备用现实TCP连接:流通过. 也就是说.假设TCP连接的两方都没有向对方发送数据.则在两个TCP模块之间不交换不论什么信息,这意味着我们能够启动一个客户与server建立连接,然后长时间不 ...
- mybatis至mysql插入一个逗号包含值误差
mybatis至mysql插入形如"11,22,33"当误差.我使用了错误的原因是美元符号镶嵌sql.正确的做法是使用# 有时间去看看mybatis的$和#差异. 版权声明:本文 ...
- 【Android进阶】快捷图标的创建与移除
注释已经说得很清楚了,如果有疑问,请留言 /** * 添加桌面快捷方式 * * @param view */ public void click1(View view) { if (isExit()) ...
- Android开发之Handler的用法(源码分享)
Handler主要接受子线程发送的数据, 并用此数据配合主线程更新UI.. 当应用程序启动时.Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发. ...