ZOJ-1610 Count the Colors(线段树染色,求染色段)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
https://vjudge.net/contest/318019#problem/F
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
Sample Output
这题题意不太好懂
题目大意:
在范围[0,8000]内,给n个操作,把[x,y]段染成颜色c,注意是线段不是点,最后按颜色从小到大输出每种颜色连续的线段的个数,有颜色的才输出;
先给出我参考的博客
https://www.cnblogs.com/kuangbin/archive/2012/08/10/2631449.html
https://www.cnblogs.com/thunder-110/p/10302782.html
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
const int maxn=1e5+;
using namespace std;
//ios::sync_with_stdio(false);
// cin.tie(NULL); int n;
int last;//flag判断相邻两个区间的颜色是否相同
struct node
{
int l;
int r;
int color;
}SegTree[<<]; int ans[<<]; void PushDown(int rt)
{
if(SegTree[rt].color>)
{
SegTree[rt<<].color=SegTree[rt].color;
SegTree[rt<<|].color=SegTree[rt].color;
SegTree[rt].color=-;
}
} void Build(int l,int r,int rt)
{
SegTree[rt].l=l;
SegTree[rt].r=r;
SegTree[rt].color=;//多样例时必须加
if(l==r)
{
return;
}
int mid=(l+r)>>;
Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
} void Update(int L,int R,int C,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(L<=l&&R>=r)
{
SegTree[rt].color=C;
return ;
}
PushDown(rt);//向下更新lazy
int mid=(l+r)>>;
if(L<=mid)//当寻找一个区间的时候,路径上的值全改成C
Update(L,R,C,rt<<);
if(R>mid)//当寻找一个区间的时候,路径上的值全改成C
Update(L,R,C,rt<<|);
SegTree[rt].color=-;//寻找到了之后,把回头的路径全部改成-1,说明如果顺着这些点下来,一定能找到区间
} void Query(int rt)
{
if(last==SegTree[rt].color)
return;
if(SegTree[rt].color==)//一次也没有被涂过
{
last=;
return;
}
if(SegTree[rt].color>)
{
if(last!=SegTree[rt].color)//不是同一块海报
{
last=SegTree[rt].color;
ans[SegTree[rt].color]++;
}
return;
}
//接下来是如果SegTree[rt].color== -1 ,表示顺着这个点 一定能找到区间
Query(rt<<);
Query(rt<<|);
} int main()
{
while(~scanf("%d",&n))
{
memset(ans,,sizeof(ans));
Build(,,);
for(int i=;i<=n;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
Update(a,b-,c+,);
}
Query();
for(int i=;i<=;i++)
{
if(ans[i])
printf("%d %d\n",i-,ans[i]);
}
printf("\n");
}
return ;
}
暴力解法:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<stack>
#include<ctype.h>
#include<stdlib.h>
#include<map>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int M=;
int main()
{
int n,t[M];
while(~scanf("%d",&n))
{
memset(t,-,sizeof(t));
int j,i,a,b,c,max1=-,max2=-;
int ans[M]= {};
for(i=; i<n; i++)
{
scanf("%d %d %d",&a,&b,&c);
max1=max(max1,b);//记录染有颜色的最右端
max2=max(max2,c);//记录染颜色的最大数值 for(j=a+; j<=b; j++)//注意这里,题目中的说的是线段,所以我们每次不标记最左端
t[j]=c;
}
for(i=; i<max1; i++)
{
if(t[i]!=t[i+]&&t[i]!=-)
ans[t[i]]++;
}
ans[t[max1]]++;
for(i=; i<=max2; i++)
{
if(ans[i]>)
printf("%d %d\n",i,ans[i]);
}
printf("\n");
}
return ;
}
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 (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
- 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 【区间覆盖 求染色段】
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)
题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有n段被染色,问染 ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- Count the Colors(线段树,找颜色段条数)
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- 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——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
随机推荐
- 干货|微软远程桌面服务蠕虫漏洞(CVE-2019-1182)分析
2019年8月,微软发布了一套针对远程桌面服务的修复程序,其中包括两个关键的远程执行代码(RCE)漏洞,CVE-2019-1181和CVE-2019-1182.与之前修复的"BlueKeep ...
- css 网格线
白色网格线 background: #58a; background-image: linear-gradient(rgba(255,255,255,.3) 1px, transparent 0), ...
- 【转】pip升级不成功怎么办
python -m pip install --upgrade pip -i https://pypi.douban.com/simple
- Java SE 5.0(JDK 1.5)新特性
目录 自动装箱与拆箱 枚举(常用来设计单例模式) 静态导入static import 可变参数(Varargs) 内省(Introspector) 泛型(Generics) For-Each循环 ja ...
- 【无网条件下】Linux系统、jdk、redis及集群、rabbitmq、nginx、weblogic和oracle安装及配置
本篇文章为原创,仅供参考使用,如果需要文章中提到的所有软件安装包和依赖包(即data),请以博客园邮箱联系获取链接. 准备资料 软件 主要软件包版本 路径 系统镜像 CentOS-6.10-x86_6 ...
- python中的单元测试模块unittest
unittest的属性: 该文以思维导图的形式描述unittest的重要属性. 其中前四个是unittest最核心的三个属性. testcase:测试用例: testsuite:测试套件,多个测试用例 ...
- (转)绝对路径${pageContext.request.contextPath}用法及其与web.xml中Servlet的url-pattern匹配过程
以系统的一个“添加商品”的功能为例加以说明,系统页面为add.jsp,如图一所示: 图一 添加商品界面 系统的代码目录结构及add.jsp代码如图二所示: 图二 系统的代码目录结构及add.js ...
- PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...
- 01Java-方法
一:动手动脑 1.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数 package reserve; import java.util.Scanner; import java.ut ...
- js中要声明变量吗?
你好,js语言是弱类型语言,无需申明即可直接使用,默认是作为全局变量使用的.建议:在function里时应使用var 申明变量,这样改变量仅仅只在function的生存周期内存在,不会污染到,全局控件 ...