hdu 1556 Color the ball (线段树+代码详解)
Color the ball
当N = 0,输入结束。
1 1
2 2
3 3
3
1 1
1 2
1 3
0
3 2 1
一开始写这个题的时候我是用了线段树,但是a了之后看其他大牛的博客,发现了另一种思路,并且时间空间都要比线段树少很多,那么现在就来归纳一下这两种方法。
方法1:
#include<stdio.h>
struct data{
int l,r,v;
}line[400005];
void bulid (int l,int r,int x) //构建二叉树;
{
line[x].l=l;
line[x].r=r;
line[x].v=0; //这里需要讲所有节点标记为零;
if(l==r){
return ;
}
int m=(r+l)/2;
bulid(l,m,x*2);
bulid(m+1,r,x*2+1);
}
void updata (int l, int r ,int x , int a , int b ) //更新二叉树;
{
if(l<=a&&b<=r){ //如果节点x在l和r区间范围之内,则这个区间标记的值加1;
line[x].v++;
return ;
}
int m=(a+b)/2;
if(m<l){ //这里需要注意符号
updata(l,r,x*2+1,m+1,b);
}else if(r<=m){
updata(l,r,x*2,a,m);
}else {
updata(l,m,x*2,a,m);
updata(m+1,r,x*2+1,m+1,b);
}
}
int query (int i,int x,int l,int r,int sum) //查询,其中sum记录涂颜色的次数;
{
if(i==l&&i==r){
return sum+line[x].v;
}
int m=(l+r)/2;
sum+=line[x].v;
if(i<=m){
return query(i,x*2,l,m,sum);
}else {
return query(i,x*2+1,m+1,r,sum);
}
}
int main ()
{
int i,j,n,m,a,b;
while(scanf("%d",&n),n!=0){
bulid(1,n,1);
for(i=0;i<n;i++){
scanf("%d %d", &a, &b);
updata(a,b,1,1,n);
}
for(i=1;i<=n;i++){
if(i==1)
printf("%d",query(i,1,1,n,0));
else printf(" %d",query(i,1,1,n,0));
}
printf("\n");
}
return 0;
}
方法二:
#include<stdio.h>
#include<string.h>
int main ()
{
int line[100010];
int n,a,b,i,sum;
while(scanf("%d",&n),n){
memset(line,0,sizeof(line));
for(i=0;i<n;i++){
scanf("%d %d",&a,&b);
line[a]++;
line[++b]--;
}
sum=0;
for(i=1;i<=n;i++){
sum+=line[i];
if(i==1)
printf("%d",sum);
else printf(" %d",sum);
}
printf("\n");
}
return 0;
}
hdu 1556 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 ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
- HDU 1556 Color the Ball 线段树 题解
本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...
- hdu 1556 Color the ball 线段树 区间更新
水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...
- hdu 1556 Color the ball (树状数组)
Color the ballTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdu 1556 Color the ball(树状数组)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数[a,b]之间的气球 ...
- HDU 1556 Color the ball (树状数组 区间更新+单点查询)
题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...
随机推荐
- Tempter of the Bone(dfs+奇偶剪枝)题解
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- dubbo 配置属性
1,服务方 <dubbo:application name="demo-provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> ...
- 事务(Transaction)
1.演示转账的功能:(1)创建一张表示学生表表 CREATE TABLE student( id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(50), ac ...
- 使用Apache Kylin搭建企业级开源大数据分析平台
转:http://www.thebigdata.cn/JieJueFangAn/30143.html 我先做一个简单介绍我叫史少锋,我曾经在IBM.eBay做过大数据.云架构的开发,现在是Kylige ...
- 【Android实验】线程的使用-计时器
目录 实验目的 实验要求 实验过程 实验结果 实验代码 实验总结 实验目的 熟悉和掌握Android线程的使用 实验要求 完成一个秒表,具备启停功能,正确使用工作线程完成界面刷新 分析秒表的计时是否准 ...
- UVa 11039 设计建筑物
https://vjudge.net/problem/UVA-11039 题意: 有n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增. 思路:正数存一个数组,负数 ...
- Intel微处理器学习笔记(二) 三种模式
三种模式:实模式.保护模式和平展模式. 实模式存储器(DOS存储器)位于00000H~FFFFFH,共1M空间(任何型号微处理器都支持). 保护模式存储器(Windows存储器)可位于整个保护存储系统 ...
- html 画圆
<html> <head> <script type = "text/javascript" src = "https://d3js.org ...
- bugfree 安装配置(Ubuntu16.04 amd 64 Desktop)
上面是我使用的版本! 1.首先搭建 xampp 下载XAMPP:https://www.apachefriends.org/download.html 注意:下载低版本的,不然之后会找不到mysql ...
- Flex布局兼容知识点总结
转载,原文http://www.cnblogs.com/tugenhua0707/p/5180841.html,部分截取 假设父容器class为 box,子项目为item.旧版语法如下:一:定义容器的 ...