都是老套路了,如果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. HTML中padding和margin的区别和用法

     margin(外边距) 定义:margin是用来隔开元素与元素的间距,发生在元素本身的外部,margin用于布局分开元素使元素与元素互不相干. 提示:margin: top right bottom ...

  2. Servlet--HttpServletResponse的2个操作流的方法

    前面已经说过无数多次了,我们的项目都是基于HTTP协议的一次请求,一次响应.实际编码中,我们在处理完逻辑后一般是跳转到一个页面上,或者用输出流返回json字符串.其实跳转到一个页面往往也就是JSP,J ...

  3. SQL模板资源管理器,你用了吗?

    SQL Server Management Studio 有个模板资源管理器,不知你用过没有?使用模板创建脚本.自定义模板等功能能大大提高你的工作效率,如果没有尝试过,赶紧去试试吧.很多时候,我们习惯 ...

  4. struts 中自定义action访问方法

    struts中action类继承了ActionSupport  默认实现了execute()方法 struts.xml配置文件中 然后可以配置如下映射: <package name =" ...

  5. sed,n,N,d,D,p,P,h,H,g,G,x,解析

    原文地址 这篇文章主要是我参考命令的,直接复制粘贴,有问题请拍砖 A. sed执行模板=sed '模式{命令1;命令2}' 即逐行读入模式空间,执行命令,最后输出打印出来 B. p打印当前模式空间所有 ...

  6. 【转】Linux查找本机默认网关

    ip route show | grep 'default' | awk '{print $3}' #vim getgw.sh #!/bin/shdefault_gateway_ip=`ip rout ...

  7. 权限问题导致zabbix无法监控mysql

    说说一个困扰自已两天的问题. 首先是用常规的方法安装上了mysql数据库.做了主从. 在监控从库的时候,发现所有的监控数据库的监控项都获取不到key值 . zabbix server端也不报错.获取到 ...

  8. eclipse每次闪退后都提示查看\workspace\.metadata\.log

    错误如下: 找到<workspace>/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi"文件,将其删掉,再重启 ...

  9. How to get started with GIT and work with GIT Remote Repo

    https://www.ntu.edu.sg/home/ehchua/programming/howto/Git_HowTo.html#zz-7. 1.  Introduction GIT is a ...

  10. 常用的几个PHP加密函数

    1.前言 PHP加密方式分为单项散列加密,对称加密,非对称加密这几类. 像常用的MD5.hash.crypt.sha1这种就是单项散列加密,单项散列加密是不可逆的. 像URL编码.base64编码这种 ...