http://poj.org/problem?id=3468 (题目链接)

题意

  给出一个序列,要求维护区间修改与区间求和操作。

Solution

  多年以前学习的树状数组区间修改又忘记了→_→。

  其实就是用树状数组维护一个差分序列${delta[i]}$,${delta[x]}$记录${[i,n]}$中每一个数的增量,每次修改${[l,r]}$就转化为了${delta[l]+=d,delta[r+1]-=d}$。

  对于求和操作${[l,r]}$,其实就是${sum(x)-sum(y)}$,我们这里只讨论${sum(x)}$的求法。

    $${sum(x)=s[x]+delta[1]*x+delta[2]*(x-1)+delta[3]*(x-2)+······+delta[x]}$$

  其中${s[x]}$表示原数组的前缀和。

    $${sum(x)=s[x]+\sum_{i=1}^{x}{delta[i]*(x-i+1)}}$$

    $${sum(x)=s[x]+(x+1)*\sum_{i=1}^{x}{delta[i]}-\sum_{i=1}^{x}{i*delta[i]}}$$

  于是我们用两个树状数组维护${delta[i]}$与${delta[i]*i}$即可。

细节

  更新与求和的时候的下标一定不要打错,没注意到,贡献1Wa→_→。

代码

// poj3468
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define MOD 100000000
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
LL c1[maxn],c2[maxn],s[maxn];
int n,m; int lowbit(int x) {return x&-x;}
LL query(int x) {
LL res=0;
for (int i=x;i;i-=lowbit(i)) res+=(x+1)*c1[i]-c2[i]; //important
return res;
}
void add(int x,LL val) {
for (int i=x;i<=n;i+=lowbit(i)) c1[i]+=val,c2[i]+=(LL)val*x;
}
int main() {
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%lld",&s[i]),s[i]+=s[i-1];
char ch[10];
for (int x,y,i=1;i<=m;i++) {
scanf("%s%d%d",ch,&x,&y);
if (ch[0]=='Q') printf("%lld\n",s[y]-s[x-1]+query(y)-query(x-1));
else {
LL z;
scanf("%lld",&z);
add(x,z);add(y+1,-z);
}
}
return 0;
}

【poj3468】 A Simple Problem with Integers的更多相关文章

  1. 【poj3468】A Simple Problem with Integers

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97008   Accepted: 30285 Case Time Limi ...

  2. 【线段树成段更新成段查询模板】【POJ3468】A Simple Problem with Integerst

    题目大意: 2个操作 A.区间a b 增加 c B 查询a b; 注意事项:1.记住要清除标记 2.查询时要下放标记,但没必要向上更新 线段:自带的,不用建模 区间和性质:sum: /* WA 1次 ...

  3. 一本通1548【例 2】A Simple Problem with Integers

    1548:[例 2]A Simple Problem with Integers 题目描述 这是一道模板题. 给定数列 a[1],a[2],…,a[n],你需要依次进行 q 个操作,操作有两类: 1 ...

  4. POJ3468:A Simple Problem with Integers(线段树模板)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 149972 ...

  5. 【POJ3468】【zkw线段树】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  6. 【POJ3468】【树状数组区间修改】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. 【POJ2761】【fhq treap】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  8. 【成端更新线段树模板】POJ3468-A Simple Problem with Integers

    http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...

  9. 【POJ 3468】 A Simple Problem with Integers

    [题目链接] 点击打开链接 [算法] 本题用线段树很容易写,但是,笔者为了练习树状数组,就用树状数组的方法做了一遍 我们不妨引入差分数组c, 则sum(n) = c[1] + (c[1] + c[2] ...

随机推荐

  1. 错题724-java

    1.代码片段: byte b1=1,b2=2,b3,b6; final byte b4=4,b5=6; b6=b4+b5; b3=(b1+b2); System.out.println(b3+b6); ...

  2. Log4net在类库中的用法

    app.config应当放置在解决方案的根目录下.具体流程如下: 第一步:应该下载log4net.dll并引入到你的项目中,下载见附件 第二步:需要配置相关的配置文件App.config或Web.co ...

  3. indows 8上强制Visual Studio以管理员身份运行

    http://diaosbook.com/Post/2013/2/28/force-visual-studio-always-run-as-admin-on-windows-8 Windows 8的一 ...

  4. scala 学习笔记(05) OOP(中)灵活的trait

    trait -- 不仅仅只是接口! 接上回继续,scala是一个非常有想法的语言,从接口的设计上就可以发现它的与众不同.scala中与java的接口最接近的概念是trait,见下面的代码: packa ...

  5. Clock Skew , Clock Uncertainty和 Period

    本文将介绍FPGA中和时钟有关的相关概念,阅读本文前需要对时序收敛的基本概念和建立.保持关系有一定了解,这些内容可以在时序收敛:基本概念,建立时间和保持时间(setup time 和 hold tim ...

  6. C# 7.0 新特性1: 基于Tuple的“多”返回值方法

    本文基于Roslyn项目中的Issue:#347 展开讨论. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: ...

  7. 个人觉得目前 最好用的Taobao API的NodeJS封装

    话说,Top API SDK默认只给了四种语言的SDK,没有我大NodeJS,这可怎么行,于是封装了一个. 参考地址 GitHub: https://github.com/xiaoppp/TopAPI ...

  8. [BZOJ3875][AHOI2014]骑士游戏(松弛操作)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3875 分析: 类似于spfa求最短路,设d[i]表示完全消灭i号怪物的最小花费,我们对 ...

  9. 14-find 查找文件

    find - search for files in a directory hierarchy 查找文件 [语法]: find [选项] [参数] [功能介绍] find命令用来在指定目录下查找文件 ...

  10. webpack 教程资源收集

    学习的过程中收藏了这些优秀教程和的项目,希望对你有帮助. github地址, 有不错的就更新 官方文档 中文指南  初级教程 webpack-howto 作者:Pete Hunt Webpack 入门 ...