hdu1556 Color the ball 线段树区间染色问题
都是老套路了,如果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 线段树区间染色问题的更多相关文章
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- Color the ball 线段树 区间更新但点查询
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...
- HDU1556 Color the ball [线段树模板]
题意:区间修改序列值,最后输出. //hdu1166 #include<iostream> #include<cstdio> #include<cstring> # ...
- hdu 1556 Color the ball 线段树 区间更新
水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...
- HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)
题目大意:有n个人,给你他们的关系(老板和员工),没有直属上司的人就是整个公司的领导者,这意味着n个人形成一棵树(多叉树).当一个人被分配工作时他会让他的下属也做同样的工作(并且立即停止手头正在做的工 ...
- hdu 5023(线段树区间染色,统计区间内颜色个数)
题目描述:区间染色问题,统计给定区间内有多少种颜色? 线段树模板的核心是对标记的处理 可以记下沿途经过的标记,到达目的节点之后一块算,也可以更新的时候直接更新到每一个节点 Lazy操作减少修改的次数( ...
- 线段树区间染色 ZOJ 1610
Count the Colors ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...
随机推荐
- 爬取知名社区技术文章_items_2
item中定义获取的字段和原始数据进行处理并合法化数据 #!/usr/bin/python3 # -*- coding: utf-8 -*- import scrapy import hashlib ...
- Mac 管理员变为了普通用户怎么办?
开机的时候一直按住 command+s 出现命令行终端的时候按照以下顺序输入命令: /sbin/mount -uw 回车rm var/db/.AppleSetupDone 回车 reboot ...
- Composer - windows下安装方法
在windows下安装的方法 方法一:使用安装程序 这是将 Composer 安装在你机器上的最简单的方法. 下载并且运行 Composer-Setup.exe,它将安装最新版本的 Composer ...
- PHP错误杂记
Notice: Only variables should be passed by reference in-- 原因:The problem is, that end requires a ref ...
- MySQL--当事务遇到DDL命令
众所周知MySQL的DDL语句是非事务的,即不能对DLL语句进行回滚操作,哪在事务中包含DDL语句会怎样呢? 如: #禁用自动提交 set autocommit=off; #创建tb1 create ...
- Notepad++运行Java
插件NppExec使用 首先要让Notepad++编译和运行Java,前提是电脑里已经配置好了Java的环境 1,安装插件NppExec:解压出来提取NppExec.dll文件放在Notepad++安 ...
- java根据模板导出pdf
在网上看了一些Java生成pdf文件的,写的有点乱,有的不支持写入中文字体,有的不支持模板,有的只是随便把数据放里面生成文件,完全不考虑数据怎样放置的以及以后的维护性,想想还是自己总结一个完全版的导出 ...
- Android 初了解
1.1G-4G 1G 大哥大 语音通话 2G 小灵通 采用GSM,美国的一个军方标准,后来被民用了. 可以发短信了,上网的网址不是www,是wap.baidu.com 3G 可以上网了,直接用ww ...
- python的pika模块操作rabbitmq
上一篇博文rabbitmq的构架和原理,了解了rabbitmq的使用原理,接下来使用python的pika模块实现使用rabbitmq. 环境搭建 安装python,不会的请参考Linux安装配置py ...
- Spring学习之装配Bean
通过注解配置的bean,默认是单例 @Autowired private CodeTypeService codeTypeService; 测试:在Controller层调用Service层对象Cod ...