【题目链接】

点击打开链接

【算法】

本题用线段树很容易写,但是,笔者为了练习树状数组,就用树状数组的方法做了一遍

我们不妨引入差分数组c,

则sum(n) = c[1] + (c[1] + c[2]) + (c[1] + c[2] + c[3]) + ... + (c[1] + c[2] + c[3] + ... + c[n])

= n * c[1] + (n - 1) * c[2] + (n - 2) * c[3] + ... + c[n]

= n * (c[1] + c[2] + c[3] + ... + c[n]) - c[2] - c[3] * 2 - c[4] * 3 - ... - c[n] * (n - 1)

所以可以用两个树状数组分别维护c的前缀和和c[i]*(i-1)的前缀和

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 100000 long long N,Q,tr,tl,i,l,r,x;
long long a[MAXN+];
char opt; template <typename T> inline void read(T &x) {
long long f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x) {
if (x < ) { x = -x; putchar('-'); }
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
} struct BinaryIndexedTree {
long long bit[MAXN+];
inline long long lowbit(long long x) { return x & -x; }
inline void clear() {
long long i;
for (i = ; i <= N; i++) bit[i] = ;
}
inline void modify(long long pos,long long val) {
long long i;
for (i = pos; i <= N; i += lowbit(i)) bit[i] += val;
}
inline long long query(long long pos) {
long long i,ret = ;
for (i = pos; i; i -= lowbit(i)) ret += bit[i];
return ret;
}
} c1,c2; int main() { read(N); read(Q);
for (i = ; i <= N; i++) {
read(a[i]);
c1.modify(i,a[i]);
c1.modify(i+,-a[i]);
c2.modify(i,(i-)*a[i]);
c2.modify(i+,-i*a[i]);
}
while (Q--) {
opt = getchar();
if (opt == 'C') {
read(l); read(r); read(x);
c1.modify(l,x);
c1.modify(r+,-x);
c2.modify(l,(l-)*x);
c2.modify(r+,-x*r);
} else {
read(l); read(r);
tl = c1.query(l-) * (l - ) - c2.query(l-);
tr = c1.query(r) * r - c2.query(r);
writeln(tr-tl);
}
} return ;
}
/*
c[1] + (c[1] + c[2]) + (c[1] + c[2] + c[3]) + ... + (c[1] + c[2] + c[3] + c[4] + ... + c[n])
= c[1] * n + c[2] * (n - 1) + c[3] * (n - 2) + ... + c[n]
= (c[1] + c[2] + c[3] + ... + c[n]) * n - c[2] - c[3] * 2 - c[4] * 3 - ... - c[n] * (n - 1)
= sigma(c1,n) * n - sigma(c2,n)
*/

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

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

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

  2. POJ 3468:A Simple Problem with Integers(线段树区间更新模板)

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

  3. 【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 ...

  4. HDU 3468:A Simple Problem with Integers(线段树+延迟标记)

    A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...

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

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

  6. 【POJ 2891】 Strange Way to Express Integers

    [题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset ...

  7. 【poj3468】 A Simple Problem with Integers

    http://poj.org/problem?id=3468 (题目链接) 题意 给出一个序列,要求维护区间修改与区间求和操作. Solution 多年以前学习的树状数组区间修改又忘记了→_→. 其实 ...

  8. 【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 ...

  9. 【poj3468】A Simple Problem with Integers

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

随机推荐

  1. [Poi2010]Bridges 最大流+二分答案 判定混合图欧拉回路

    https://darkbzoj.cf/problem/2095 bzoj 相同的题挂了,这个oj可以写. 题目就是要我们找一条欧拉回路(每个桥经过一次就好,不管方向),使得这条回路上权值最大的尽量小 ...

  2. SQL入门随笔(上机实验报告)

    <数据定义部分> 一.定义模式和删除模式 a.为用户定义一个模式学生—课程模式 S-T CREATE  SCHEMA  "S-T"  AUTHORIZATION USE ...

  3. spring data jpa 查询部分字段

    @Query("select new map(ah as ah,salq as sqlq,yg as yg, bg as bg,ay as ay) FROM Aj where ahdm=?1 ...

  4. 洛谷——P1057 传球游戏

    P1057 传球游戏 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹 ...

  5. [Bzoj5043][Lydsy1709月赛]密码破译(按位dp)

    5043: [Lydsy1709月赛]密码破译 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 477  Solved: 125[Submit][Sta ...

  6. Ubuntu 16.04安装JAD反编译工具(Java)

    JAD反编译工具有个好处,就是字节码和源代码一起输出. 官网:https://varaneckas.com/jad/ 安装步骤: 1.下载: 离线版本:(链接: https://pan.baidu.c ...

  7. 关于Lisp和函数式编程 & 各种语言对比 & TIOBE

    上一篇文章提到了,今天又读到Lisp的一些内容 <为什么Lisp如此先进>ruanyifeng(Link).关于Lisp的八卦,可以参考我前面两篇文章 http://www.cnblogs ...

  8. php 中函数获取可变参数的方法, 这个语法有点像 golang 语言中的

    原文呢:http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict Onl ...

  9. IntelliJ IDEA 默认需要进行maven的设置

    IntelliJ IDEA 默认需要进行maven的设置 需要指定maven的地址,指定settings.xml的地址: 可以默认的在user/.m2/下面放一个settings.xml文件: 学习: ...

  10. SAE云平台的使用

    参考文章:http://www.cnblogs.com/luyangsblog/p/3956135.html                 Web开发从零单排之一:在新浪云平台SAE上开发一个htm ...