HDU 1556.Color the ball-差分数组-备忘
备忘。
差分数组:
区间更新查询有很多方法,线段树、树状数组等都可以。如果为离线查询,就可以考虑使用差分数组。
假设对于区间[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
当N = 0,输入结束。
题意很好理解,就是区间更新然后查询,是离线操作,差分数组登场,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-差分数组-备忘的更多相关文章
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball【差分数组裸题/模板】
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...
- HDU 1556 Color the ball (数状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556 Color the ball (树状数组)
Color the ballTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
- 线段树(求单结点) hdu 1556 Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556 Color the ball(区间更新,单点求值)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- [剑指Offer] 18.二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5 [思路1 ...
- 【bzoj1458】士兵占领 有上下界最小流
题目描述 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放置了Li个士兵 ...
- 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 ...
- EOS docker开发环境
EOS Wiki提供了有关如何使用docker容器编译最新版本代码的说明.但可能有它自己的一些问题,因此我们鼓励你在学习时引用下面镜像.这样最初会更容易,更快. 如果你还没有安装docker,请在此处 ...
- Small things are better
Yesterday I had fun time repairing 1.5Tb ext3 partition, containing many millions of files. Of cours ...
- HDU 1059 完全背包
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- poj 2104 (主席树写法)
//求第K的的值 1 #include<stdio.h> #include<iostream> #include<algorithm> #include<cs ...
- codeforces 1060 B
https://codeforces.com/contest/1060/problem/B 题意:给你一个数C ,你要找到两个数A.B,使得A+B=C并且A的每个位的数的和最大,求最大的和是多少 题解 ...
- spring4.3注解
Spring4.3中引进了 {@GetMapping.@PostMapping.@PutMapping.@DeleteMapping.@PatchMapping},分别对应这个查询,插入,更新,删除 ...
- hbase监控实现
目前实现的监控概览