HDU - 1556 Color the ball(树状数组)
Input
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
Sample Output
1 1 1 3 2 1 解题思路:本题可以想到三种方法,暴力, 线段树, 树状数组。暴力的话复杂度为O(n^2),显然会超时。由于我现在还不会线段树,所以就只能树状数组了吧233 这道题树状数组有些不同,以前都是对点更新找区间,这个是对区间更新找点,那么具体怎么做呢,首先要理解树状数组的c[]数组每个元素代表的是一段区间的值,我们把区间抽象成点,Sum(i)可以表示第i个气球被涂色的次数(画张图就可以理解),比如我们要涂色x~y区间的气球,那么我们先对x进行+1更新操作,相当于[x,MaxN]的气球被涂色一次,但由于我们只涂了x~y区间的气球,y+1及其以后的气球应该都没有涂色,那么我们把y+1再进行一次-1的更新操作就可以了,相当于[y+1,MaxN]的气球这次没有被涂色。这样进行操作始终保证了Sum(i)是第i个气球被涂色的次数。
代码:
1 #include<cstdlib> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<string> 6 #include<iostream> 7 #include<algorithm> 8 #include<map> 9 #include<vector> 10 using namespace std; 11 const int MaxN = 1e5; 12 13 int n; 14 int num[MaxN+5], c[MaxN+5]; 15 16 int lowbit(int x) 17 { 18 return x&(-x); 19 } 20 21 void Add(int x, int y) 22 { 23 for(int i = x;i <= n;i += lowbit(i)) 24 { 25 c[i] += y; 26 } 27 } 28 29 int Sum(int x) 30 { 31 int ans = 0; 32 for(int i = x;i > 0;i -= lowbit(i)) 33 { 34 ans += c[i]; 35 } 36 return ans; 37 } 38 39 40 int main() 41 { 42 int a, b; 43 while(cin >> n && n) 44 { 45 memset(num, 0, sizeof(num)); 46 memset(c, 0, sizeof(c)); 47 for(int i = 1;i <= n;i++) 48 { 49 cin >> a >> b; 50 Add(a, 1); 51 Add(b + 1, -1); 52 } 53 printf("%d", Sum(1)); 54 for(int i = 2;i <= n;i++) 55 { 56 printf(" %d", Sum(i)); 57 } 58 printf("\n"); 59 } 60 return 0; 61 }
转载于:https://www.cnblogs.com/ScaleCX/p/9307780.html
HDU - 1556 Color the ball(树状数组)相关推荐
- HDOJ/HDU 1556 Color the ball(树状数组)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...
- HDU1556 Color the ball(树状数组+抖机灵)
题目: Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 1556 Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 解题报告:hdu 1556 Color the ball(区间修改,单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N ...
- hdu 5157(manacher+前缀和+树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5157 解题思路: 我们可以先用mancher算法对字符串进行处理,把以每个点为中心的回文串半径求出来, ...
- HDU 5517---Triple(二维树状数组)
题目链接 Problem Description Given the finite multi-set A of n pairs of integers, an another finite mult ...
- hdu 6447YJJ's Salesman 离散化+树状数组+DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 因为图中点的坐标值过大,达到1e9.然而只有1e5个点.所以先将其离散化.并按照<x.y& ...
- [HDU - 2852] KiKi's K-Number (树状数组+二分)
链接 http://acm.hdu.edu.cn/showproblem.php?pid=2852 题意 现在需要你对一个空序列做nnn次操作,操作分三种 0x0\ \ x0 x :向序列中加入一个 ...
最新文章
- pytorch bert文本分类_一起读Bert文本分类代码 (pytorch篇 四)
- solaris vi 方向键问题
- springboot 集成 swagger 自动生成API文档
- 饿了么超级会员,年卡低至108元!饿了么会员,点外卖超省钱!
- PDH光端机的作用及其特点
- eBPF and IO Visor: The what, how, and what next!
- Sharepoint学习笔记---Debug--使用ULS Log跟踪Solution错误信息
- 2008年12月信息处理技术员上午试卷 51CTO版参考答案
- vuejs+webpack环境搭建
- oneproxy mysql_Mysql 中间件 oneProxy总结
- 业务、架构、技术,我们应该关注什么 Java和.Net的优势劣势简单看法 市场经济决定,商业之道即是软件之道,市场的需求决定着软件技术的发展 利益决定着选择应用新技术...
- 民生银行用户画像搭建与应用 / 民生银行客户画像搭建与应用
- 屏幕录像专家 - 视频压缩教程
- Ubuntu 19.04 磁盘加密
- 前台、中台、后台到底是什么?
- android sim卡状态改变广播,android监听SIM状态
- 老主板怎么接机箱前面的USB和mic
- html将页面分成四部分,将HTML页面拆分为定义的宽度和高度部分
- 万兴剪刀手去水印教程_万兴神剪手怎么去水印教程_赶快收藏吧
- 前端小白浅谈seo优化以及web性能优化方案