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. Express 框架的安装

    从零开始用 Node.js 实现一个微博系统,功能包括路由控制.页面模板.数据库访问.用户注册.登录.用户会话等内容. Express 框架. MVC 设计模式. ejs 模板引擎 MongoDB 数 ...

  2. thinkphp---定义前台视图模板

    具体可以参考: http://document.thinkphp.cn/manual_3_2.html#template_define 方法一:在入口文件中定义 // 定义模板路径 define(&q ...

  3. 豆瓣api开发

    前面有说过豆瓣API的开发,在做一些开源项目的时候,很多时候会用到豆瓣API接口,拿过来做测试,现在只是对豆瓣API开发做一些简单的梳理: 豆瓣API开发的接口: https://developers ...

  4. 310实验室 Linux 软件安装常见问题

    电脑装好Linux系统后,系统默认安装软件不齐,需要自己根据自己的需要安装一些列工具软件,在linux系统中,用的最多的就是Synaptic Package Manager,什么是Synaptic?新 ...

  5. angular-translate国际化

    1.<h1>{{"hello" | translate}}</h1>2.<h1 ng-bind-html="'hello' | transl ...

  6. Allocation Sinking Optimization

    LuaJIT Sponsorship Program http://luajit.org/sponsors.html Sponsorship for allocation/store sinking ...

  7. jira-source--plugin

    X11 forward impact on jira plugin system initialization. 而Linux的curl使用的证书库在文件“/etc/pki/tls/certs/ca- ...

  8. No module named pip.req

    https://stackoverflow.com/questions/25192794/no-module-named-pip-req#

  9. 使用celery之了解celery(转)

    原文  http://www.dongwm.com/archives/shi-yong-celeryzhi-liao-jie-celery/   前言 我想很多做开发和运维的都会涉及一件事:cront ...

  10. MySQL的知识海洋

    第一篇:初识数据库 第二篇:库操作 第三篇:表操作 第四篇:数据操作 第五篇:视图.触发器.存储过程.函数.事物与数据库锁 第六篇:索引原理与慢查询优化 第七篇:pymysql(用python连接以及 ...