都是老套路了,如果n=5,要把区间[1,4]染色,可以递归去染区间[1,3]和区间[4,4],如果区间相等就自加,不相等继续递归寻找对应区间。

打印结果时,把所有到达叶节点包含i的区间值相加,就是最后答案。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=8e5;
int tree[maxn];
void Build_tree(int l,int r,int ll,int rr,int cur){
	if(l==ll&&r==rr){
		tree[cur]++;
		return;
	}
	int mid=(ll+rr)/2;
	if(r<=mid) Build_tree(l,r,ll,mid,cur<<1);
	else if(l>=mid+1) Build_tree(l,r,mid+1,rr,(cur<<1)+1);
	else {
		Build_tree(l,mid,ll,mid,cur<<1);
		Build_tree(mid+1,r,mid+1,rr,(cur<<1)+1);
	}
}
int u,n;
void print(int l,int r,int cur,int cnt){
	cnt+=tree[cur];
	if(l==r) {
		++u;
		if(u==n) printf("%d\n",cnt);
		else printf("%d ",cnt);
		return;
	}
	print(l,(l+r)/2,cur<<1,cnt);
	print((l+r)/2+1,r,(cur<<1)+1,cnt);
}
int main(){
	while(scanf("%d",&n)==1&&n){
		memset(tree,0,sizeof(tree));
		u=0;
		int l,r;
		for(int i=0;i<n;++i){
			scanf("%d%d",&l,&r);
			Build_tree(l,r,1,n,1);
		}
		print(1,n,1,0);
	}
	return 0;
}

如有不当之处欢迎指出!

hdu1556 Color the ball 线段树区间染色问题的更多相关文章

  1. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  2. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  3. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  4. Color the ball 线段树 区间更新但点查询

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...

  5. HDU1556 Color the ball [线段树模板]

    题意:区间修改序列值,最后输出. //hdu1166 #include<iostream> #include<cstdio> #include<cstring> # ...

  6. hdu 1556 Color the ball 线段树 区间更新

    水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...

  7. HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)

    题目大意:有n个人,给你他们的关系(老板和员工),没有直属上司的人就是整个公司的领导者,这意味着n个人形成一棵树(多叉树).当一个人被分配工作时他会让他的下属也做同样的工作(并且立即停止手头正在做的工 ...

  8. hdu 5023(线段树区间染色,统计区间内颜色个数)

    题目描述:区间染色问题,统计给定区间内有多少种颜色? 线段树模板的核心是对标记的处理 可以记下沿途经过的标记,到达目的节点之后一块算,也可以更新的时候直接更新到每一个节点 Lazy操作减少修改的次数( ...

  9. 线段树区间染色 ZOJ 1610

    Count the Colors ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...

随机推荐

  1. jdbc与java.sql

    JDBC连接操作数据库流程:1.将数据库驱动jar包放在lib文件夹下. 2.定义驱动名(driver),数据库url,username,password字符串常量 3.注册数据库驱动Class.fo ...

  2. ecplise最有用的8个快捷键

    1. ctrl+shift+r 打开资源 这组快捷键可以让你打开你工作区中的任何一个文件.而你只需要按下键盘的文件名或前几个字母 美中不足的是这组快捷键并非在所有视图下都能用. 2.ctrl+o:快速 ...

  3. Go基础--goroutine和channel

    goroutine 在go语言中,每一个并发的执行单元叫做一个goroutine 这里说到并发,所以先解释一下并发和并行的概念: 并发:逻辑上具备同时处理多个任务的能力 并行:物理上在同一时刻执行多个 ...

  4. JDK及Tomcat集成到MyEclipse

    JDK及Tomcat集成到MyEclipse 1.安装好MyEclipse 2.破解 3.配置环境JDK D:\jdk1.6.0_21\bin; ==>放在系统path前面 4.打开MyEcli ...

  5. Git 生成 SSH 公钥

    2018-01-05 11:24:04 许多 Git 服务器都使用 SSH 公钥进行认证. 为了向 Git 服务器提供 SSH 公钥,如果某系统用户尚未拥有密钥,必须事先为其生成一份. 这个过程在所有 ...

  6. cmd中控制某个命令执行多少次

    for /l %i in (1,1,5) do calc五次启动计算器程序for /l %i in (start ,step,end) do commandfor /l 表示从数字start开始,以s ...

  7. <>和“”的区别

    <stdio.h>是直接从系统里边找. ""是先在本地找,然后在系统里边找. <>不可以替换"",       "" ...

  8. Django中url的生成过程详解

    在前面我们知道,Django启动之前会执行admin.py中的autodiscover()方法. def autodiscover(): autodiscover_modules('admin', r ...

  9. 编译安装 apache 2.4.6

    如果配置apr,需要预先安装apr 以下是安装apache 步骤: groupadd webuser useradd -g webuser webuser 下载apache2 下载链接:http:// ...

  10. Spark Streaming编程指南

    Overview A Quick Example Basic Concepts Linking Initializing StreamingContext Discretized Streams (D ...