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 ...
随机推荐
- mysql初始化出现:FATAL ERROR: Neither host 'DB01' nor 'localhost' could be looked up with
初始化时: FATAL ERROR: Neither host 'DB01' nor 'localhost' could be looked up with /application/mysql/bi ...
- Vuex 是什么
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vue 的官方调试工具 ...
- h5页面乱码-设置编码
1.h5页面正常,重定向以后出现乱码,如图所示. 解决办法:重定向的时候 需要设置编码. 2.文件charset已经是utf-8,页面还是乱码,文件保存的格式也要是utf-8的哦
- MySql 的操作指令(window)
1.登录: mysql -uroot -p 2.查看所有数据库: show databases 3.切换数据库 : use databasename(数据库名称) 4.查看数据库的所有表格 ...
- 多线程之间通讯JDK1.5-Lock
synchronized:代码开始上锁,代码结束时释放锁:内置锁.自动化的.效率低.扩展性不高(不够灵活): JDK1.5并发包Lock锁 --保证线程安全问题,属于手动挡,手动开始上锁,手动释放锁, ...
- Delphi流的操作_文件合并
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- linux 替换jdk指定jar包
我的bug是:jdk1.8的安全策略和腾讯邮箱服务有冲突.我知道的解决方法: 1更换低版本安全策略相关的jar包.(windows:http://www.cnblogs.com/dennyzhangd ...
- POJ 3321 Apple Tree 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
- Nginx无法监听虚拟VIP的问题报:99: Cannot assign requested address
99: Cannot assign requested address #本地网卡上没有10.0.0.3这个IPNginx就会报错: [root@lb01 conf]# /application/ng ...
- proto3 不支持内建类型的非空判断即 hasXXX
proto3 移除了内建类型的非空判断方法 即代码生成工具不会为 bool int 等类型生成has方法 有使用过proto2 或者其它rpc 框架的人都知道使用has 方法去判断消息里的值是否设置, ...