POJ 3468 A Simple Problem with Integers 【树状数组】
题目链接: id=3468">http://poj.org/problem?id=3468
题目大意:给出一组数组v[i],有两种操作,一种给出两个数a,b。要求输出v[a]到v[b]之间的和。还有一种给出三个数a,b,c,让v[a]到v[b]之间的数全都加上c。
全然是树状数组可以实现的功能,可是假设就这样单纯的套用模板,做另外一种操作是更新每一个值,这种操作就有可能超时。
换一种思路,既然另外一种操作是给某区间上的全部数加上同样的值,那么应该是可以简化的才对。
如果数组sum[i]为原数组从v[1]到v[i]的和。数组c1[i]为更新之后,v[i]到v[n]的添加量。分析一下结果ans:
a,b之间的和ans=sum[b]-sum[a-1]+c1[1]*x + c1[2]*(x-1) + c1[3]*(x-2)+...+c1[x]*1
ans=sum[b]-sum[a-1]+segema(c1[i]*(x-i+1))
ans=sum[b]-sum[a-1] + (x+1)*segma(c1[i]) - segma(c1[i]*i)
令c2[i]=c1[i]*i;
如此便能利用树状数组解出此题
代码:
#include <stdio.h>
#define N 100001
#define lowbit(i) ( i & (-i) )
int n;
__int64 v[N];
__int64 c1[N];// 每一个C数组代表v[i-lowbit(i)+1]到v[i]之间的和
__int64 c2[N];
__int64 sum[N]; void Updata(__int64 *array,__int64 i,__int64 a)
{
for(;i<=n;i+=lowbit(i))
array[i]+=a;
}
__int64 Sumv(__int64 *array,__int64 i) //求出数组v[1]到v[i]的和
{
__int64 result=0;
while (i>=1)
{
result+=array[i];
i-=lowbit(i);
}
return result;
}
int main()
{
__int64 q,i=0;
__int64 ans=0;
scanf("%I64d%I64d",&n,&q);
for(i=1;i<=n;i++)
scanf("%I64d",&v[i]);
for(__int64 i=1;i<=n;i++)
sum[i]=sum[i-1]+v[i];
while(q--)
{
char ch[2];
scanf("%s",ch);
if(ch[0]=='Q')
{
__int64 s,t;
scanf("%I64d%I64d",&s,&t);
ans=sum[t]-sum[s-1];
ans+=((t+1)*Sumv(c1,t)-Sumv(c2,t));
ans-=(s*Sumv(c1,s-1)-Sumv(c2,s-1));
printf("%I64d\n",ans);
}
else
{
__int64 a,b,c;
scanf("%I64d%I64d%I64d",&a,&b,&c);
Updata(c1,a,c);
Updata(c1,b+1,-c);
Updata(c2,a,c*a);
Updata(c2,b+1,-c*(b+1));
}
}
return 0;
}
POJ 3468 A Simple Problem with Integers 【树状数组】的更多相关文章
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 4267 A Simple Problem with Integers --树状数组
题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val 操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...
- POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
随机推荐
- arcgis python添加几何属性
import arcpy import numpy import math def AddGeometryAttributes(fc, geomProperties, lUnit, aUnit, cs ...
- Nginx和Tomcat负载均衡实现session共享
以前的项目使用Nginx作为反向代理实现了多个Tomcat的负载均衡,为了实现多个Tomcat之间的session共享,使用了开源的Memcached-Session-Manager框架. 此框架的优 ...
- CATransform3D的m34值动画
CATransform3D的m34值动画 效果 源码 https://github.com/YouXianMing/Animations // // CATransform3DM34Controlle ...
- GNU GRUB
Introduction GNU GRUB is a Multiboot boot loader. It was derived from GRUB, the GRand Unified Bootlo ...
- OpenCV学习(40) 人脸识别(4)
在人脸识别模式类中,还实现了一种基于LBP直方图的人脸识别方法.LBP图的原理参照:http://www.cnblogs.com/mikewolf2002/p/3438698.html 在 ...
- 屏保:画线圈LineFlower
LineFlowerSP 小时候玩过一种画线圈的玩具,将一个圆形齿轮在一个大圈里转,会画出各种图形来.这个程序就是模仿它做的.算法原理:将一个圆围绕着另一个大圆公转,并且它还做自转运动.那么圆内一点的 ...
- 4 Sum leetcode java
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- 巧妙利用函数的惰性载入提高javascript 代码性能
在 javascript 代码中,因为各浏览器之间的行为的差异,我们经常会在函数中包含了大量的 if 语句,以检查浏览器特性,解决不同浏览器的兼容问题. 例如,我们最常见的为 dom 节点添加事件的函 ...
- IOS UITableView索引排序功能
UITbableView分组展示信息时,有时在右侧会带索引,右侧的索引一般为分组的首字母,比如城市列表的展示.当点击右侧索引的字母,列表会快速跳到索引对应的分组,方便我们快速查找.下面,就介绍一下索引 ...
- RS错误RSV-VAL-0032之项目未在布局中引用的3种解决办法
如下图所示,我用RS新建了一个空白页面,拖入了一个列表,给该列表新建了一个条件样式 条件样式如下所示,表达式来自查询1 运行,报错如下图所示 原因就是条件样式使用到了查询1中的数据项1但是数据项1在报 ...