poj 3468: A Simple Problem with Integers (树状数组区间更新)
题目链接: http://poj.org/problem?id=3468
题目是对一个数组,支持两种操作
操作C:对下标从a到b的每个元素,值增加c;
操作Q:对求下标从a到b的元素值之和。
这道题也可以用线段树解,本文不做描述,下面分析如何用树状数组来解决这道题。
/*先把问题简化一点,因为 结果=初值+增量,所以,我们可以只对增量进行分析。然后,这种题有一个特点,就是如果对一般的一个操作C与操作查询前缀和的组合符合条件,那么无论进行多少次任意操作结果都是正确的。故 假设,先进行一次参数分别为 l,r,c 的操作C,再进行一次查询前缀和Si的操作(i 与l r的大小关系不定)。操作C之后,对Si,①当i<l时,Si=0,②当l<=i<r时,Si=c*(i-l+1),③当i>=r时,Si=c*(r-l+1)。要使情况①③满足比较简单,只需使add操作不在l左边进行,且对一树状数组的l和r分别进行+x+c*(r-l+1),-x的操作;而分析如何满足情况②,可以把Si看作是分布在直线y=c(x-l)=cx-cl上的一系列散点,易看出实现+cx的方法,就是在l执行add c的操作,在r执行add -c的操作,查询时查询sum()*x,而实现-cl的方法可以与上面“分别进行+x+c*(r-l+1),-x的操作”(引号中的x是不确定的)联系起来得出。故而任意Si都可以得出。*/
#include <cstdio> typedef long long LL; const int maxn =1e5+;
LL a[][maxn];
LL psum[maxn];
int n; inline int lowbit(int x)
{
return x&-x;
}
void add(LL a[],int x,int d)
{
while(x<=n)
{
a[x]+=d;
x+=lowbit(x);
}
}
LL sum(LL a[],int x)
{
LL ret=;
while(x)
{
ret+=a[x];
x-=lowbit(x);
}
return ret;
} LL query(int x)
{
return sum(a[],x)*x+sum(a[],x);
} int main()
{
int q;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%I64d",&psum[i]);
psum[i]+=psum[i-];
}
char op[];
while(q--)
{
int l,r;
scanf("%s%d%d",op,&l,&r);
if(op[]=='Q')
printf("%I64d\n",query(r)-query(l-)+psum[r]-psum[l-]);
else
{
int c;
scanf("%d",&c);
add(a[],l,c);
add(a[],r,-c);
add(a[],l,c*(-l+));
add(a[],r,c*r);
}
}
}
//上面内容废弃,以下解析为2018.05.30更新
假设数组用a[]表示,定义辅助数组s[]、d[],其具体含义为

且s[]、d[]间有如下关系

原题中对a[]的区间修改,可以视为对d[]的单点修改,而s[]又可以由d[i]、i*d[i]的前缀和推导出来。且维护s1[]、s2[]较容易,因为每次操作都是对d[]进行单点修改。具体可以参考以下代码
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
typedef long long LL; const int N=1e5+; LL s1[N],s2[N];
int n,q; inline int lowbit(int x)
{
return x&-x;
}
void add(LL a[],int i,LL x)
{
while(i<=n)
{
a[i]+=x;
i+=lowbit(i);
}
}
LL sum(LL a[],int i)
{
LL ret=;
while(i)
{
ret+=a[i];
i-=lowbit(i);
}
return ret;
}
void Add(int i,LL x)
{
add(s1,i,x*i),add(s2,i,x);
}
LL Sum(int i)
{
return -sum(s1,i)+(i+)*sum(s2,i);
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
LL t;
scanf("%lld",&t);
Add(i,t),Add(i+,-t);
}
while(q--)
{
int l,r;
char op[];
scanf("%s%d%d",op,&l,&r);
if(op[]=='Q')
printf("%lld\n",Sum(r)-Sum(l-));
else
{
LL t;
scanf("%lld",&t);
Add(l,t),Add(r+,-t);
}
}
}
poj 3468: A Simple Problem with Integers (树状数组区间更新)的更多相关文章
- POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...
- HDU 4267 A Simple Problem with Integers --树状数组
题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val 操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 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 ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
随机推荐
- springboot启动报错start bean 'eurekaAutoServiceRegistration' NullPointerException
解决方案参考:https://blog.csdn.net/hhj13978064496/article/details/82825365 我将eureka的依赖包放到了依赖包的最下面,启动报错, 如下 ...
- (\w+)\s*, \s*(\w+)
\s表示空格 \w表示任何字符,字母数字下划线 _就表示下划线
- C++ 测量程序执行时间的办法
#include <time.h> clock_t start = clock(); //时间起始 /*待测试代码*/ clock_t end = clock(); //时间测试结束 co ...
- apicloud地图、即时通讯、人脸识别登录、以及平时踩过得坑
apicloud技术浅谈 导语 apicloud 的学习也有一段时间了,这是我个人的一些经验,和踩过的坑,希望对大家能有一些帮助. apicloud的知识准备 apicloud 是一个用原生的思想搭建 ...
- 第 1 章 前端之html
一.html初始 1.web服务本质 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM ...
- SEC2- - mysql 的介绍
一.mysql的背景 前身属于瑞典的一家公司,mysql AB 08年被sun公司收购 09年被oracle公司收购 二.mysql的优点 1. 开源免费成本低 2. 性能高,移植性好 3. 体积 ...
- javaweb的Filter过滤器设置全站编码
FIlter配置全站编码有一种方法是重写getParameter方法,也就是继承HttpServletRequestWrapper在重写getParameter方法,还有一种就是如下: public ...
- Netty之大名鼎鼎的EventLoop
EventLoopGroup 与Reactor: 前面的章节中我们已经知道了,一个Netty 程序启动时,至少要指定一个EventLoopGroup(如果使用到的是NIO,通常是指NioEventLo ...
- eclipse新建maven项目出错 pom.xml报错
问题: 1.新建项目后会提示一个这样的错 maven-compiler-plugin:3.1:compile(1 errors) maven-compiler-plugin:3.1:testCompi ...
- 史上最全最常用的正则表达式(转自微信公众号:javascript)
很多不太懂正则的朋友,在遇到需要用正则校验数据时,往往是在网上去找很久,结果找来的还是不很符合要求.所以我最近把开发中常用的一些正则表达式整理了一下,在这里分享一下.给自己留个底,也给朋友们做个参考. ...