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 ...
随机推荐
- VUE.js入门学习(1)-起步
1.hello world <div id="app">{{content}}</div>var app = new Vue({ el:'#app', da ...
- 越南CTF的crypto 100
自己做CTF还是没有经验,本来以为crypto更多应该是python编程的,结果这个100的题目是Do you love Arithmetic? 打开文件来看内容是 # charset = " ...
- java-正则表达式判断移动联通电信手机号
package com.linbilin.phone; import java.util.regex.Matcher; import java.util.regex.Pattern; public c ...
- CSS(3)之 less 和rem
less 预编译脚本语言. LESS 语法 less语法2 LESS中文 rem rem的适配原理 rem 是相对于页面根源素html的字体大小的一个尺寸单位 页面内容可以使用rem为单位,那么htm ...
- CSU 1425 NUDT校赛 I题 Prime Summation
这个题本来有希望在比赛里面出了的 当时也想着用递推 因为后面的数明显是由前面的推过来的 但是在计算的时候 因为判重的问题 ...很无语.我打算用一个tot[i]来存i的总种树,tot[i]+=tot[ ...
- macOS下的播放器
很早前用 MplayerX, 现在不能用了, 找到一个替代品 https://iina.io/. 挺不错.
- mysql分组和排序操作
分组.排序操作 sele ...
- python Mysql数据库连接池组件封装(转载)
以前一直在用Java来开发,数据库连接池等都是有组件封装好的,直接使用即可,最近在尝试Python的学习,碰到了和数据库打交道的问题,和数据库打交道我们都知道,数据库连接池必不可少,不然要么就是程序异 ...
- 光纤卡网卡的区别以及HBA的常规定义-----引自百度百科
在讨论这个问题的时候,需要先说清楚一个问题:我们知道,在早期的SAN存储系统中,服务器与交换机的数据传输是通过光纤进行的,因为服务器是把SCSI指令传输到存储设备上,不能走普通LAN网的IP协议,所以 ...
- frp内网穿透,centos7+frp成功样例
准备工作: 阿里云服务器一台,备案域名一个,本地服务器一台(本人用的虚拟机centos7) frp文件:frp_0.22.0_linux_amd64.tar.gz 链接:https://pan.bai ...