题目链接: 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 (树状数组区间更新)的更多相关文章

  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. 当执行一条查询语句时,MySQL内部经历了什么?

    假如说我们有一张表 T ,表里只有一个字段 ID,当我们执行下边这条SQL语句时: mysql> select * fron T where ID=10; 在我们眼中能看到的只是输入一条 SQL ...

  2. Python分布式爬虫打造搜索引擎完整版-基于Scrapy、Redis、elasticsearch和django打造一个完整的搜索引擎网站

    Python分布式爬虫打造搜索引擎 基于Scrapy.Redis.elasticsearch和django打造一个完整的搜索引擎网站 https://github.com/mtianyan/Artic ...

  3. Chrome开发小技巧--浏览器控制台现写并运行js代码--snippets

    想简单等运行一段js代码,以前可能会新建一个html 里面包含script标签,或者引入一个js,然后chrome浏览器打开.这样很麻烦. 想再console控制台写,也不方便,换行处理麻烦. 基于在 ...

  4. mariadb(一)基础

    一.数据库介绍 1.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来 ...

  5. symfony 初始化项目

    学习Symfony首先看一下已经发布了哪些版本; 现在我记录一下两个版本的使用情况: 3.4 是一个长期维护且稳定的版本 4.3是一个最新版本且速度飞快地版本 官方介绍:https://symfony ...

  6. vue-slot的使用

    父组件在子组件内套的内容,是不显示的:vue有一套内容分发的的API,<slot>作为内容分发的出口,假如父组件需要在子组件内放一些DOM,那么这些DOM是显示.不显示.在哪个地方显示.如 ...

  7. poj1258Agri-Net(最小生成树)

    题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...

  8. Android深度探索-卷1第八章心得体会

    本章介绍了如何将Linux驱动分成多个实现文件和Linux常用的代码重用方式还有些强行卸载Linux驱动的方法 开发一个Linux驱动,可能会在init.exit等函数中发生错误导致Linux驱动安装 ...

  9. Mac006--swithchosts安装

    Mac006--swithchosts安装 使用brew命令安装:brew cask install switchhosts 因为mac上,网络下载无法进行安装,所以应用brew命令安装. switc ...

  10. Pair Testing

    All-Pairs Testing is a test design method to deal with the combinatorics problem of defining test ca ...