备忘。

差分数组:

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

假设对于区间[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~

代码:

  1. //HDU 1556-差分数组 备忘
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<bitset>
  7. #include<cassert>
  8. #include<cctype>
  9. #include<cmath>
  10. #include<cstdlib>
  11. #include<ctime>
  12. #include<deque>
  13. #include<iomanip>
  14. #include<list>
  15. #include<map>
  16. #include<queue>
  17. #include<set>
  18. #include<stack>
  19. #include<vector>
  20. using namespace std;
  21. typedef long long ll;
  22.  
  23. const double PI=acos(-1.0);
  24. const double eps=1e-;
  25. const ll mod=1e9+;
  26. const int inf=0x3f3f3f3f;
  27. const int maxn=1e5+;
  28. const int maxm=+;
  29. #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  30. #define lson l,m,rt<<1
  31. #define rson m+1,r,rt<<1|1
  32.  
  33. int a[maxn],ans[maxn];
  34.  
  35. int main()
  36. {
  37. int n;
  38. while(scanf("%d",&n)){
  39. if(n==) break;
  40. memset(a,,sizeof(a));
  41. memset(ans,,sizeof(ans));
  42. for(int i=;i<n;i++){
  43. int l,r;
  44. scanf("%d%d",&l,&r);
  45. a[l]+=;
  46. a[r+]-=;
  47. }
  48. ans[]=;
  49. for(int i=;i<=n;i++){
  50. ans[i]=ans[i-]+a[i];
  51. }
  52. for(int i=;i<n;i++)
  53. printf("%d ",ans[i]);
  54. printf("%d\n",ans[n]);
  55. }
  56. }

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. [剑指Offer] 18.二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5 [思路1 ...

  2. 【bzoj1458】士兵占领 有上下界最小流

    题目描述 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放置了Li个士兵 ...

  3. LeetCode -- Best Time to Buy and Sell Stock系列

    Question: Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pri ...

  4. EOS docker开发环境

    EOS Wiki提供了有关如何使用docker容器编译最新版本代码的说明.但可能有它自己的一些问题,因此我们鼓励你在学习时引用下面镜像.这样最初会更容易,更快. 如果你还没有安装docker,请在此处 ...

  5. Small things are better

    Yesterday I had fun time repairing 1.5Tb ext3 partition, containing many millions of files. Of cours ...

  6. HDU 1059 完全背包

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  7. poj 2104 (主席树写法)

    //求第K的的值 1 #include<stdio.h> #include<iostream> #include<algorithm> #include<cs ...

  8. codeforces 1060 B

    https://codeforces.com/contest/1060/problem/B 题意:给你一个数C ,你要找到两个数A.B,使得A+B=C并且A的每个位的数的和最大,求最大的和是多少 题解 ...

  9. spring4.3注解

     Spring4.3中引进了 {@GetMapping.@PostMapping.@PutMapping.@DeleteMapping.@PatchMapping},分别对应这个查询,插入,更新,删除 ...

  10. hbase监控实现

    目前实现的监控概览