http://acm.hdu.edu.cn/showproblem.php?pid=1556

题意:

N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数。

思路:

这道题目用树状数组和线段树都可以,拿这道题来入门一下线段树的区间更新。

 #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = + ; int n;
int ans[maxn]; struct node
{
int l, r;
int n;
}t[maxn]; int a[maxn]; void build(int l, int r, int o)
{
t[o].l = l;
t[o].r = r;
t[o].n = ;
if (l == r) return;
int mid = (l + r) / ;
build(l, mid, * o);
build(mid + , r, * o + );
} void update(int l, int r, int o)
{
if (t[o].l == l && t[o].r == r)
{
t[o].n++;
return;
}
int mid = (t[o].l + t[o].r) / ;
if (r <= mid) update(l, r, * o);
else if (l > mid) update(l, r, * o + );
else
{
update(l, mid, * o);
update(mid + , r, * o + );
}
} void add(int o)
{
for (int i = t[o].l; i <= t[o].r; i++)
{
ans[i] += t[o].n;
}
if (t[o].l == t[o].r) return;
add( * o);
add( * o + );
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int x, y;
while (~scanf("%d", &n) , n)
{
memset(ans, , sizeof(ans));
build(, n, );
for (int i = ; i <= n; i++)
{
scanf("%d%d", &x, &y);
update(x, y, );
}
add();
for (int i = ; i <= n-; i++)
cout << ans[i] << " ";
cout << ans[n] << endl;
}
}

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. Jmeter中ftp测试下载默认路径及文件

    今天在测试一个FTP下载功能接口时,发现根据官方文档下载可以成功,但找不到文件,管方文档的配置图如下: 根据官方文档,自己建立了一个请求如下: 但实际下载成功时却发现找不到文件 原来,奥秘是: 本地文 ...

  2. MYSQL-max_binlog_cache_size参数

    max_binlog_cache_size 解释:这是设置最大二进制日志的缓存区大小的变量.若处理多语句事务时需要的内存大小比设置值大的话就会提示一个error:Multi-statement tra ...

  3. 输入一个网站地址到网站展现的过程以及APR协议(鬼知道中间经历了什么)

    以前只知道输入一个网站,然后看着返回琳琅满目的内容,其实中间经历的过程和步骤太多了.为了满足好奇心以及学习需要,特查阅了资料将其记录下来以备后续自己复习. 从我在地址栏输入www.zhihu.com ...

  4. Lucene.net之解决锁的问题

    public sealed class SearchIndexManager { private static readonly SearchIndexManager searchManager=ne ...

  5. Sublime 取消每次自动更新弹窗设置

    首选项 --> 设置-用户(英文版  :  "Preferences  - -> "Settings - user"") update_check ...

  6. 解决命令行执行shell脚本成功,但crontab执行失败

    实际生产案例 生产机房自建PPTP客户端通过拨号连接到生产机房,但是一旦客户端网络是意外断线再重新拨号 会产生IP冲突,于是写了一个脚本监控PPTP的IP是否有多个(一般冲突以后会生成2个IP) #! ...

  7. 南京网络赛B-The writing on the wall

    30.43% 2000ms 262144K Feeling hungry, a cute hamster decides to order some take-away food (like frie ...

  8. Linux救援模式

    Linux系统使用版本:CentOS 6.5 救援模式有什么作用: ◆可以更改root密码: ◆恢复硬盘.文件系统操作: ◆系统启动不来的时候,只能通过救援模式来启动: 救援模式启动的步骤如下: 1. ...

  9. Python开发【项目】:RPC异步执行命令(RabbitMQ双向通信)

    RPC异步执行命令 需求: 利用RibbitMQ进行数据交互 可以对多台服务器进行操作 执行命令后不等待命令的执行结果,而是直接让输入下一条命令,结果出来后自动打印 实现异步操作 不懂rpc的请移步h ...

  10. (2.15)Mysql之SQL基础——开发设计最佳规范

    (2.15)Mysql之SQL基础——开发设计最佳规范 关键字:mysql三大范式,mysql sql开发规范 分析: show profile.mysqllsla.mysqldrmpslow.exp ...