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

区间更新这里引进了一个数组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. css预处理语言--让你的css编写更加简单方便

    CSS预处理语言之一-------LESS Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. Less 可以运行在 Nod ...

  2. 开博近一年的感想 by 程序员小白

    /* 好吧,这里的写博客应该理解为更宏观的写文章. */   在去年的这个时候,我所知道的平台只有 CSDN 和博客园..然而 CSDN 的广告实在是不想吐槽了,选择博客园是一件非常自然的事情.要说开 ...

  3. HTML 简述

    1.本页超链接 <a href="#t">查看 Chapter 4.</a> <a name="t">Test</a& ...

  4. Hive导出复杂数据到csv文件

    工作中经常遇到使用Hive导出数据到文本文件供数据分析时使用.Hive导出复杂数据到csv等文本文件时,有时会遇到以下几个问题: 导出的数据只有数据没有列名. 导出的数据比较复杂时,如字符串内包含一些 ...

  5. python常见模块命令(os/sys/platform)

    一.Os Python的标准库中的os模块主要涉及普遍的操作系统功能.可以在Linux和Windows下运行,与平台无关. os.sep 可以取代操作系统特定的路径分割符. os.name字符串指示你 ...

  6. HDU1421搬寝室(简单DP)

    当然,还可以加滚动数组优化. #include<cstdio> #include<cstdlib> #include<iostream> #include<m ...

  7. Oracle虚拟机VirtualBox安装成功后的注意事项

    首先VirtualBox的安装教程 (1)按文档安装 (2)安装完之后配置共享文件夹 (3)安装windowxp镜像 (4)安装Oracle  详情请见Oracle安装文档 (5)启动xp虚拟机 (6 ...

  8. 从头编写 asp.net core 2.0 web api 基础框架 (4) EF配置

    第1部分:http://www.cnblogs.com/cgzl/p/7637250.html 第2部分:http://www.cnblogs.com/cgzl/p/7640077.html 第3部分 ...

  9. SQL Server XML数据解析

    --5.读取XML --下面为多种方法从XML中读取EMAIL DECLARE @x XML SELECT @x = ' <People> <dongsheng> <In ...

  10. BZOJ-4915-简单的数字题

    Description 对任意的四个不同的正整数组成的集合A={a_1,a_2,a_3,a_4 },记S_A=a_1+a_2+a_3+a_4,设n_A是满足a_i+a_j (1 ≤i<j≤4)| ...