Color the ball

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

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
 
//注意laz[]要和线段树数组开一样大小
#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
const int n = ;
using namespace std;
void pushdown(int num);
int tre[n * ];
int laz[n*];
void update(int num, int le, int ri, int x, int y,int z)
{
if (x <= le && y >= ri)
{
tre[num] = tre[num] + z;//初值为0,z=1
laz[num] = laz[num] + z;
return;
}
pushdown(num);
int mid = (le + ri) / ;
if (x <= mid)
update(num * , le, mid, x, y, z);
if (y > mid)
update(num * + , mid + , ri, x, y, z);
}
void pushdown(int num)
{
if (laz[num] != )
{
tre[num * ] += laz[num];
tre[num * + ] += laz[num];
laz[num * ] += laz[num];
laz[num * + ] += laz[num];
laz[num] = ;
}
}
int query(int num, int le, int ri, int x)
{
if (le == ri)
{
return tre[num];
}
pushdown(num);
int mid = (le + ri) / ;
if (x <= mid)
return query(num * , le, mid, x);
else
return query(num * + , mid + , ri, x);
}
int main()
{
int t;
while (cin >> t)
{
memset(tre, , sizeof(tre));
memset(laz, , sizeof(laz));//初始化延迟标记
for (int i = ; i < t; i++)
{
int x, y;
cin >> x >> y;
update(, , t, x, y, );
cout << query(, , n, x) << endl;
}
}
return ;
}

用结构体储存数据

#include <stdio.h>
#include<iostream>
#include <string.h>
using namespace std;
struct node
{
int left, right, count;
}c[ * ];
int sum[];
void build(int le, int ri, int root)
{
c[root].left = le;
c[root].right = ri;
c[root].count = ;
if (le == ri)
return;
int mid = (le + ri) / ;
build(le, mid, root * );
build(mid + , ri, root * + );
}
void update(int le, int ri, int root)//更新
{
if (c[root].left == le && c[root].right == ri)//只需要在这个区间+1就行了,节省时间,不用找到每个数
{
c[root].count++;
return;
}
int mid = (c[root].left + c[root].right) / ;
if (mid<le)//如果更新区间在右子树上,就只更新右区间
update(le, ri, root * + );
else if (mid >= ri)//更新左子树
update(le, ri, root * );
else//更新区间即在右子树上,又在左子树上
{
update(le, mid, root * );
update(mid + , ri, root * + );
}
}
void tosum(int root)//求和、记录每个气球被涂过的次数
{
for (int i = c[root].left; i <= c[root].right; i++)
sum[i] += c[root].count;
if (c[root].left == c[root].right)
return;
tosum(root * );//通过子节点来更新父节点
tosum(root * + );
}
int main()
{
int n;
while (scanf("%d", &n) && n)
{
memset(sum, , sizeof(sum));
memset(&c, , sizeof(&c));
build(, n, );
for (int i = ; i<n; i++)
{
int le, ri;
scanf("%d %d", &le, &ri);
update(le, ri, );
}
tosum();
printf("%d", sum[]);
for (int i = ; i <= n; i++)
printf(" %d", sum[i]);
printf("\n");
}
return ;
}

hdu 1556 涂气球 线段树(区间更新~对区间[x,y]更新,求任意节点被更新的次数)的更多相关文章

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

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

  2. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  3. bzoj 4631: 踩气球 线段树合并

    4631: 踩气球 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 265  Solved: 136[Submit][Status][Discuss] ...

  4. hdu 5692(dfs序+线段树,好题)

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  5. bzoj 4631: 踩气球 线段树

    题目: Description 六一儿童节到了, SHUXK 被迫陪着M个熊孩子玩一个无聊的游戏:有N个盒子从左到右排成一排,第i个盒子里装着Ai个气球. SHUXK 要进行Q次操作,每次从某一个盒子 ...

  6. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  7. 【bzoj4631】踩气球 线段树

    题解: 真是很zz 我都想到线段树分治的思路了... 不过还是一道好题 首先跟线段树分治一样将区间投射到线段树上去 每次修改如果该个区间修改为0,则对他们对应的特定区间-1 这样每个区间会有一次变0, ...

  8. kb-07线段树-12--二分查找区间边界

    /* hdu4614 本题刚开始想能不能记录该区间最前面开始的点,最后面的点,区间空的数量:但是病不行 然后线段树的本质是区间操作,所以!这题主要就是区间的空的全放满,只要定出区间的边界就好办了: 这 ...

  9. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

随机推荐

  1. How to watch property in attrs of directive

    Q: I have a controller that has a counter that changes from time to time. That counter is tied to an ...

  2. requestLayout, invalidate和postInvalidate的异同

    requestLayout 当一个VIEW的布局属性发生了变化的时候,可以调用该方法,让父VIEW调用onmeasure 和onlayout重新定位该view的位置,需要在UI线程调用 invalid ...

  3. c++11: thread_local

    thread_local变量是C++ 11新引入的一种存储类型.它会影响变量的存储周期(Storage duration),C++中有4种存储周期: automatic static dynamic ...

  4. java格式化数字、货币、金钱

    网上摘来的,以后可能会用到 java开发中经常会有数字.货币金钱等格式化需求,货币保留几位小数,货币前端需要加上货币符号等.可以用java.text.NumberFormat和java.text.De ...

  5. SQl Server 函数篇 数学函数,字符串函数,转换函数,时间日期函数

    数据库中的函数和c#中的函数很相似 按顺序来, 这里价格特别的 print  可以再消息栏里打印东西 数学函数 ceiling()  取上限   不在乎小数点后面有多大,直接忽略 floor()   ...

  6. Luogu 3233 [HNOI2014]世界树

    BZOJ 3572 首先看出虚树,然后考虑如何$dp$. 我们先在处理出的虚树上$dp$一遍,处理出虚树上所有点距离最近的关键点(关键点一定在虚树上嘛). 具体来说,先搜一遍处理出每一个点的父亲到它的 ...

  7. Javascript parseInt()和parseFloat()的用法

    parseInt()方法首先查看位置0处的 字符,判断它是否是个有效数字:如果不是,该方法将返回NaN,不再继续执行其他操作.但如果该字符是有效数字,该方法将查看位置1处的字符,进行同样的 测试.这一 ...

  8. 原型模式--其实就是考察clone

    http://blog.csdn.net/zhengzhb/article/details/7393528

  9. PartyLocation的Post请求问题---debug

    这里,遇到了一个debug: @Override public void setPrimaryPartyLocation(PartyLocation partyLocation) { if (!get ...

  10. repo的一些用法

    repo的用法注:repo只是google用Python脚本写的调用git的一个脚本,主要是用来下载.管理Android项目的软件仓库.(也就是说,他是用来管理给git管理的一个个仓库的) 1.下载r ...