题目大意:多组数据,每组给一个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(区间染色)的更多相关文章

  1. ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

    任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...

  2. zoj 1610 Count the Colors 【区间覆盖 求染色段】

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  3. ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】

    Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

  4. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  5. zoj 1610 Count the Colors 线段树区间更新/暴力

    Count the Colors Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...

  6. ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  7. zoj 1610 Count the Colors

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610  Count the Colors Time Limit:2000MS   ...

  8. ZOJ 1610 Count the Colors 【线段树】

    <题目链接> 题目大意: 在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每 ...

  9. ZOJ 1610 Count the Colors (线段树成段更新)

    题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...

随机推荐

  1. ZJOI 2017 二试 day1 4.26

    day0,11:30熄灯,又因为在房间里太浪,空调开了28度,过了好久才成功降温,导致睡得不太好QaQ. 于是早上昏昏欲睡,也没怎么听懂(orz孙耀峰). 中午大家一致提议下午不去听课,回到房间浪了好 ...

  2. android lib 存储

    存储在 /data/app-lib目录下:

  3. 【字符串】KMP字符串匹配

    百度百科 Definition \(KMP\)算法是一个字符串匹配算法.他接收两个字符串\(A,B\),返回\(B\)在\(A\)中出现的所有位置. 以下称需要被匹配的串\(A\)为主串,可能在主串中 ...

  4. Linux之系统信息操作20170330

    介绍一下Linux系统中一些自带信息的获取操作等,首先从源码中找到系统信息结构体进行分析. 1.系统信息结构体说明与获取方法: 含义: struct sysinfo { long uptime;    ...

  5. laravel5.1 使用中间表的多对多关联

    用户表user 标签表tag 中间表user_tag(user_id,tag_id) 在user模型中定义tags关联如下: public function tags() { return $this ...

  6. python--生成器协程运算

    生成器 一.yield运行方式 我们定义一个如下的生成器: def put_on(name): print("Hi {}, 货物来了,准备搬到仓库!".format(name)) ...

  7. 跟我一起写Makefile(六)

    使用条件判断—————— 使用条件判断,可以让make根据运行时的不同情况选择不同的执行分支.条件表达式可以是比较变量的值,或是比较变量和常量的值. 一.示例 下面的例子,判断$(CC)变量是否“gc ...

  8. turn服务部署

    centos7.2 git clone https://github.com/coturn/coturnyum -y install openssl-develyum install openssl ...

  9. MySql 插入数据库报错 Incorrect string value: '\xF0\xA0\x86\xA2'

    今天从nginx日志分析搜索关键字,然后把关键字插入到Mysql数据库里,出现如下错误 SQL state [HY000]; error code [1366]; Incorrect string v ...

  10. 使用 XSLT 作为 HTML 的样式表

    简介 当听到样式表这个词时,您可能会想到 CSS 样式表.XSLT 样式表通常用于 XML 转换,比如在 Web 服务之间映射数据.因为 XSLT 非常适合此用途,所以创建了顶层元素 <styl ...