【题目链接】

点击打开链接

【算法】

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

我们不妨引入差分数组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. Codeforces 515E Drazil and Park (ST表)

    题目链接 Drazil and Park 中文题面 传送门 如果他选择了x和y,那么他消耗的能量为dx + dx + 1 + ... + dy - 1 + 2 * (hx + hy). 把这个式子写成 ...

  2. 快速掌握RabbitMQ(一)——RabbitMQ的基本概念、安装和C#驱动

    1 RabbitMQ简介 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现,官网地址:http://www.rabbitmq.com.Ra ...

  3. Head first python前六章小结

    看这本Head first python已经有十几天了,到第七章开始讲Web开发.移动应用开发,后半年我主要是想往后端的方向发展,所以这本书暂时告一段落.这篇博客没有太多的注释,主要是内容比较简单,只 ...

  4. nginx原配置

    #原配置 server { listen ; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main ...

  5. 使用 Git + Dropbox + SourceTree 做 Source Code Management

    此篇文章主要針對有安裝 XCode 的 Mac 用戶. Git 版本控管工具,作用類似 CVS.Subversion(簡 稱SVN),好處在於 Git 不像 CVS 及 SVN 是屬於集中式的版本控管 ...

  6. 聊聊WiFi Hacks:为何你的Karma攻击不好使了

    0.前言 三年前我发表了一篇文章<黑客有办法让你自动连上陌生WiFi>,介绍Karma攻击可以让你的无线设备自动连上黑客的WiFi.当时引起了还算比较热烈的讨论,关于WiFi安全,关于Ka ...

  7. 391. Perfect Rectangle

    最后更新 一刷 16-Jan-2017 这个题我甚至不知道该怎么总结. 难就难在从这个题抽象出一种解法,看了别人的答案和思路= =然而没有归类总结到某种类型,这题相当于背了个题... 简单的说,除了最 ...

  8. MRP Force Reservation的作用

    生产单根据BOM计算出相应的物料需求,生产领料单stock.picking ( internal moves) Stock.picking使用工作流自动计算库存量,如果库存量够,则使用 test_as ...

  9. centos下的hadoop集群实现ssh的无密码登陆

    CentOS 下SSH无密码登录的配置 最近学习Hadoop.它要求各节点之间通过SSH无密码登录,配置SSH的时候费了一番功夫,记录下来,以备忘. 配置SSH无密码登录需要3步: 1.生成公钥和私钥 ...

  10. Posix消息队列相关函数

    Posix消息队列(message queue) IPC函数中常用的参数取值: 打开或创建POSIX IPC对象所用的各种oflag常值o_RDONLY   只读O_WRONLY   只写O_RDWD ...