HDU 1556 Color the ball(线段树:区间更新)
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(线段树:区间更新)的更多相关文章
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556 Color the ball 线段树 区间更新
水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- hdu 1556 Color the ball (线段树+代码详解)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
- HDU 1556 Color the Ball 线段树 题解
本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...
- Color the ball 线段树 区间更新但点查询
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...
- hdu1556Color the ball线段树区间更新
题目链接 线段树区间更新更新一段区间,在更新区间的过程中,区间被分成几段,每一段的左右界限刚好是一个节点的tree[node].left和tree[node].right(如果不是继续分,直到是为止) ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
随机推荐
- 【BZOJ2661】[BeiJing wc2012]连连看 最大费用流
[BZOJ2661][BeiJing wc2012]连连看 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给 ...
- ios UIImage图片拉伸 resizableImageWithCapInsets:
常见的按钮添加和背景设置如下: UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(80, 130, 160, 44)];[bu ...
- squid 高匿设置
1.透明代理.匿名代理.混淆代理.高匿代理有什么区别 这4种代理,主要是在代理服务器端的配置不同,导致其向目标地址发送请求时,REMOTE_ADDR, HTTP_VIA,HTTP_X_FORWARDE ...
- javascript飞机大战-----0010完整版代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Oracle Schema Objects——Index
索引主要的作用是查询优化. Oracle Schema Objects 查看执行计划的权限:查看执行计划plustrace:set autotrace trace exp stat(SP2-0618. ...
- Django框架【基础篇】
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- vue.js(四)
由于组件内容太多又特别关键,我决定在官网教程的基础上,加上自己的理解,针对每个内容详细记录一下 1.注册组件 ①全局注册 //首先创建组件 Vue.component('blog-post', { ...
- rpc、socket、mq
关于RPC与MQ异同的理解 相同:1.都利于大型系统的解耦:2.都提供子系统之间的交互,特别是异构子系统(如java\node等不同开发语言):不同:1.RPC侧重功能调用,因此多半是同步的:备注:也 ...
- Drawing Graphs using Dot and Graphviz
Drawing Graphs using Dot and Graphviz Table of Contents 1. License 2. Introduction 2.1. What is DOT? ...
- python知识大全目录,想学的看过来!
Python总结篇——知识大全 python装饰器 PyCharm安装与配置,python的Hello World sort与sorted的区别及实例 我必须得告诉大家的MySQL优化 ...