都是老套路了,如果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. js随机生成验证码及其颜色

    今天迎来了2018年第一场雪,这个美好的日子,总的写点什么纪念一下,在这里写了一个在js中使用Math.random()函数,随机生成四位数的验证码及其验证码换颜色. js代码如下: var arra ...

  2. 《共享库PATH与ld.so.conf简析》

    这是摘抄<共享库PATH与ld.so.conf简析>1. 往/lib和/usr/lib里面加东西,是不用修改/etc/ld.so.conf的,但是完了之后要调一下ldconfig,不然这个 ...

  3. Cannot create an instance of OLE DB provider “OraOLEDB.Oracle” for linked server "xxxxxxx".

    在SQL SERVER 2008 R2下用Windows 身份认证的登录名创建了一个访问ORACLE数据库的链接服务器xxxxx,测试成功,木有问题,但是其它登录名使用该链接服务器时,报如下错误: 消 ...

  4. H5WebSocket消息推送

    1.效果图 2.前端代码 @{ ViewBag.Title = "Home Page"; } @*HTML5 WebSocket WebSocket是HTML5开始提供的一种在单个 ...

  5. ArcGIS License启动无响应

    根据对网上的总结以及个人的经验,首先建议关闭电脑的防火墙以及杀毒软件 如果点击启动,依然没有反应,建议在dos下进行启动,命令为: lmgrd -z -c service.txt 如果需要深入研究,可 ...

  6. Community Stories: Cinemachine and Timeline——Community Stories: Cinemachine and Timeline

    Community Stories: Cinemachine and Timeline 社区故事:Cinemachine 和 Timeline Adam Myhill, 八月 25, 2017 原文: ...

  7. [eslint-plugin-vue] [vue/no-unused-vars] 'scope' is defined but never used.

    前言 今天在做项目的时候Visual Studio Code报了一个错 这个错的意思是声明了scope却没有使用它,这是vue的eslink插件检测的. 我想这个scope的属性不是自己的吗,咋是我声 ...

  8. 洛谷 [P251] 餐巾计划问题

    有上下界的最小费用最大流 可以联想到供求平衡问题,所以我们要拆点做这道题 把每天分为二分图两个集合中的顶点Xi,Yi,建立附加源S汇T. 1.从S向每个Xi连一条容量为ri,费用为0的有向边. 2.从 ...

  9. CF 455D. Serega and Fun [分块 deque]

    Serega and Fun 题意: [l,r]循环右移一位,查询区间内某个数出现次数 为什么好多人用链表?反正我是不会写双向链表 完全可以分块然后模拟啊...中间的块只会插入删除一个元素呀....用 ...

  10. HDU 4315 Climbing the Hill [阶梯Nim]

    传送门 题意: 和上题基本一样:山顶可以有多人,谁先把king放到山顶谁就胜 并不太明白 #include <iostream> #include <cstdio> #incl ...