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. Meet Python: little notes 2

    From this blog I will turn to Markdown for original writing. Source: http://www.liaoxuefeng.com/ ♥ l ...

  2. 【转】mysql触发器的实战(触发器执行失败,sql会回滚吗)

    1   引言Mysql的触发器和存储过程一样,都是嵌入到mysql的一段程序.触发器是mysql5新增的功能,目前线上凤巢系统.北斗系统以及哥伦布系统使用的数据库均是mysql5.0.45版本,很多程 ...

  3. 华为acl(traffic-filter)和dhcp管理

    华为alc配置实例:-traffic-filter # 在VLAN100上配置基于ACL的报文过滤,允许源IP地址为192.168.0.2/32的报文通过,丢弃其他报文. <HUAWEI> ...

  4. shell 脚本替换文件中某个字符串

    1.将当前目录下包含jack串的文件中,jack字符串替换为tom sed -i "s/jack/tom/g" `grep "jack" -rl ./` 2.将 ...

  5. Qt Creator 常用快捷键

    多行注释模式                                                                                            Ct ...

  6. 搭建emacs的go编程语言环境

    关于emacs就不说明了,直接切入主题.关于我的emacs配置,可以直接参考GitHub上的lienhua34/myemacs-conf. go-mode 安装 关于go-mode的安装,可以直接参考 ...

  7. js基础知识温习:构造函数与原型

    构造函数 构造函数主要用于初始化新对象.按照惯例,构造函数名第一个字母都要大写. 构造函数有别于其它函数在于它使用new操作符来调用生成一个实例对象.换句话说,如果一个函数使用new操作符来调用,则将 ...

  8. 安全模式下运行Windows installer并卸载程序

    [安全模式] 打开命令行 执行 REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Minimal\MSIServer" ...

  9. Linux运维教程

    最近看马哥Linux运维,收益颇多.愿马哥,身体健康! 2013马哥全套 http://pan.baidu.com/s/1c0JQu9i 运维技术文档 http://pan.baidu.com/s/1 ...

  10. 【JavaEE企业应用实战学习记录】struts国际化

    <%-- Created by IntelliJ IDEA. User: Administrator Date: 2016/10/6 Time: 16:26 To change this tem ...