Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6609    Accepted Submission(s): 3468

Problem Description
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
 
Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
 
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
 
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
 
Sample Output
1 1 1
3 2 1

思路:线段树题目,感觉是时候学学线段树这种数据结构了,于是找了一题练练,一次AC,爽啊!

成断更新,单点查询!

AC代码:

 #include<stdio.h>
#define MAX 100001
typedef struct
{
int left;
int right;
int cover;
}Node;
Node ball[*MAX];
int cnt;
void build(int l,int r,int k)
{
int mid;
if(l == r)
{
ball[k].left = l;
ball[k].right = r;
ball[k].cover = ;
return ;
}
ball[k].left = l;
ball[k].right = r;
ball[k].cover = ;
mid = (l+r) >> ;
build(l,mid,k << );
build(mid+,r,k << |);
} void insert(int l,int r,int k)
{
if(l == ball[k].left && r == ball[k].right)
{
ball[k].cover++;
return ;
}
if(ball[k].right == ball[k].left)
return ;
int mid = (ball[k].left+ball[k].right) >> ;
if(r <= mid)
insert(l,r,k << );
else if(l > mid)
insert(l,r,k << |);
else
{
insert(l,mid,k << );
insert(mid+,r,k << |);
}
return ;
} void search(int num,int k)
{
if(ball[k].left == ball[k].right)
{
cnt += ball[k].cover;
return ;
}
cnt += ball[k].cover;
int mid = (ball[k].left+ball[k].right) >> ;
if(num <= mid)
search(num,k << );
else
search(num,k << |);
return ;
} int main()
{
int n,i;
int a,b;
while(~scanf("%d",&n) && n)
{
build(,n,);
for(i = ;i < n;i ++)
{
scanf("%d%d",&a,&b);
insert(a,b,);
}
for(i = ;i < n;i ++)
{
cnt = ;
search(i,);
printf("%d ",cnt);
}
cnt = ;
search(n,);
printf("%d\n",cnt);
}
return ;
}

Color the ball HDOJ--1556的更多相关文章

  1. Color the ball HDU - 1556 (非线段树做法)

    题意:在1到n的气球中,在不同的区域中涂颜色,问每个气球涂几次. #include<cstdio>int num[100010];int main(){ int n, x, y;; whi ...

  2. Color the ball HDU - 1556 (线段树)

    思路:线段树,区间更新 #include<iostream> #include<vector> #include<string> #include<cmath ...

  3. A - Color the ball HDU - 1556 (差分数组+前缀和)

    思路等引自博客 https://blog.csdn.net/johnwayne0317/article/details/84928568 对数组a[7]: a[0]=1; = d[0] a[1]=1; ...

  4. 树状数组模板--Color the ball

    Color the ball HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电 ...

  5. hdoj 1556 Color the ball【线段树区间更新】

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  9. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  10. HDU 1556 Color the ball (数状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. Core模块其他常用知识点[OpenCV 笔记14]

    Matx 轻量级的Mat,必须在使用前规定好大小,比如一个2x3的float型的Matx,可以声明为Matx23f Vec Vec是Matx的一个派生类,是一个一维的Matx,跟vector很相似.在 ...

  2. 暑假集训(1)第四弹 -----Find a way(Hdu2612)

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  3. 【CODECHEF】【phollard rho + miller_rabin】The First Cube

    All submissions for this problem are available. Read problems statements in Mandarin Chinese and Rus ...

  4. jQuery插件综合应用(四)头像设置

    一.操作流程 会员点击头像设置,弹出一个层,在层中,有上传图片的按钮,用户点击按钮上传图片,图片在服务器端按大小压缩保存(方便剪切).保存后,在前端显示,然后用户可修剪图片.选择图片区域,点击提交,保 ...

  5. php验证复选框有效性的示例

    本文介绍一个简单的php通过代码验证复选框值的有效性,有需要的可以参考一下 验证复选框的php代码,如下: 复制代码代码如下: <?php   /**   * 在php中验证复选框的有效性  * ...

  6. hadoop1 和haddop2 mapperreducer的wordcount详解

    转 mapreduce中wordcount详细介绍(包括hadoop1和hadoop2版本) 发表于1年前(2014-04-24 10:08)   阅读(1458) | 评论(0) 1人收藏此文章, ...

  7. python学习笔记enumerate()与range(len)运用及赋值小计

    #!/uer/bin/env python # _*_ coding: utf-8 _*_ #格式1 a = 'abc' for i in range(len(a)): print a[i],'(%d ...

  8. Soy文件生成JS文件 - 一个使用Google soy模板的例子

    1.下载工具包,后解压. http://closure-templates.googlecode.com/files/closure-templates-for-javascript-latest.z ...

  9. C# 两时间,时间间隔

    #region 返回时间差        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)        {  ...

  10. 转:Stack Overflow通过关注性能,实现单块应用架构的扩展能力

    原文来自于:http://www.infoq.com/cn/news/2015/07/scaling-stack-overflow 在New York QCon 2015大会上,David Fulle ...