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 ...
随机推荐
- .net Core连接MongoDB
前两天在学习MongoDB相关的知识,做了个小Demo,大概是省份里面有多少所学校 连接MongoDB首先要通过Nuget添加一个MongoDB的包,下载此包 安装完毕后开始写代码了,创建一个省份实体 ...
- maven依赖问题
我的一个maven项目A依赖于我的另一个maven项目B,但是maven dependencies中显示的是文件.如下图: 而且项目A部署的时候,部署到tomcat容器的时候也是直接部署的B的编译后的 ...
- 防盗链[referer]
原文出处:http://www.cnblogs.com/devilfree/archive/2012/09/11/2680914.html 总结一下今天学习防盗链Filter的一些知识点: 防盗链要实 ...
- linkin大话面向对象--java关键字
java中的关键字有以下几个,他们不能作任何其它的用途. 发现没,java中的关键字全是小写,java是严格区分大小写的. abstract default null synchronized ...
- 【原创】相对完整的一套以Jmeter作为工具的性能测试教程(接口性能测试,数据库性能测试以及服务器端性能监测)
准备工作 jmeter3.1,为什么是3.1,因为它是要配合使用的serveragent所支持的最高版本,下载链接 https://pan.baidu.com/s/1dWu5Ym JMeterPlug ...
- java面向对象基础(二)
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- sublime卡顿
sublime突然卡顿,输入字符要一两秒后才显示出来, 解决方法:首选项--插件控制--禁用插件 Git Gutter
- zabbix_sender用法实例
环境centos6.8 zabbix版本3.2.4 需求: 要远程监控一台服务器A,但只能通过远程服务器连接本地服务器B,但B不能主动连A(因为A没有固定公网ip) 使用了zabbix_agent的a ...
- Tomcat8远程访问manager,host-manager被拒绝403
Tomcat部署在服务器之后在服务器本地访问manager和host-manager成功(即127.0.0.1:8080或者localhost:8080),但使用测试主机访问tomcat的manage ...
- Java基础教程1:环境配置及第一个HelloWorld.java
本文主要介绍JDK环境配置.Sublime Text3配置及第一个HelloWorld.Java程序.运行环境为Win10系统,使用JDK1.8版本. 1. JDK下载及环境配置 1.1 JDK下载 ...