这题用线段树的话简直就是一个水题。。不过刚学树状数组,要用一下。

题意:每次给你a,b,表明a~b之间涂色,然后最后一次输出每个气球被涂色的次数。

要用树状数组就要考虑怎么转化为前缀和问题,这题可以这样做:每次输入a,b,令A[a] = 1,A[b+1] = -1; 然后更新和,查询的时候容易知:a~b之间都被涂了一次,求前缀和结果也为一次,多次插入a,b,性质不变,插入后即可直接输出。复杂度很小,这也是树状数组的好处。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100100 int c[N];
int n; int lowbit(int k)
{
return k&(-k);
} void insert(int k,int num)
{
while(k<=n)
{
c[k] += num;
k += lowbit(k);
}
} int getsum(int k)
{
int sum = ;
while(k>)
{
sum += c[k];
k -= lowbit(k);
}
return sum;
} void init()
{
int a,b,i;
memset(c,,sizeof(c));
for(i=;i<=n;i++)
{
scanf("%d%d",&a,&b);
insert(a,);
insert(b+,-);
}
} int main()
{
int j;
while(cin>>n && n)
{
init();
for(j=;j<n;j++)
{
cout<<getsum(j)<<" ";
}
cout<<getsum(j)<<endl;
}
return ;
}

代码参考了:http://blog.csdn.net/lulipeng_cpp/article/details/7816527

HDU 1556 Color the ball的更多相关文章

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

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

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

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

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

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

  4. HDU 1556 Color the ball (数状数组)

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

  5. 线段树(求单结点) hdu 1556 Color the ball

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

  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 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  8. hdu 1556 Color the ball (线段树+代码详解)

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

  9. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  10. hdu 1556 Color the ball 线段树

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

随机推荐

  1. javascript获取url信息的常见方法

    先以"http://www.cnblogs.com/wuxibolgs329/p/6188619.html#flag?test=12345"为例,然后获得它的各个组成部分. 1.获 ...

  2. andriod CheckBox

    <?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:orientatio ...

  3. Atitit.jsou html转换纯文本 java c# php

    Atitit.jsou html转换纯文本 java c# php 1. 原理<p> <h> <li><div> 等lable转换为回车1 2. 调用2 ...

  4. [ html canvas createImageData 创建万花筒效果 ] canvas绘图属性 createImageData 属性讲解 及创建万花筒效果

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  5. SharePoint 2013 JQuery Asset Picket

      var b = new AssetPickerConfig(""); b.ClientID = ""; b.DefaultAssetLocation = & ...

  6. 【读书笔记】iOS-装箱

    通常将一个基本类型的数据包装成对象叫做装箱,从对象中提取基本类型的数据叫做取消装箱.有些语言有自动装箱功能,它可以自动包装基本基础类型的数据,也可以自动从包装后的对象中提取基础数据.Objective ...

  7. 软件工程(c编码实践) 学习笔记(一)

    vim 有三种模式:一般模式,编辑模式,命令模式. -------------------------------------------------------------------------- ...

  8. iOS开发之网络编程--使用NSURLConnection实现文件上传

    前言:使用NSURLConnection实现文件上传有点繁琐.    本文并没有介绍使用第三方框架上传文件. 正文: 这里先提供用于编码测试的接口:http://120.25.226.186:3281 ...

  9. Windows 编 程中的字符串

    (1)在win32编程中,如何使用string类型 #include <string> using namespace std; LPTSTR    lpCmdLine = L" ...

  10. 阿里云ECS/Ubuntu下JDK、Tomcat、MySQL安装记录

    今天六一儿童节,然后... ... ... ... 然后就是父亲节呀孩子们!!! ———————————————————————割———————————————————————— 同事需要JDK.To ...