ZOJ 1610 Count the Colors(区间染色)
题目大意:多组数据,每组给一个n(1=<n<=8000),下面有n行,每行有l,r,color(1=<color<=8000),表示将l~r颜色变为color,最后求各种颜色(1~8000)所占区段数,如果所占区段为0则不用输出。
解题思路:基本还是跟区间染色问题差不多,但要注意一些东西
①这里l,r指的是端点。比如
3
1 4 1
1 2 2
3 4 3
这组数据最后输出结果为:
1 1
2 1
3 1
原因是还有[2,3]这一段颜色为1,处理办法是将更新函数写成(1,l+1,r,color)也就是左闭右开的形式,这样就可以使得中间的那块区间不背忽略。
②题目都没给总区间长度,所以无论查询还是建树,我们都默认用0~8000。
③关于查询各颜色所占区段数的问题,不可能直接通过线段树找到个颜色区段数,因为还要考虑两个子树是相邻的情况。所以可以通过使用一个vis[i]数组,记录各区间的颜色,最后只要简单处理一下就可以得到结果了。
#include<cstring>
#include<stdio.h>
#include<iostream>
#define LC(a) ((a<<1))
#define RC(a) ((a<<1)+1)
#define MID(a,b) ((a+b)>>1)
using namespace std;
typedef long long ll;
const int N=8e3*; struct node{
ll l,r;
ll color;//颜色为-2表示混合色
}tree[N]; ll ans[N];
ll vis[N];
//下推
void pushdown(ll p){
tree[RC(p)].color=tree[LC(p)].color=tree[p].color;
}
//上推
void pushup(ll p){
tree[p].color=(tree[LC(p)].color==tree[RC(p)].color?tree[LC(p)].color:-);
} void build(ll p,ll l,ll r){
tree[p].l=l;
tree[p].r=r;
tree[p].color=-;//初始化颜色因题意而定
if(l==r){
return;
}
build(LC(p),l,MID(l,r));
build(RC(p),MID(l,r)+,r);
} void update(ll p,ll l,ll r,ll color){
if(r<tree[p].l||l>tree[p].r)
return;
if(tree[p].color==color)
return;
if(l<=tree[p].l&&r>=tree[p].r){
tree[p].color=color;
return;
}
//**释放lazy标记
if(tree[p].color!=-){
pushdown(p);
}
update(LC(p),l,r,color);
update(RC(p),l,r,color);
pushup(p);
} void query(ll p,ll l,ll r){
if(r<tree[p].l||l>tree[p].r)
return; //纯色,不用再找下去
if(tree[p].color!=-){
//**使用vis数组记录区间颜色
for(int i=tree[p].l;i<=tree[p].r;i++){
vis[i]=tree[p].color;
}
return;
}
query(LC(p),l,r);
query(RC(p),l,r);
} int main(){
ios::sync_with_stdio(false);
ll n;
//注意是从0~n
while(cin>>n){
build(,0,);
ll maxc=;
for(int i=;i<=n;i++){
ll l,r,color;
cin>>l>>r>>color;
maxc=max(maxc,color);
update(,l+,r,color);
}
memset(ans,,sizeof(ans));
memset(vis,-,sizeof(vis));
query(,,);
//处理vis数组,统计各区段数
for(int i=;i<=;i++){
if(vis[i]!=vis[i-]&&vis[i]!=-)
ans[vis[i]]++;
}
for(int i=;i<=maxc;i++){
if(ans[i])
cout<<i<<" "<<ans[i]<<endl;
}
cout<<endl;
}
}
ZOJ 1610 Count the Colors(区间染色)的更多相关文章
- 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: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- 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 (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- 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
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610 Count the Colors Time Limit:2000MS ...
- ZOJ 1610 Count the Colors 【线段树】
<题目链接> 题目大意: 在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每 ...
- ZOJ 1610 Count the Colors (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
随机推荐
- [IOI2013]Dreaming
link 一道非常类似的题目(link) 试题大意 给你一棵含有$n$个节点的有边权森林,问每次连边将会用$L$的代价,问你若此图通过加边成为树时的最小直径.$n \leq 5\times 10^5$ ...
- Hibernate持久化对象修改id重新保存的办法
Hibernate持久化对象修改id重新保存的办法——Hibernate学习记录二 2017年11月10日 20:16:48 筱光 阅读数:1122 版权声明:本文为博主原创文章,未经博主允许不得 ...
- array_diff、array_diff_key、array_diff_ukey、array_diff_assoc、array_diff_uassoc 的用法
<?php // array_diff* 系列的函数都返回关联数组// array_diff* 系列函数返回数组的差集(返回在第一个参数中, 但不在其他参数中的元素) $array1 = [ ' ...
- laravel5.1 关联模型保存的方法(使用associate方法)
模型定义 class User { public function customer() { return $this->hasOne('Customer'); } } class Custom ...
- 装饰器--decorator1
装饰器 一.定义 1.装饰器:本质是函数 2.功能:用来装饰其他函数,为其他函数添加附加功能 二.原则 1.不能修改被装饰函数的源代码 2.不能修改被装饰函数的调用方式 三.实现装饰器 1.函数 即 ...
- [DeeplearningAI笔记]卷积神经网络1.4-1.5Padding与卷积步长
4.1卷积神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.4Padding 一张\(6*6\)大小的图片,使用\(3*3\)的卷积核设定步长为1,经过卷积操作后得到一个\(4*4 ...
- MingW和MSVC默认的编码方式不一样
同一份源代码,源文件编码格式为UTF-8: string tmp = "我"; ;i<tmp.size();++i) { printf("%0x ",tm ...
- react UI组件库 Salt UI
https://salt-ui.github.io/?spm=a219a.7629140.0.0.JWztQO
- StringUtils.htmlEncode()--html标签过滤方法实现
package org.guyezhai.utils; import java.text.CharacterIterator; import java.text.StringCharacterIter ...
- Tinyos 第三版Make系统
1.make系统安装 cd tools ./Bootstrap ./configure make sudo make install 2.make系统结构 3.第三版Makerules文件部分解析 # ...