备忘。

差分数组:

区间更新查询有很多方法,线段树、树状数组等都可以。如果为离线查询,就可以考虑使用差分数组。

假设对于区间[l,r]的每个数都加1,我们用一个数组a来记录,a[l]+=1;a[r+1]-=1;

然后使用一个数组ans来记录数组a的前缀和,ans[i]=ans[i-1]+a[i];ans保存的就是所有更新操作完成后每个数对应的值。

原理很好理解,树状数组也有这种思想。

差分数组代码可比线段树短了相当多,记住这个还是很好的。

Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 29031    Accepted Submission(s): 14125

Problem Description
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
 
Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
 
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
 
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
 
Sample Output
1 1 1
3 2 1
 
Author
8600
 
Source
 

题意很好理解,就是区间更新然后查询,是离线操作,差分数组登场,bling~bling~

代码:

 //HDU 1556-差分数组 备忘
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int a[maxn],ans[maxn]; int main()
{
int n;
while(scanf("%d",&n)){
if(n==) break;
memset(a,,sizeof(a));
memset(ans,,sizeof(ans));
for(int i=;i<n;i++){
int l,r;
scanf("%d%d",&l,&r);
a[l]+=;
a[r+]-=;
}
ans[]=;
for(int i=;i<=n;i++){
ans[i]=ans[i-]+a[i];
}
for(int i=;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
}

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【差分数组裸题/模板】

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

  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 ballTime Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. hdu 1556 Color the ball 线段树

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

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

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

  9. hdu 1556 Color the ball(区间更新,单点求值)

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

随机推荐

  1. Acunetix Web Vulnarability Scanner V10.5 详细中文手册

    目录: 0×00.什么是Acunetix Web Vulnarability Scanner ( What is AWVS?) 0×01.AWVS安装过程.主要文件介绍.界面简介.主要操作区域简介(I ...

  2. Working with large data sets in MySQL

    What does working with large data sets in mySQL teach you ? Of course you have to learn a lot about ...

  3. (转)Django常用命令

    转自GoodSpeed,http://www.cnblogs.com/cacique/archive/2012/09/30/2709145.html . . . . .

  4. webpack 引入 html-webpack-plugin 报错

    配置webpack当中,出现一个问题: 引入html-webpack-plugin 插件报错. 这时需要本地(也就是当前项目下)安装一下webpack就可以解决问题了. 注意:现在是webpack4版 ...

  5. css中clip:rect矩形剪裁功能

    一.示例 img { position:absolute; clip:rect(30px,200px,200px,20px); } 二.理解 clip 属性剪裁绝对定位元素. clip:rect矩形剪 ...

  6. Fragment使用--文章集锦

    android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态 Android Fragment 真正的完全解析(上) Android Fragment实 ...

  7. 图片上传是否为空,以及类型的js验证

    function check2() { var file = document.getElementsByName("file").value; if(file=="&q ...

  8. 【uva1380 - 一个调度问题】思路题+树形dp

    [题意] 有n<=200个恰好需要一天完成的任务,要求用最少的时间完成所有任务.任务可以同时完成.但是有一些约束,分有向和无向两种,其中A-->B表示A必须在B前面完成,而A--B表示A和 ...

  9. 【poj3294-不小于k个字符串中最长公共子串】后缀数组

    1.注意每两个串之间的连接符要不一样. 2.分组的时候要注意最后一组啊!又漏了! 3.开数组要考虑连接符的数量.100010是不够的至少要101000. #include<cstdio> ...

  10. [转载]C#用正则表达式 获取网页源代码标签的属性或值

    最近调试程序需要用到获取网页指定标签的属性和值,找到了一个比较好的正则匹配方法,特此备份. [原]C#用正则表达式 获取网页源代码标签的属性或值 整理两个 在C#中,用正则表达式 获取网页源代码标签的 ...