这个题刚开始的时候是套模板的,并没有真的理解什么树状数组的区间更新,这几天想了一下,下面是总结:

区间更新这里引进了一个数组delta数组,delta[i]表示区间 [i, n] 的共同增量,每次你需要更新的时候只需要更新delta数组就行了,因为每段区间更新的数都记录在这个数组中,那怎么查询前缀和呐?

sum[i]=a[1]+a[2]+a[3]+......+a[i]+delta[1]*(i-0)+delta[2]*(i-1)+delta[3]*(i-2)+......+delta[i]*(i-i+1);

= sigma( a[x] ) + sigma( delta[x]  *  (i + 1 - x) )

= sigma( a[x] ) + (i + 1) * sigma( delta[x] ) - sigma( delta[x] * x )

红字不难理解就是从当前位置到i区间共同的增量乘上当前位置到i有多少个数就是增加的总量;具体写到代码中怎么体现?还是以这个题为例:

#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 100010
#define ll long long
using namespace std;
ll c1[N];//delta的前缀和   
ll c2[N];//delta * i的前缀和   
ll ans[N];//存放的前缀和
ll n,m;
string op;
ll lowbit(ll x)
{
return x&(-x);
}
void update(ll x,ll val,ll *c)
{
while(x<=n)
{
c[x]+=val;
x+=lowbit(x);
}
}
ll getsum(ll x,ll *c)
{
ll s=;
while(x>)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
int main()
{
freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
while(scanf("%lld%lld",&n,&m)!=EOF)
{
memset(c1,,sizeof c1);
memset(c2,,sizeof c2);
memset(ans,,sizeof ans);
for(int i=;i<=n;i++)
{
scanf("%lld",&ans[i]);
ans[i]+=ans[i-];
}
getchar();
for(int i=;i<=m;i++)
{
cin>>op;
if(op=="C")
{
ll s1,s2,s3;
scanf("%lld%lld%lld",&s1,&s2,&s3);
update(s1,s3,c1);//delta的前缀和  更新
update(s2+,-s3,c1);
update(s1,s1*s3,c2);//delta * i的前缀和  更新
update(s2+,-(s2+)*s3,c2);
}
else if(op=="Q")
{
/*
sigma( a[x] ) + (i + 1) * sigma( delta[x] ) - sigma( delta[x] * x )  
*/ ll s1,s2;
scanf("%lld%lld",&s1,&s2);
/*sigma( a[x] )*/
ll cur=ans[s2]-ans[s1-];//首先等于s1~s2这个区间的基础值
/*(i + 1) * sigma( delta[x] )*/
cur+=getsum(s2,c1)*(s2+)-getsum(s2,c2);//0~s2对前缀和的影响
/*sigma( delta[x] * x )*/  
cur-=getsum(s1-,c1)*(s1)-getsum(s1-,c2);//0~s1对前缀和的影响
printf("%lld\n",cur);
}
}
}
}

POJ 3468 A Simple Problem with Integers(树状数组区间更新) 续的更多相关文章

  1. POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问

    今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...

  2. HDU 4267 A Simple Problem with Integers --树状数组

    题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val  操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...

  3. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  4. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  5. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  6. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  7. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  8. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  9. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  10. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

随机推荐

  1. cglib代理

    简介: github地址:https://github.com/cglib/cglib,可以访问这个地址查看cglib源码和相关文档. 简单的摘录了wiki上关于cglib的描述: cglib is ...

  2. Maximum 贪心

    Maximum Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Des ...

  3. MySQL之常用函数

    MySQL有如下常用函数需要掌握: 1.数学类函数 函数名称 作用 ABS(x)   返回x的绝对值                      SQRT(x)   返回x的非负二次方根 MOD(x,Y ...

  4. mysql error 1130 hy000:Host 'localhost' is not allowed to connect to this mysql server 解决方案

    ERROR 1130 (HY000): Host 'localhost' is not allowed to connect to this MySQL server D:\Wamp\mysql-\b ...

  5. 在项目中创建单元测试时junit的配置和使用

    首先配置项目中AndroidMainfest.xml文件,加入 <instrumentation android:name="android.test.InstrumentationT ...

  6. python的urlparse

    urlparse主要是URL的分解和拼接,分析出URL中的各项参数,可以被其他的URL使用. 主要的函数有: 1.urlparse 将URL分解为6个片段,返回一个元组,包括协议.基地址.相对地址等等 ...

  7. Feature Scaling深入理解

    Feature Scaling 可以翻译为特征归一化,或者数据归一化,比如统计学习中,我们一般都会对不同量纲的特征做归一化,深度学习中经常会谈到增加的BN层,LRN层会带来训练收敛速度的提升,等等.问 ...

  8. 图片载入状态判断及实现百分比效果loading

    前言 一些大的外部资源会导致页面加载速度慢,这时候一般会加上loading效果:这里实现的是根据图片加载进度的百分比loading效果 如何判断图片加载的状态 1.onload  onerror 推荐 ...

  9. elasticsearch的映射(mapping)和分析(analysis)

    转发自:http://blog.csdn.net/hzrandd/article/details/47128895 分析和分析器 分析(analysis)是这样一个过程: 首先,表征化一个文本块为适用 ...

  10. plsql中文乱码问题方案解决

    1.查看服务器端编码 a.select   userenv('language')  from  dual; b.我实际查到的结果为:AMERICAN_AMERICA.ZHS16GBK 2.执行语句 ...