1、HDU 1556  Color the ball   区间更新,单点查询

2、题意:n个气球,每次给(a,b)区间的气球涂一次色,问最后每个气球各涂了几次。

(1)树状数组

总结:树状数组是一个查询和修改复杂度都为log(n)的数据结构。主要用于查询任意两位之间的所有元素之和,但是每次只能修改一个元素的值。

这里改下思路可以用树状数组。在更新(a,b)时,向上更新,将a~n加1,b+1~n减1。查询点时,向下求和即可。

#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstdio>
#define F(i,a,b) for (int i=a;i<b;i++)
#define FF(i,a,b) for (int i=a;i<=b;i++)
#define mes(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=,MAX=; int n,c[MAX]; int lowbit(int x) {
//计算2^k
return x&-x;
} void update(int x,int val)
{
//向上更新,使所有包含了x的区间都更新一下
while(x<=n) {
c[x]+=val;
x+=lowbit(x);
}
} int Sum(int x)
{
//向下查询
int sum=;
while(x>) {
sum+=c[x];
x-=lowbit(x);
}
return sum;
} int main()
{
while(~scanf("%d",&n),n) {
mes(c,);
int a,b;
FF(i,,n) {
scanf("%d%d",&a,&b);
update(a,);
update(b+,-);
}
F(i,,n) printf("%d ",Sum(i));
printf("%d\n",Sum(n));
} return ;
}

(2)线段树+lazy思想

总结:lazy,更新时只到区间,可以节省很多时间。

#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstdio>
#define F(i,a,b) for (int i=a;i<b;i++)
#define FF(i,a,b) for (int i=a;i<=b;i++)
#define mes(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=,MAX=; int n,c[MAX<<]; void build(int o,int L,int R)
{
c[o]=;
if(L==R) return ;
int mid=(L+R)>>;
build(o<<,L,mid);
build(o<<|,mid+,R);
} void update(int o,int l,int r,int L,int R)
{
if(l<=L&&R<=r) { c[o]++; return ; } //关键:只更新到区间,一开始更新到每个单独的点,果断T了,//然后用l==L&&R==r,又果断MLE
int mid=(L+R)>>;
if(mid<l) update(o<<|,l,r,mid+,R);
else if(r<=mid) update(o<<,l,r,L,mid);
else {
update(o<<,l,r,L,mid);
update(o<<|,l,r,mid+,R);
}
} int ans[MAX];
void query(int o,int L,int R)
{
if(c[o]) {
for(int i=L; i<=R; i++)
ans[i]+=c[o]; //也是lazy思想,标记了的区间就加上
}
if(L==R) return ; //询问时还是要到点
int mid=(L+R)>>;
query(o<<,L,mid);
query(o<<|,mid+,R);
} int main()
{
while(~scanf("%d",&n) ,n )
{
build(,,n);
fill(ans+,ans++n,); //fil函数
int a,b;
FF(i,,n) {
scanf("%d%d",&a,&b);
update(,a,b,,n);
}
query(,,n);
F(i,,n) printf("%d ",ans[i]);
printf("%d\n",ans[n]);
} return ;
}

HDU 1556 线段树或树状数组,插段求点的更多相关文章

  1. Codeforces 390E Inna and Large Sweet Matrix 树状数组改段求段

    题目链接:点击打开链接 题意:给定n*m的二维平面 w个操作 int mp[n][m] = { 0 }; 1.0 (x1,y1) (x2,y2) value for i : x1 to x2 for ...

  2. hdu 4970 树状数组 “改段求段”

    题意:塔防.给1--n,给出m个塔,每个塔有攻击力,给出k个怪兽的位子和血量,问有几只可以到达n点. 今天刚刚复习了树状数组,就碰到这个题,区间更新.区间求和类型.第三类树状数组可以斩. 注意一下大数 ...

  3. POJ3468--A Simple Problem with Integers--线段树/树状数组 改段求段

    题目描述 You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type ...

  4. CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段)

    树状数组仅仅能实现线段树区间改动和区间查询的功能,能够取代不须要lazy tag的线段树.且代码量和常数较小 首先定义一个数组 int c[N]; 并清空 memset(c, 0, sizeof c) ...

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

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

  6. HDU 1556 线段树/树状数组/区间更新姿势 三种方法处理

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

  7. HDU 1166 敌兵布阵 (数状数组,或线段树)

    题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

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

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

  9. poj2299树状数组入门,求逆序对

    今天入门了树状数组 习题链接 https://blog.csdn.net/liuqiyao_01/article/details/26963913 离散化数据:用一个数组来记录每个值在数列中的排名,不 ...

随机推荐

  1. [Unity] Shader - CG语言 流程控制语句

    CG语言中: 不支持 switch 语句(可以写,但不能很好的执行.) 循环语句中, 循环次数不能大于 1024 ,否则会报错. If...ELSE 条件判断语句: if (true) { } els ...

  2. mysql order by 优化 |order by 索引的应用

    在某些场景,在不做额外的排序情况下,MySQL 可以使用索引来满足 ORDER BY 子句的优化.虽然 ORDER BY并不完全精确地匹配索引,但是索引还是会被使用,只要在WHERE子句中,所有未被使 ...

  3. js中join和split的用法

  4. arguments 对象的老历史

    引题:为什么 JavaScript 中的 arguments 对象不是数组 http://www.zhihu.com/question/50803453 JavaScript 1.0 1995 年, ...

  5. 【推荐】MySQL Cluster报错及解决方法(不断更新中)

    排查问题技巧: MySQL Cluster 自带了一个错误代码的查看的小程序.通过这个小东西我们可以方便的定位问题的原因. 这个程序就是 perror 在MYSQL安装目录的bin下面. 如报错:ER ...

  6. update语句关联表更新

    UPDATE dbo.NodeInstance SET OrderNumber=temp.OrderNo FROM dbo.NodeInstance ins,dbo.NodeTemplate temp ...

  7. jquery 页面加载时获取图片高度

    $(function () { $(window).load(function(){ alert($('img').height()); }); });

  8. am335x watchdog 设备出错

    问题描述: am335x watchdog 设备节点打开失败. 如果是直接将omap_wdt 直接编译成uImage,这样会出现打开文件节点失败的情况. 如果单独编译成模块在后面文件系统内插入则不会. ...

  9. 网页Screen width、height、availWidth、availHeight属性

    *screen.width 功能:声明了显示浏览器的屏幕的宽度,以像素计. 语法:screen.width *screen.height 功能:声明了显示浏览器的屏幕的可用宽度,以像素计. 语法:sc ...

  10. 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用

    本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSInvocationOperation.NSOperationQueue的使用,列举几个简单的例子. 默认情况下 ...