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 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
随机推荐
- Vue项目SEO优化的另一种姿态
背景:当前项目首页和登陆后的平台在一个项目里,路由采用hash模式,现在要做SEO优化,这时候同构SSR(Server Side Rendering)服务端渲染代价显然太大,影响范围比较广,同样更改当 ...
- thinkphp常见问题
1.数据库查询中execute和query方法的区别 tp中execute()和query()方法都可以在参数里直接输入sql语句. 但是不同的是execute()通常用来执行insert或者upda ...
- Qt ------ QString 操作
QStringList QString::arg ------- 字符串的格式化处理,类始于sprintf 比如:QString("%1").arg(10,2,16,QLa ...
- git grep mysql 操作历史
history |grep mysql-----git history匹配出mysql操作的命令 !626 到mysql命令安装处链接mysql /usr/local/mysql/bin/mysql ...
- 修改Docker默认镜像和容器的存储位置
一.Why Docker默认的镜像和容器存储位置在/var/lib/docker中,如果仅仅是做测试,我们可能没有必要修改,但是当大量使用的时候,我们可能就要默认存储的位置了. 二.How 2.1 修 ...
- Kubernetes Job配置
我们知道使用kubernetes的rc或者rs创建的pod,kubernetes会实时监控其健康状态,如果发现pod挂掉以后,会自动启动一个新的,让pod的数量始终保持在指定的replicas上.那么 ...
- 第4章-Vue.js 交互及实例的生命周期
一.学习目标 了解实例生命周期的过程 理解钩子函数的作用 掌握Vue.js过滤器的使用方法 (重点) 能够使用网络请求进行前后端交互 (重点.难点) 二.交互的基本概念 2.1.前端和后端的概念 说明 ...
- js 30分钟倒计时
<html> <head> <meta charset="UTF-8"> <title></title> </he ...
- 2017北京国庆刷题Day3 morning
期望得分:100+60+0=160 实际得分:100+30+0=130 考场上用的哈希 #include<cstdio> #include<cstring> #include& ...
- 779D. String Game 二分 水
Link 题意: 给出两字符串$a$,$b$及一个序列,要求从前往后按照序列删掉$a$上的字符,问最少删多少使$b$串不为a的子串 思路: 限制低,直接二分答案,即二分序列位置,不断check即可. ...