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. [Head First设计模式]身边的设计模式——适配器模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  2. 前端工具之Gulp

    Gulp是一款前端自动化的工具,如果能熟练使用Gulp来进行开发一定可以节省很多的时间,也可以快速的提高工作效率. 在使用Gulp之前就是要配置好Gulp安装的环境,这是我们能使用Gulp快速开发的第 ...

  3. Pandas-数据探索

    Pandas包对数据的常用探索功能,方便了解数据描述性属性. 目录 基础属性 shape indexs columns values dtype/dtypes 汇总和计算描述统计 count() va ...

  4. 常用的Javascript设计模式

    <parctical common lisp>的作者曾说,如果你需要一种模式,那一定是哪里出了问题.他所说的问题是指因为语言的天生缺陷,不得不去寻求和总结一种通用的解决方案. 不管是弱类型 ...

  5. [转载]Java数组扩容算法及Java对它的应用

    原文链接:http://www.cnblogs.com/gw811/archive/2012/10/07/2714252.html Java数组扩容的原理 1)Java数组对象的大小是固定不变的,数组 ...

  6. ASP.NET正则表达式(URL,Email)

    public static bool IsUrl(this string str)    {        if (str.IsNullOrEmpty())            return fal ...

  7. WebService的创建发布及部署

    1.打开win7的IIS功能: http://jingyan.baidu.com/article/2a138328ae4b85074b134f55.html 2.IIS注册Frameworkt4.0 ...

  8. Class.forName的使用

    Class.forName的使用 Class.forName返回一个类,使用此方法可以获取类 首先,创建一个Student类 /*** * This Class is for Student bean ...

  9. Effective Python2 读书笔记1

    Item 2: Follow the PEP 8 Style Guide Naming Naming functions, variables, attributes lowercase_unders ...

  10. STL_lower_bound&upper_bound用法

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...