HDU 1556 Color the ball - from lanshui_Yang
当N = 0,输入结束。
这是一道典型的一维树状数组的变形,普通的一维树状数组的用途是:单点更新,区间求值。而这道题的则是用到树状数组的另一个用途:区间更新,单点求值。原理如下:
假设原始数组的各个元素为a[1] , a[2] ,…… a[n] , 那么 d[n] = a[1] + a[2] + …… + a[n] 求的就是前n项和,这就是树状数组的第一个用途:单点更新,区间求和。
然后,稍微做些改动,假设原始数组的各个元素为a[1] - 0 , a[2] - a[1] , a[3] - a[2] ,……,a[n] - a[n - 1] , 那么此时的前n项和 d[n] = a[n] ,也就是说,现在原始数组的前n项和d[n] 就等于单点的值a[n] 了 ,大家看到这里是不是就有些明白了呢?
接着,如果你想时区间[ a[m] …… a[n] ] 中的所有值都 + Val ,那么只需将原始数组的第m项 (a[m] - a[m - 1] ) 加上 Val , 和将第n + 1项 (a[n + 1] - a[n]) 减去 Val 就可以了, 这样当 m <= i <= n 时 ,
数列的前 i 项和:
d[i] = (a[1] - 0) + (a[2] - a[1]) + (a[3] - a[2]) + …… + (a[m] - a[m - 1] + val) + (a[m + 1] - a[m]) + …… + (a[i] - a[i - 1] ) = a[i] + val 。
同理当 i > n 时 ,d[i] 等于原来的 a[i] 。看到这里,大家是不是就豁然开朗啦。注意一点,这里a[1] …… a[n] 的初始值均为0 !!
下面请看代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std ;
const int MAXN = 1e5 + 5 ;
int C[MAXN] ;
int n ;
int lowbit (int x)
{return x & -x ;
}
void add(int x , int d)
{while(x <= n){C[x] += d ;x += lowbit(x) ;}
}
int sum(int x)
{int sumt = 0 ;while (x > 0){sumt += C[x] ;x -= lowbit(x) ;}return sumt ;
}
int main()
{while (scanf("%d" , &n) != EOF){if(n == 0 )break ;memset(C , 0 , sizeof(C)) ;int t = n ;int i ;while ( t-- ){int a , b ;scanf("%d%d", &a , &b) ;add(a , + 1) ;add(b + 1 , -1) ;}for(i = 1 ; i <= n ; i ++){printf("%d" , sum(i)) ;if(i < n)printf(" ") ;}puts("") ;}return 0 ;
}
HDU 1556 Color the ball - from lanshui_Yang相关推荐
- 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
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 1556 Color the ball
题解:基础的树状数组区间修改,单点查询. #include <cstdio> #include <cstring> int c[100005],a,b,n; int modif ...
- HDU - 1556 Color the ball(树状数组)
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...
- HDOJ/HDU 1556 Color the ball(树状数组)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...
- HDU 1556 Color the Ball 线段树 题解
本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...
- hdu 1556 Color the ball 线段树 区间更新
水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...
最新文章
- Linux Shell脚本攻略:shell中各种括号()、(())、[]、[[]]、{}的作用
- 国产化达梦数据库数据迁移文档:oracle11g数据库转达梦8数据库实例演示
- cx oracle 返回码,oracle错误代码处理cx_oracle
- 13.配置 influxDB 鉴权及 HTTP API 写数据的方法
- java.util.ListIterator
- GMF 教程 Mindmap 5
- 4-什么是Image和container
- UVa 1153 Keep the Customer Satisfied 【贪心 优先队列】
- 网站地图sitemap.xml的格式
- 权威解读 GitHub、Apache 疑云:主流开源软件究竟是否会被闭源?| CSDN 独家
- Ehcahe spring
- 计算机ps一级知识点,2017年计算机等级一级Photoshop常考知识点
- Vue项目搭建常用的配置文件,request.js和vue.config.js
- 我的Python心路历程 第十期 (10.12 股票实战可视化之分位数)
- ESP8266 SOC门磁系统(一)---短信报警功能
- Graphics画图自定义画笔
- Wing IDE安装与破解方法
- Xamarin Mono For Android
- 矩阵的主元+秩+矩阵等价
- 华为云计算IE面试笔记-FusionSphere Openstack规划设计过程中要考虑哪些网络平面,各平面主要走哪些业务?