A - Color the ball HDU - 1556 (差分数组+前缀和)
思路等引自博客 https://blog.csdn.net/johnwayne0317/article/details/84928568
对数组a[7]:
a[0]=1; = d[0]
a[1]=1; = d[1]+d[0]
a[2]=1; = d[2]+d[1]+d[0]
a[3]=1; ..................
a[4]=1;
a[5]=1;
a[6]=1;
a[7]=1; = d[7]+d[6]+d[5]+d[4]+d[3]+d[2]+d[1]+d[0]
如果要对a[0]~a[4]都加1: 最朴素的方法是: a[0]++;a[1]++。。。。。时间复杂度为o(n);所以要引入插分数组。
a[0]=d[0]; d[1]=a[1]-a[0]; d[2]=a[2]-a[1] d[3]=a[3]-a[2]-a[1]; 由此得出以上结论。
如果现在要修改a[0]-a[4],只需要把d[0]+1(所有的a都有d[0]的元素),然后把d[5]-1(a[5]及以后的数字都有d[5]的元素)
a[0]=d[0]
a[1]=d[1]+d[0]+1
a[2]=d[2]+d[1]+d[0]+1
a[3]=d[3]+…+d[0]+1
a[4]=d[4]+…+d[0]+1
a[5]=d[5]-1+…+d[0]+1
a[6]=d[6]+d[5]-1+…+d[0]+1
a[5]=d[7]+d[6]+d[5]-1+…+d[0]+1
差分数组的时间复杂度是O(2) 区间修改就变成了两个单点修改,
所以呢根据上一段有核心代码:
for(int i=;i<=n;i++)
{
cin>>a>>b;
d[a]++;
d[b+]--;
}
再根据前缀和(因为有上述规律 : a[3]=d[3]+d[2]+d[1]+d[0])当然本题里没有a[0]哈。
for(int i=;i<=n;i++)
d[i]+=d[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
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll d[];
int main()
{
ll n;
while(cin>>n)
{
if(n==)
break;
memset(d,,sizeof(d));
ll a,b;
for(int i=;i<=n;i++)
{
cin>>a>>b;
d[a]++;
d[b+]--;
}
for(int i=;i<=n;i++)
d[i]+=d[i-];
for(int i=;i<n;i++)
printf("%lld ",d[i]);
printf("%lld\n",d[n]);
}
}
A - Color the ball HDU - 1556 (差分数组+前缀和)的更多相关文章
- Color the ball HDU - 1556 (非线段树做法)
题意:在1到n的气球中,在不同的区域中涂颜色,问每个气球涂几次. #include<cstdio>int num[100010];int main(){ int n, x, y;; whi ...
- Color the ball HDU - 1556 (线段树)
思路:线段树,区间更新 #include<iostream> #include<vector> #include<string> #include<cmath ...
- 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 ...
- 洛谷 U14472 数据结构【比赛】 【差分数组 + 前缀和】
题目描述 蒟蒻Edt把这个问题交给了你 ---- 一个精通数据结构的大犇,由于是第一题,这个题没那么难.. edt 现在对于题目进行了如下的简化: 最开始的数组每个元素都是0 给出nnn,optopt ...
- hdu 1556 Color the ball(树状数组)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数[a,b]之间的气球 ...
- HDU 1556 Color the ball (树状数组 区间更新+单点查询)
题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...
- HDU 1556 Color the ball【树状数组】
题意:给出n个区间,每次给这个区间里面的数加1,询问单点的值 一维的区间更新,单点查询,还是那篇论文里面讲了的 #include<iostream> #include<cstdio& ...
- Color the ball(HDU1556)树状数组
每次对区间内气球进行一次染色,求n次操作后后所有气球染色次数. 树状数组,上下区间更新都可以,差别不大. 1.对于[x,y]区间,对第x-1位减1,第y位加1,之后向上统计 #include<b ...
随机推荐
- delphi base64编码
需要uses IdCoderMIME: function TForm1.Base64E(Path: string): string;var filepath: string; filestream: ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-star-empty
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- UVA - 712 S-Trees(S树)
题意:0往左走,1往右走,已知所有叶子的值,每个查询都是根结点到叶子结点的路径,路径的每一个点分别对应着x1,x2,x3……但是实际上的S树的路径可能并非是x1,x2,x3…… 分析:先存路径变量的顺 ...
- 每天一点点之 uni-app 框架开发 - 页面滚动到指定位置
项目需求:在页面中,不管位于何处,点击评论按钮页面滚动到对应到位置 实现思路如下: uni.createSelectorQuery().select(".comment").bou ...
- python学习笔记2018-9-17
1.print("{0:^30}\n{1:^30}\n{1:10}".format("age","name")) {0:^30}中的0是一个 ...
- 云时代架构阅读笔记六——Java内存模型详解(二)
承接上文:云时代架构阅读笔记五——Java内存模型详解(一) 原子性.可见性.有序性 Java内存模型围绕着并发过程中如何处理原子性.可见性和有序性这三个特征来建立的,来逐个看一下: 1.原子性(At ...
- kNN.py源码及注释(python3.x)
import numpy as npimport operatorfrom os import listdirdef CerateDataSet(): group = np.array( ...
- 136-PHP 使用类名访问static修饰的类方法
<?php class test{ //定义一个类 public static function class_info(){ //定义类方法 return '这是一个用于测试的类.'; } } ...
- Elasticsearch 修改数据
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
- Apache nifi 第二篇(小白初试) nifi数据对接流程初次尝试
一.准备工作 1.官网下载nifi 2.上传到linux随便哪里把,因为nifi是用java写的,所以首先要保证你的linux装了jdk 其次保证系统在装了zookeeper,因为nifi是一个分布 ...