ZOJ 1610——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
#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——————【线段树区间替换、求不同颜色区间段数】的更多相关文章
- 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 ...
- ZOJ 1610 Count the Colors (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)
1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...
- Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
随机推荐
- C#中实现UrlEncode和UrlDecode
有时需要进行url编码.解码,比如从html中捞数据,有可能>.&等字符会被编码成>等. WinForm中默认没有引入System.Web,因此要现在项目中引入依赖 System. ...
- 「POJ 2182」 Lost Cows
题目链接 戳这 题目大意 \(N(2 <= N <= 8,000)\)头奶牛有\(1..N\)范围内的独特品牌.对于每头排队的牛,知道排在那头牛之前的并比那头牛的品牌小的奶牛数目.根据这些 ...
- HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories
Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262 ...
- 在请求中存取属性setAttribute&getAttribute方法
在请求中保存属性: public void setAttribute(String name,Object o) request.setAttribute("mess"," ...
- Azure KUDU工具
Azure网站提供了一个比较不错可以用来对我们的网站进行分析的工具------KUDU,下面我们就来看看这个工具主要能为我们做些啥,啥时候使用它. 如何打开KUDU KUDU所展现的强大功能 如何打开 ...
- Hadoop文章
hadoop入门--简单的MapReduce案例:https://blog.csdn.net/zhangt85/article/details/42077281?utm_source=blogxgwz ...
- Java ExecutorService 四种线程池
1.new Thread的弊端 new Thead(new Runnable(){ @Override public void run() { // TODO Auto-generated metho ...
- 【转】nginx在Windows系统启动不了
这几天用到Nginx,第一次是win7系统下部署,一次性成功,第二次在win10系统下,部署失败. 出现的情况: 打开Nginx.exe,界面一闪而过,而且进程里面搜不到Nginx. 1.端口占用问题 ...
- C++_类继承7-类设计回顾
编译器生成的成员函数 编译器会自动生成一些公有的成员函数——特殊成员函数. 1. 默认构造函数 提供构造函数的动机之一是确保对象总能被正确地初始化.如果类包含指针成员,则必须初始化这些成员.最好提供一 ...
- [FJOI2017]矩阵填数
[Luogu3813] [LOJ2280] 写得很好的题解 \(1.\)离散化出每一块内部不互相影响的块 \(2.\)\(dp[i][j]\)为前 \(i\) 种重叠块其中有 \(j\) 这些状态的矩 ...