hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8984 Accepted Submission(s): 4594
当N = 0,输入结束。
Update(a,);
Update(b+,-);
注意更新a的值就相当于更新区间[a,n]的值(n为限界/因为a要更新往后的所有区间的值),那么更新b+1的值就是更新[b+1,n](更新b往后所有区间的值)。
所以将区间[a,n]值+1,再将无关区间[b+1,n]的值-1,最后就是将区间[a,b]的值+1。
下面是第二类树状数组的实现的两种方式:
代码一(向上更新,向下求和):
#include <iostream>
using namespace std; int n; //限界
int c[]; int lowbit(int x)
{
return x&-x;
} void Update(int x,int v) //更新区间[x,n]的值
{
while(x<=n){
c[x]+=v;
x+=lowbit(x);
}
} int Sum(int x) //求x之前的和
{
int sum = ;
while(x>){
sum+=c[x];
x-=lowbit(x);
}
return sum;
} int main()
{
int a,b,i;
while(scanf("%d",&n)!=EOF){
memset(c,,sizeof(c));
if(n==) break;
for(i=;i<n;i++){
scanf("%d%d",&a,&b);
Update(a,); //将区间[a,n]的值+1,再将无关区间[b+1,n]的值-1,即将区间[a,b]的值+1
Update(b+,-);
}
for(i=;i<=n;i++){
printf("%d",Sum(i)); //求单个的点直接求sum
if(i<n)
printf(" ");
}
printf("\n");
}
return ;
}
代码二(向下更新,向上求和):
#include <iostream>
using namespace std; int n; //限界
int c[]; int lowbit(int x)
{
return x&-x;
} void Update(int x,int v) //更新区间[x,n]的值
{
while(x>){
c[x]+=v;
x-=lowbit(x); //改动,+变成了-,向下更新
}
} int Sum(int x) //求x之前的和
{
int sum = ;
while(x<=n){
sum+=c[x];
x+=lowbit(x); //改动,-变成了+,向上求和
}
return sum;
} int main()
{
int a,b,i;
while(scanf("%d",&n)!=EOF){
memset(c,,sizeof(c));
if(n==) break;
for(i=;i<n;i++){
scanf("%d%d",&a,&b);
Update(a-,-); //将区间[1,a-1]的值-1,再将无关区间[1,b]的值+1,即将区间[a,b]的值+1
Update(b,);
}
for(i=;i<=n;i++){
printf("%d",Sum(i)); //求单个的点直接求sum
if(i<n)
printf(" ");
}
printf("\n");
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)的更多相关文章
- HDU 1556 Color the ball (一维树状数组,区间更新,单点查询)
中文题,题意就不说了 一开始接触树状数组时,只知道“单点更新,区间求和”的功能,没想到还有“区间更新,单点查询”的作用. 树状数组有两种用途(以一维树状数组举例): 1.单点更新,区间查询(即求和) ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- HDU 1556 Color the ball(线段树:区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...
- HDU 1556 Color the ball (树状数组区间更新)
水题,练习一下树状数组实现区间更新. 对于每个区间,区间左端点+1,右端点的后一位-1,查询每个位置的覆盖次数 #include <cstdio> #include <cstring ...
- HDU 1556 线段树/树状数组/区间更新姿势 三种方法处理
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Sub ...
随机推荐
- Python自动化之rabbitmq rpc client端代码分析(原创)
RPC调用client端解析 import pika import uuid # 建立连接 class FibonacciRpcClient(object): def __init__(self): ...
- Eclipse中项目红叉但找不到错误解决方法
首先windows-show view-problems 根据地址查找错误 若提示: Description Resource Path Location TypeJava c ...
- 2.1---删除链表中重复元素(CC150)
分成两种,1种开了额外空间,临时缓冲区,一种没有开 import java.util.HashSet; import java.util.Set; class ListNode{ int data; ...
- 转:安装MySQL遇到MySQL Server Instance Configuration Wizard未响应的解决办法
问题:安装了MySQL之后进入配置界面的时候,总会显示“MySQL Server Instance Configuration Wizard未响应”,一直卡死. 解决办法:Win7系统中,以管理员的权 ...
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...
- aspx、ashx、asmx文件处理请求效率比较
人生总是面临着许多抉择许多困惑!作为一名“攻城师”或“程序猿”的我们,工作的时候更是如此.你曾经是否苦恼过在系统中使用哪种文件编写客户端请求最合适或最高效呢?aspx.ashx.asmx到底该如何选择 ...
- 《Head First Servlet JSP》容器工作原理(如tomcat)
- Sort Transformed Array
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- ODATA 云驱动 http://www.cdata.com/cloud/
ODATA 云驱动 http://www.cdata.com/cloud/ 目前支持:ORACLE.MS SQL . MYSQL. -------------- rssbus ht ...
- hdu1520
基本的树形dp #include <cstring> #include <cstdio> #include <vector> using namespace std ...