<题目链接>

题目大意:

在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每种颜色的色块数量是多少。

解题分析:

首先要注意,这是对区间进行更新,。所以update的时候是对输入区间[a,b]的左闭右开或者是左开右闭进行处理。然后本题思路非常清晰,就是按照输入顺序更新线段树对应区间,然后对[0,8000]这个区间从左向右进行色块的统计。PS:虽然最后还是要将所有lazy下放到所有的叶子节点,进行色块数量的暴力统计,但是pushdown函数确实能够减少 update 的一些复杂度。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define Lson rt<<1,l,mid
#define Rson rt<<1|1,mid+1,r
const int M =8e3;
int tr[M<<],lazy[M<<],last;
int col[M<<];
void Pushdown(int rt){
if(lazy[rt]!=-){
int tmp=lazy[rt];
lazy[rt<<]=lazy[rt<<|]=tmp;
tr[rt<<]=tr[rt<<|]=tmp;
lazy[rt]=-;
}
}
void update(int rt,int l,int r,int L,int R,int c){ //区间染色
if(L<=l&&r<=R){
lazy[rt]=c;
tr[rt]=c;
return;
}
Pushdown(rt);
int mid=(l+r)>>;
if(L<=mid)update(Lson,L,R,c);
if(R>mid)update(Rson,L,R,c);
}
void query(int rt,int l,int r){ //运用递归(类似于dfs),达到从左到右逐步查询的效果,当遇到染色的点时,判断其是否与前一个点相同,如果不同,就说明该颜色的区块
if(l==r){
if(tr[rt]!=last&&tr[rt]!=-)col[tr[rt]]++;
last=tr[rt];
return;
}
Pushdown(rt);
int mid=(l+r)>>;
query(Lson);
query(Rson);
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
memset(tr,-,sizeof(tr));
memset(lazy,-,sizeof(lazy));
memset(col,,sizeof(col));
while(n--){
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
update(,,M,x+,y,c); //由于本题是区间更新,所以不能直接更新[a,b]这个闭区间的所有点,而是对a~b的左开右闭或者左闭右开区间进行更新,类似于图的将边权转化为点权
}
last=-;
query(,,M);
for(int i=;i<=M;i++){
if(col[i])printf("%d %d\n",i,col[i]);
}
printf("\n");
}
return ;
}

2018-09-22

ZOJ 1610 Count the Colors 【线段树】的更多相关文章

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

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

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

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

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

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

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

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

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

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

  6. ZOJ 1610.Count the Colors-线段树(区间染色、区间更新、单点查询)-有点小坑(染色片段)

    ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting s ...

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

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

  8. ZOJ 1610 Count the Colors (线段树区间更新与统计)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

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

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

  10. zoj 1610 Count the Colors(线段树延迟更新)

    所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...

随机推荐

  1. oracle 11g完全彻底的卸载

    1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: 2.打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit>  HKEY ...

  2. Ionic 2: ReferenceError: webpackJsonp is not defined

    I'm new to Ionic. I have started project with super template. But when I try to run the app in brows ...

  3. 【python】安装hyperscan-python

    环境centos6.2 1. 先安装hyperscan 主要参考http://blog.csdn.net/cheng_fangang/article/details/51143412 注意:里面2.8 ...

  4. follow

    public function follow(Request $request, FeedModel $model, FeedRepository $repository) { if (is_null ...

  5. poj3070 单位矩阵(转移矩阵构造)+矩阵快速幂

    太妙了..通过矩阵乘法来加速递推 #include<iostream> #include<cstring> #include<cstdio> using names ...

  6. vue router.push(),router.replace(),router.go()和router.replace后需要返回两次的问题

    转载:https://www.cnblogs.com/lwwen/p/7245083.html https://blog.csdn.net/qq_15385627/article/details/83 ...

  7. 常见的爬虫分析库(1)-Python3中Urllib库基本使用

    原文来自:https://www.cnblogs.com/0bug/p/8893677.html 什么是Urllib? Python内置的HTTP请求库 urllib.request          ...

  8. mac pro 基本使用

    command+空格  调出搜索框 可以输入:网络,就是配置ip等信息 可以输入:终端,就是命令行窗口 可以输入:触控板,可以调节多点触控版 可以输入:系统偏好设置,进行配置 触控板:多点触控——两个 ...

  9. python算法

    选择排序的实现: #选择排序的实现 def xzpx(arr): for i in range(0,len(arr)): # 每趟排序 k=i for j in range(i+1,len(arr)) ...

  10. 【C++ Primer | 08】IO库

    IO类 C++的输入输出分为三种: (1)基于控制台的I/O (2)基于文件的I/O (3)基于字符串的I/O istringstream类 描述:从流中提取数据,支持 >> 操作 这里字 ...