树状数组的成段更新。

首先要明白,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. [C#] LINQ之Join与GroupJoin

    声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.编写Person与City类,如下: class Person { public int CityID { set; get; } ...

  2. Windows Community Toolkit 4.0 - DataGrid - Part02

    概述 在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Part01 中,我们针对 DataGrid 控件的 CollectionView 部分做了详细 ...

  3. rabbitMQ教程(五)rabbitmq 指令 以及解决web管理界面无法使用guest用户登录

    安装最新版本的rabbitmq(3.3.1),并启用management plugin后,使用默认的账号guest登陆管理控制台,却提示登陆失败. 翻看官方的release文档后,得知由于账号gues ...

  4. 解决win7 win10上网卡慢问题

    在管理员身份运行CMD命令行,然后输入netsh interface tcp set global autotuninglevel=disable 即可.

  5. Python-collections模块-57

    返回顶部 模块的导入和使用 模块的导入应该在程序开始的地方 更多相关内容 http://www.cnblogs.com/Eva-J/articles/7292109.html   常用模块 colle ...

  6. elasticsearch数据输入和输出

    Elastcisearch 是分布式的 文档 存储.它能存储和检索复杂的数据结构–序列化成为JSON文档–以 实时 的方式. 换句话说,一旦一个文档被存储在 Elasticsearch 中,它就是可以 ...

  7. PHP中多个文件包含的问题 (二)

    首先php中有常用的两种方法将文件包含:include和require,而include_once和require_once无非就是升级版而已,这里就不阐述他们的区别,我只提一下我遇到的问题: 先看一 ...

  8. Display Hibernate SQL to console – show_sql , format_sql and use_sql_comments

    (转)灵活控制 Hibernate 的日志或 SQL 输出,以便于诊断 - CS408 - 博客园 https://www.cnblogs.com/lixuwu/p/7479496.html Disp ...

  9. [转帖]50个必知的Linux命令技巧,你都掌握了吗?

    50个必知的Linux命令技巧,你都掌握了吗? https://blog.51cto.com/lizhenliang/2131141 https://blog.51cto.com/lizhenlian ...

  10. picker-view 组件 的value失效问题

    首先检查是不是漏了绑定关系 组件内 组件引用 如过还不行就用下面的方法,顺序问题 在给暂时列表赋值之后再对value赋值