树状数组的成段更新。

首先要明白,insert函数的意思是更新某一点值,query函数的意思是从起点到某一点的和。

更新[a,b]段时,在a点插入一个1,在b+1点插入一个-1。这时,query(a)...query(b)都是1,而query(1)...query(a-1),query(b+1)...query(N)都是0。

这样就跟成段更新的效果相同。

 #include <cstdio>
#include <cstring> using namespace std; const int maxn = ;
int C[maxn];
int N; int lowbit(int x)
{
return x&(-x);
} int Query(int x)
{
int rtn=;
while(x > )
{
rtn += C[x];
x -= lowbit(x);
}
return rtn;
} void Insert(int x,int num)
{
while(x <= N)
{
C[x] += num;
x += lowbit(x);
}
} int main()
{
while(scanf("%d",&N) && N)
{
memset(C,,sizeof C);
for(int i=;i<N;i++)
{
int a,b;
scanf("%d%d",&a,&b);
Insert(a,);
Insert(b+,-);
}
for(int i=;i<=N;i++)
printf("%s%d",i==?"":" ",Query(i));
printf("\n");
}
}

HDU 1556-Color the ball-树状数组的更多相关文章

  1. HDOJ/HDU 1556 Color the ball(树状数组)

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...

  2. HDU 1556 Color the ball (树状数组区间更新)

    水题,练习一下树状数组实现区间更新. 对于每个区间,区间左端点+1,右端点的后一位-1,查询每个位置的覆盖次数 #include <cstdio> #include <cstring ...

  3. HDU 1556 Color the ball 树状数组 题解

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动 ...

  4. Color the ball(树状数组+线段树+二分)

    Color the ball Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  5. HDU 1556 Color the ball【差分数组裸题/模板】

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...

  6. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  9. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  10. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

随机推荐

  1. [翻译] ASP.NET Core 利用 Docker、ElasticSearch、Kibana 来记录日志

    原文: Logging with ElasticSearch, Kibana, ASP.NET Core and Docker 一步一步指导您使用 ElasticSearch, Kibana, ASP ...

  2. 深入理解Redis Cluster

    Redis Cluster采用虚拟槽分区,所有的key根据哈希函数映射到0~16383槽内,计算公式: slot = CRC16(key) & 16383 每个节点负责维护一部分槽以及槽所映射 ...

  3. RNG牛掰!

    2018-05-21 RNG牛掰!Uzi圆梦! 不说了,先去哭了! 2018-07-08 洲际赛后更新,RNG依然牛逼! 2018-08-30 亚运后后更新,UZI加油! 2018-10-22 继续加 ...

  4. Python_复习_34

    +# 函数 —— 2天 # 函数的定义和调用 # def 函数名(形参): #函数体 #return 返回值 #调用 函数名(实参) # 站在形参的角度上 : 位置参数,*args,默认参数(陷阱), ...

  5. CRM系统(第三部分)

      阅读目录 1.销售与客户的表结构 2.公共客户池 3.确认跟进 4.我的客户 5.code 1.销售与客户的表结构 1.公共客户与我的客户 ---公共客户(公共资源) 1.没有报名 2.3天没有跟 ...

  6. 05Hadoop 概论

    Hadoop的思想之源:Google Google搜索引擎,Gmail,安卓,AppspotGoogle Maps,Google earth,Google 学术,Google翻译,Google+,下一 ...

  7. echarts使用笔记五:echarts的Zoom控件

    option = { title: { text: '趋势' }, tooltip : { trigger: 'axis', show:true, axisPointer : { // 坐标轴指示器, ...

  8. git reset的用法

    git reset三个选项 --mix,--hard,--soft 数据 针对每个选项都是操作这个文件. [root@centos demo]# git init Initialized empty ...

  9. Git命令以及常见注意事项

    命令: git init -> 初始化一个git仓库 git clone -> 克隆一个本地库 git pull -> 拉取服务器最新代码 git fetch –p -> 强行 ...

  10. day 7-19 Mysql索引原理与查询优化

    一,介绍 1.什么是索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语 ...