ZOJ - 1610 经典线段树染色问题
这个是一个经典线段树染色问题,不过题目给的是左右左右坐标,即[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 经典线段树染色问题的更多相关文章
- pku 2777(经典线段树染色问题)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41202 Accepted: 12458 Des ...
- F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)
题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树 但是没有push_up 最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段 思路是 ...
- Count the Colors(线段树染色)
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- POJ 2777 Count Color(线段树 + 染色问题)
传送门:Count Color Description Chosen Problem Solving and Program design as an optional course, you are ...
- POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
- 2243: [SDOI2011]染色 树链剖分+线段树染色
给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段), 如“112221”由3段组 ...
- zoj 3349 dp + 线段树优化
题目:给出一个序列,找出一个最长的子序列,相邻的两个数的差在d以内. /* 线段树优化dp dp[i]表示前i个数的最长为多少,则dp[i]=max(dp[j]+1) abs(a[i]-a[j])&l ...
- 经典线段树 UVALive 3938/UVA 1400
题意:就是相当于动规里面的求最大连续子串,不同的是,这里需要读入一个区间x,y,输出的区间 a,b 且x<=a<=b<=y,使得a b的连续子串最长,而且询问次数达到了10的五次方. ...
随机推荐
- Cs231n课堂内容记录-Lecture2-Part1 图像分类
Lecture 2 课程内容记录:(上)https://zhuanlan.zhihu.com/p/20894041?refer=intelligentunit (下)https://zhuanlan. ...
- JavaScript -- 时光流逝(二):js中数组的方法
JavaScript -- 知识点回顾篇(二):js中数组的方法 1. 数组 (1)定义数组,数组赋值 <script type="text/javascript"> ...
- Vue学习之路4-v-bind指令
1. 定义 1.1 v-bind 指令被用来响应地更新 HTML 属性,其实它是支持一个单一 JavaScript 表达式 (v-for 除外). 2. 语法 2.1 完整语法:<span v- ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- MVC+EF 序列化类型为“System.Data.Entity.DynamicProxies.__的对象时检测到循环引用
用MVC+EF做简单查询时,返回json格式数据出现问题 原代码: public ActionResult JSon({ NorthwindEntities db = new NorthwindEnt ...
- C#基础知识之String,Stringbuilder和Stringbuffer
String可以储存和操作字符串,即包含多个字符的字符数据.这个String类提供了存储数值不可改变的字符串. StringBuilder是线程不安全的,运行效率高,如果一个字符串变量是在方法里面定义 ...
- node 打开浏览器
npm install --save opn const opn = require('opn'); // 使用默认浏览器打开 // opn('http://sindresorhus.com'); / ...
- 数据合并处理concat
var data = [ {name: '海门', value: 9}, {name: '鄂尔多斯', value: 12}, {name: '招远', value: 12}, {name: '舟山' ...
- 新建SpringBoot项目运行页面报错Whitelabel Error Page This application has no explicit mapping for /error, so yo
新建SpringBoot项目运行页面报错Whitelabel Error Page This application has no explicit mapping for /error, so yo ...
- CentOS 7 安装telnet服务
今天测试zabbix需要用到telnet服务,查询到Centos7下下载安装telnet服务的方法,特此整理记录! 一.通过yum下载安装telnet yum -y install xinetd te ...