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. SSH电力项目二

    底层方法封装(CommonDaoImpl类) public class CommonDaoImpl<T> extends HibernateDaoSupport implements IC ...

  2. OC开发_Storyboard——AutoLayout

    一.autolayout 自动布局: 1. 设置所有视图框架的三种方法,可以通过代码创建也可以storyboard设置 = 规则 (1 蓝线+约束:(位置) 使用蓝线,根据蓝线拖动控件,只是告诉Xco ...

  3. 模拟退火算法(西安网选赛hdu5017)

    Ellipsoid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  4. 170703、springboot编程之模板使用(thymeleaf、freemarker)

    官方不推荐集成jsp,关于使用jsp模板我这里就不赘述,如果有需要的,请自行百度! thymeleaf的使用 1.在pom中增加thymeleaf支持 <dependency> <g ...

  5. vue报错 vue-cli 引入 stylus 失败

    1.1.1.   vue-cli 引入 stylus 失败 先通过vue-cli的webpack模板建立文件夹: vue init webpack test-stylus 然后安装依赖 npm ins ...

  6. VM+CentOS+hadoop2.7搭建hadoop完全分布式集群

    写在前边的话: 最近找了一个云计算开发的工作,本以为来了会直接做一些敲代码,处理数据的活,没想到师父给了我一个课题“基于质量数据的大数据分析”,那么问题来了首先要做的就是搭建这样一个平台,毫无疑问,底 ...

  7. Nginx文件下载服务器

    1. 配置文件 server { listen 80; #端口 server_name localhost; #服务名 charset utf-8; #避免中文乱码 root /data/packag ...

  8. 信号(Django信号、Flask信号、Scrapy信号)

    简介 Django.Flask.scrapy都包含了一个“信号分配器”,使得当一些动作在框架的其他地方发生的时候,解耦的应用可以得到提醒. 通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒 ...

  9. 【转发】Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...

  10. 第六章 优化服务器设置--高性能MySQL 施瓦茨--读书笔记

    MySql的默认配置不适用于使用大量资源,因为其通用性很高. 不要期望改变配置文件会带来巨大的性能提升.提升大小取决于工作负载,通常可以通过选择适当的配置参数得到两到三倍的性能提升.在这时候,性能提升 ...