<题目链接>

题目大意:

在[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 数据库实例简介

      回到顶部 一:Oracle 数据库实例简介 1:数据库实例的启动顺序: 使用数据库其实就是访问内存.即:数据库实例.数据库的启动是顺序是 先 nomount ---->  mount --- ...

  2. ignitius and princess 2(全排列)

    A - Ignatius and the Princess II Now our hero finds the door to the BEelzebub feng5166. He opens the ...

  3. 【kafka】设置指定topic和group_id消耗的offset

    该博文方法有问题,正确方案在http://www.cnblogs.com/dplearning/p/7992994.html 背景: 搭建了一个kafka集群,建立了topic test,用group ...

  4. JavaScript 高级程序设计第二版

    20.4 部署 20.4.1 构建 构建过程始于在源控制中定义用于存储文件的逻辑结构.最好避免使用一个文件存放所有的JavaScript,遵循以下面向对象语言中的典型模式:将每个对象或自定义了类别分别 ...

  5. spring boot引入json,jsonobject,需要指定jdk15

    spring boot引入json,需要指定jdk15 <dependency> <groupId>net.sf.json-lib</groupId> <ar ...

  6. C#Enum用Tuple保存值绑定到前端的CheckBox

    //把数字转成枚举 public static T[] NumStringsToEnums<T>(string enumNumString ) //where T:Enum { if (s ...

  7. 字符转ASCII码

    char k = '成'; int str = (int)k; Console.WriteLine(str); 结果25104就是‘成’对应的ASCII值

  8. Git 将本地库添加到远程仓库

    git remote add origin ssh://admin@127.0.0.1:29418/Prjs/prj1.git git push -u origin master

  9. 【C#】wpf中的xmlns命名空间为什么是一个网址,代表了什么意思(转载)

    原文:https://blog.csdn.net/catshitone/article/details/71213371 新建一个wpf的项目,我们先来看下它默认的命名空间都是哪些? 可以看到xmln ...

  10. 统计各个数据库的各个数据表的总数,然后写入到excel中

    1.最近项目基本进入最后阶段了,然后会统计一下各个数据库的各个数据表的数据量,开始使用的报表工具,report-designer,开源的,研究了两天,发现并不是很好使,最后自己下班回去,晚上思考,想着 ...