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 这个题就是对区间的各种操作,感觉这道题写的一点意思都没有,写到后面都不想写了,而且更神奇的是,自己的编译器连结果都输不出来,但是交上就过了,也是 ...
随机推荐
- AutoFac使用方法总结:Part III
生命周期 AutoFac中的生命周期概念非常重要,AutoFac也提供了强大的生命周期管理的能力. AutoFac定义了三种生命周期: Per Dependency Single Instance P ...
- Android -- Looper.prepare()和Looper.loop() —深度版
Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理.handler事实上能够看做是一个工具类.用来向消息队列中插入消息的. (1) Loop ...
- Java 大数类
划分结果存在数组.供应商下标0 在剩下的标记1 import java.math.BigInteger; import java.util.Scanner; public class Main { p ...
- Pagination jquery ajax 分页参考资料
http://www.zhangxinxu.com/wordpress/2010/01/jquery-pagination-ajax%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB% ...
- How to install PL/SQL developer on linux (转)
PL/SQL developer 在linux上的安装方法工欲善其事必先利其器,PL/SQL和toad对于ORACLE从业人员来说都是很重要的工具,但这些工具都没有linux的发行版,如果要在linu ...
- Web API Test Client 1.2.0
使用方法 1 安装 matthewcv.WebApiTestClient 到你的Web API 项目 PM> Install-Package matthewcv.WebApiTestClient ...
- 【JAVA】【NIO】3、Java NIO Channel
Java NIO和流量相似,但有些差异: ·通道可读写,流仅支持单向.读或写 ·异步通道读取 ·通道读写器,他们是和Buffer交替 道的实现 下面是Java NIO中最重要的通道的实现: ·File ...
- C利用宏语言(#,##,do…while(0)盛大)
C利用宏语言(#,##.do-while(0)盛大) 1.使用宏预先定义__FILE__,__FUNCTION__.__LINE__. #include <stdio.h> void fu ...
- 证明 poj 1014 模优化修剪,部分递归 有错误
这个问题是存在做.我发现即使是可行的一个问题,但不一定正确. 大部分数据疲软,因为主题. id=1014">poj 1014 Dividing 题目大意:有6堆石头,权重分别为1 2 ...
- MVC5
MVC5 不知不觉 又逢年底, 穷的钞票 所剩无几. 朋友圈里 各种装逼, 抹抹眼泪 MVC 继续走起.. 本系列纯属学习笔记,如果哪里有错误或遗漏的地方,希望大家高调指出,当然,我肯定不会低调改正的 ...