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 ...
随机推荐
- wrap ConcurrentDictionary in BlockingCollection
ConcurrentDictionary<int, BlockingCollection<string>> mailBoxes = new ConcurrentDictiona ...
- mysql访问权限GRANT ALL PRIVILEGES ON,访问权限表
开启远程连接:2, 修改 Mysql-Server 用户配置mysql> USE mysql; -- 切换到 mysql DBDatabase changedmysql> SELECT U ...
- bzoj 1565 [NOI2009]植物大战僵尸 解题报告
1565: [NOI2009]植物大战僵尸 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2161 Solved: 1000[Submit][Stat ...
- 【BZOJ】【2694】Lcm
数论/莫比乌斯反演/线性筛 题解:http://www.cnblogs.com/zyfzyf/p/4218176.html JZPTAB的加强版?感觉线性筛好像还是不怎么会啊……sad 题目记下来,回 ...
- ubuntu下mongodb常用命令
1. 启动脚本 #!/bin/bash mongod --dbpath /usr/local/mongodb/data1 chmod +x run-mongodb 2. 关闭数据库服务 官方文档说可以 ...
- java之 22天 GUI 图形界面编程(二)
转自:http://takeme.iteye.com/blog/1876853 模拟window开打文件 import java.awt.Button; import java.awt.Dialog; ...
- assets raw 资源 AssetManager
assets raw 目录简介 assets核心特性:不会被编译成二进制,支持子目录(可以分类,这是相对raw目录最大的好处),通过文件名访问,调用getAssets通过AssetManager访问 ...
- ML—朴素贝叶斯
华电北风吹 日期:2015/12/12 朴素贝叶斯算法和高斯判别分析一样同属于生成模型.但朴素贝叶斯算法须要特征条件独立性如果,即样本各个特征之间相互独立. 一.朴素贝叶斯模型 朴素贝叶斯算法通过训练 ...
- 【js】利用闭包消除回调函数启动时值已经发生变化的影响
在以下代码中,timeFun异步执行了一个匿名函数,当输出color的值时已经由green变成red,因此输出为red. function timedFun(callback){ setTimeout ...
- UVA 10090 Marbles(扩展欧几里得)
Marbles Input: standard input Output: standard output I have some (say, n) marbles (small glass ball ...