这个是一个经典线段树染色问题,不过题目给的是左右左右坐标,即[0,3]包含0-1这一段 1-2这一段 2-3这一段,和传统的染色不太一样,不过其实也不用太着急。

我们把左边的坐标+1,即可,那么[0,3]其实变成了[1,3]而线段树是按照点询问的,也就是每个点代表的颜色,我们就有了1,2,3,这个三个,并且避免了线段树编号不能到0的情况,然后代码就十分简单了,无需laze标记,因为每个节点的颜色就可以当成laze标记,然后不断往下pushdown既可以,当时还有一个问题就是线段树访问连续两个节点的颜色是相同的,那么它其实是一个连续的算一个,我们可以维护一个last,代表前一个区间的颜色,由于我们递归是按照左右儿子进行递归的,左儿子过了肯定是右儿子,实际上这是连续的,所以我们这需要判断这个区间颜色是不是和前面那个区间的颜色相同,从而判断这两个颜色是否相邻即可

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int maxx = ;
inline int L(int r){return r<<;};
inline int R(int r){return r<<|;};
inline int MID(int l,int r){return (l+r)>>;};
int n,last;
struct node{
int l,r,col;
}tree[maxx<<];
int ans[maxx];
void pushdown(int root){
if(tree[root].col!=-){
tree[L(root)].col=tree[root].col;
tree[R(root)].col=tree[root].col;
tree[root].col=-;
}
}
void buildtree(int root,int l,int r){
tree[root].l=l;
tree[root].r=r;
tree[root].col=-;
if (l==r){
return;
}
int mid=MID(l,r);
buildtree(L(root),l,mid);
buildtree(R(root),mid+,r);
}
void update(int root,int ul,int ur,int c){
int l=tree[root].l;
int r=tree[root].r;
if(ul<=l && r<=ur){
tree[root].col=c;
return;
};
if (tree[root].col==c)return;
pushdown(root);
int mid=MID(l,r);
if (ur<=mid){
update(L(root),ul,ur,c);
}else if(ul>mid){
update(R(root),ul,ur,c);
}else{
update(L(root),ul,mid,c);
update(R(root),mid+,ur,c);
}
}
void query(int root,int ql,int qr){
if (ql==qr){
if(tree[root].col!=- && tree[root].col!=last){
ans[tree[root].col]++;
}
last=tree[root].col;
return;
}
pushdown(root);
if(ql==qr)return;
int mid = (ql+qr)>>;
query(L(root),ql,mid);
query(R(root),mid+,qr);
}
int main(){
while(~scanf("%d",&n)){
int x,y,z;
buildtree(,,);
for (int i=;i<=n;i++){
scanf("%d%d%d",&x,&y,&z);
update(,x+,y,z);
}
last=-;
memset(ans,,sizeof(ans));
query(,,);
for (int i=;i<=;i++){
if (ans[i]){
printf("%d %d\n",i,ans[i]);
}
}
puts("");
}
return ;
}

ZOJ - 1610 经典线段树染色问题的更多相关文章

  1. pku 2777(经典线段树染色问题)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41202   Accepted: 12458 Des ...

  2. F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)

    题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树  但是没有push_up  最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段  思路是 ...

  3. Count the Colors(线段树染色)

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

  4. POJ 2777 Count Color(线段树 + 染色问题)

    传送门:Count Color Description Chosen Problem Solving and Program design as an optional course, you are ...

  5. POJ2528 Mayor's posters —— 线段树染色 + 离散化

    题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...

  6. POJ 2777 Count Color(线段树染色,二进制优化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42940   Accepted: 13011 Des ...

  7. 2243: [SDOI2011]染色 树链剖分+线段树染色

    给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段), 如“112221”由3段组 ...

  8. zoj 3349 dp + 线段树优化

    题目:给出一个序列,找出一个最长的子序列,相邻的两个数的差在d以内. /* 线段树优化dp dp[i]表示前i个数的最长为多少,则dp[i]=max(dp[j]+1) abs(a[i]-a[j])&l ...

  9. 经典线段树 UVALive 3938/UVA 1400

    题意:就是相当于动规里面的求最大连续子串,不同的是,这里需要读入一个区间x,y,输出的区间 a,b 且x<=a<=b<=y,使得a b的连续子串最长,而且询问次数达到了10的五次方. ...

随机推荐

  1. 系统休眠(System Suspend)和设备中断处理

    一.设备IRQ的suspend和resume 本小节主要解决这样一个问题:在系统休眠过程中,如何suspend设备中断(IRQ)?在从休眠中唤醒的过程中,如何resume设备IRQ? 一般而言,在系统 ...

  2. xshell远程登录工具的星号密码查看方法

    当我们在使用ftp,或者xshell等远程登录工具的时候,连接的密码是用星号*处理的,无法查看到,该如何查看到原始的密码呢? 推荐一款星号密码查看器,可以查看一些软件的带星号的密码,非常好用. 下载地 ...

  3. vue 之 .sync 修饰符

    在一些情况下,我们可能会需要对一个 prop (父子组件传递数据的属性) 进行“双向绑定”. 在vue 1.x 中的 .sync 修饰符所提供的功能.当一个子组件改变了一个带 .sync 的prop的 ...

  4. NGINX Load Balancing – TCP and UDP Load Balancer

    This chapter describes how to use NGINX Plus and open source NGINX to proxy and load balance TCP and ...

  5. gRPC奇怪的编译命令protoc

    举个栗子: protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld 大神说得没错,读文档就 ...

  6. (转)Spring Boot(九):定时任务

    http://www.ityouknow.com/springboot/2016/12/02/spring-boot-scheduler.html 在我们开发项目过程中,经常需要定时任务来帮助我们来做 ...

  7. WPF触发器(非数据库中的触发器)

    一.什么是触发器?触发器(Trigger)就是当某种条件满足后即完成相应逻辑功能的一部分程序组成.在当前的WPF中,Trigger一共有三种类型,它们分别是: (1)属性触发器:其对应的类是Trigg ...

  8. docker学习笔记(二)-dockerfile docker仓库

    dckerfile2这样写才对,不然打印的是hello $name 例子 登陆https://hub.docker.com 上面是提交到公共仓库 创建本地私有仓库 开启另外一个虚机,将重新构建上传镜像 ...

  9. Tesseract-OCR识别

    参考 https://studygolang.com/topics/4527/comment/13217 安装版Windows下链接: https://digi.bib.uni-mannheim.de ...

  10. DataGrid获取单元格的值

    string str = (dataGrid.Columns[0].GetCellContent(dataGrid.Items[0]) as TextBlock).Text;