Color the ball

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

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
 
Author
8600
 
Source
 
Recommend
LL   |   We have carefully selected several similar problems for you:  1542 1394 1255 2795 3397 

 
  线段树,区间更新,经典题
 
  题意:给你N个气球,不断刷新指定区间的颜色,刷新N次,最后输出每一个气球的刷新次数。
  思路
  很典型的区间更新的思想,那么很自然想到用线段树来做。线段树的每一个节点代表一个区间,并且有一个特定值来存储这个区间特定的信息,这里我们用这个特定值val记录该区间被刷新的次数。
  那么思路就有了,每一次刷新,用函数 Update(1,a,b),不断递归找到 [a,b] 区间,然后将该区间记录的值 val+1,表示该区间被刷新一次。注意必须找到区间,而且只刷新该区间的val值,如果遇到区间分开的情况,那么就分开来找。
  这样指定区间就存储了刷新的次数,我们最后要做的就是把每一条递归路径的刷新次数累加起来,最后到达的终点就是哪个气球的刷新次数。例如,有三个气球,第一个气球的刷新次数就是区间节点[1,3],[1,2]和[1,1]的val值相加。这个步骤可以在查询函数Query中做。
  代码
 #include <iostream>
#include <stdio.h>
using namespace std;
#define MAXN 100010
struct Node{
int L,R;
int val; //被涂过的次数
};
Node a[MAXN*+];
void Init(int d,int l,int r) //初始化线段树
{
if(l==r){ //递归出口
a[d].L = l;
a[d].R = r;
a[d].val = ;
return ;
} //初始化当前节点
a[d].L = l;
a[d].R = r;
a[d].val = ; //递归初始化孩子节点
int mid = (l+r)/;
Init(d*,l,mid);
Init(d*+,mid+,r);
}
void Update(int d,int l,int r) //更新某一区间的值
{
if(a[d].L==l && a[d].R==r){ //递归出口。找到区间
a[d].val++;
return ;
}
if(a[d].L==a[d].R) //递归出口。没有找到
return ;
//没找到
int mid = (a[d].L+a[d].R)/;
if(mid>=r){ //去左孩子找
Update(d*,l,r);
}
else if(mid<l){ //去右孩子找
Update(d*+,l,r);
}
else { //中点在要查询区间的中间,两边都要找
Update(d*,l,mid);
Update(d*+,mid+,r);
}
}
int Query(int d,int l,int r) //查询
{
if(a[d].L==l && a[d].R==r) //找到区间
return a[d].val;
if(a[d].L==a[d].R)
return ; int mid = (a[d].L+a[d].R)/;
if(mid>=r){ //去左孩子找
return a[d].val + Query(d*,l,r);
}
else if(mid<l){ //去右孩子找
return a[d].val + Query(d*+,l,r);
}
else { //中点在要查询区间的中间,两边都要找
return a[d].val + Query(d*,l,mid) + Query(d*+,mid+,r);
}
}
int main()
{
int N,A,B,i,sum;
while(scanf("%d",&N)!=EOF && N){
Init(,,N);
for(i=;i<=N;i++){ //输入并更新线段树
scanf("%d%d",&A,&B);
Update(,A,B);
}
for(i=;i<=N;i++){ //输出每一个气球被涂过的次数
sum = Query(,i,i);
printf("%d",sum);
if(i!=N) printf(" ");
}
printf("\n");
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1556: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 线段树 区间更新

    水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...

  4. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  5. hdu 1556 Color the ball (线段树+代码详解)

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

  6. hdu 1556 Color the ball 线段树

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

  7. HDU 1556 Color the Ball 线段树 题解

    本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...

  8. Color the ball 线段树 区间更新但点查询

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...

  9. hdu1556Color the ball线段树区间更新

    题目链接 线段树区间更新更新一段区间,在更新区间的过程中,区间被分成几段,每一段的左右界限刚好是一个节点的tree[node].left和tree[node].right(如果不是继续分,直到是为止) ...

  10. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

随机推荐

  1. django-cms安装

    ubuntu:12.04 (32bit) djangocms 0.5.1 =========================== 首先,跟着这个做: https://github.com/divio/ ...

  2. JavaScript——同源策略

    概念:同源策略是客户端脚本(尤其是Javascript)的重要的安全度量标准.它最早出自Netscape Navigator2.0,其目的是防止某个文档或脚本从多个不同源装载.   这里的同源指的是: ...

  3. Unity3d 扩展自定义类Inspector

    public class MyClass : MonoBehaviour { public int A; // Use this for initialization void Start () { ...

  4. 解读Unity中的CG编写Shader系列七(不透明度与混合)

    转自http://www.itnose.net/detail/6098539.html 1.不透明度 当我们要将两个半透的纹理贴图到一个材质球上的时候就遇到混合的问题,由于前面的知识我们已经知道了片段 ...

  5. (转)C++0x语言新特性一览

    转自:http://blog.csdn.net/zwvista/article/details/2429781 原文请见http://en.wikipedia.org/wiki/C%2B%2B0x. ...

  6. 8.js模式-状态模式

    1. 状态模式 var offLightState = function(light){ this.light = light; } offLightState.prototype.buttonWas ...

  7. 11. javacript高级程序设计-DOM扩展

    1. DOM扩展 1.1 选择符API l querySelector() 接收一个css选择符,返回与该模式匹配的第一个元素 l querySelectorAll() 接收一个css选择符,返回所有 ...

  8. Java for LeetCode 237 Delete Node in a Linked List

    Java实现如下: public class Solution { public void deleteNode(ListNode node) { if(node==null||node.next== ...

  9. Excel保护工作表

    最近见到一个Excel文件,其中的表不能选中,不能编辑,今天研究了一下 明白了其设置方法 excel中开始--格式--保护工作表--去掉所有勾选,输入保护密码  即可

  10. VB兼容问题

    window7 64位无法显示打印窗问题 在Windows7 64位和VS2008环境下,PrintDialog.ShowDialog不能显示打印对话框 在VS2008中编写?如下代码: PrintD ...