【POJ 3468】 A Simple Problem with Integers
【题目链接】
【算法】
本题用线段树很容易写,但是,笔者为了练习树状数组,就用树状数组的方法做了一遍
我们不妨引入差分数组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的更多相关文章
- 一本通1548【例 2】A Simple Problem with Integers
1548:[例 2]A Simple Problem with Integers 题目描述 这是一道模板题. 给定数列 a[1],a[2],…,a[n],你需要依次进行 q 个操作,操作有两类: 1 ...
- POJ 3468:A Simple Problem with Integers(线段树区间更新模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 141093 ...
- 【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 ...
- HDU 3468:A Simple Problem with Integers(线段树+延迟标记)
A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...
- 【成端更新线段树模板】POJ3468-A Simple Problem with Integers
http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...
- 【POJ 2891】 Strange Way to Express Integers
[题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset ...
- 【poj3468】 A Simple Problem with Integers
http://poj.org/problem?id=3468 (题目链接) 题意 给出一个序列,要求维护区间修改与区间求和操作. Solution 多年以前学习的树状数组区间修改又忘记了→_→. 其实 ...
- 【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 ...
- 【poj3468】A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97008 Accepted: 30285 Case Time Limi ...
随机推荐
- 【mac】屏幕截图快捷键
这里只说四种 1.command+shift+4 截图是区域截图,会自动保存在桌面上 2.command+shift+control+4 区域截图,截图会自动保存在剪切板中,然后你可以通过comman ...
- 超级强大的淘宝开源平台(taobao-code)
今天发现了一个免费又高级的开源SVN服务器,taobao,阿里云CODE.迫不及待的注册了一个.感觉不错,分享给大家. 先说说我们用过的几个SVN服务器吧: google code oksvn(感觉不 ...
- php 解决MySQL插入数据出现 Incorrect string value: '\xF0\x9F\x92\x8BTi...'错误
在项目中向MySQL插入数据时.发现数据插入不完整,通过调试,发现插入语句也没什么特殊的错误. 可是就是差不进去,于是就打开mysqli错误的调试 $ret = mysqli_query($this- ...
- "听话"的品格的症状
反思了一下,也许是因为以前比较听话,听大人的话,听老师的话,听长辈的话.听电视剧的话..........哈哈 现在发现,世界是靠自己去认识,去体会的,别人的经验都只能作为参考,绝对不能不加思考的照搬硬 ...
- 使用网络监视器(IRSI)捕捉和分析协议数据包
转载请注明原地址. 实验名称: 理解子网掩码.网关和ARP协议的作用 一.实验目的和要求 (1) 熟悉IRIS的使用 (2) 验证各种协议数据包格式 (3) 学会捕捉并分析各 ...
- redis中关于过期键的删除策略
我们已经了解到了Redis是一种内存数据库,Redis中数据都是以key-value的形式存储在内存中.由Redisserver来维护和管理这部分内存,内存是何足珍贵,不须要的数据或者是已经使用过的无 ...
- Python - scrapy安装中libxml2问题
先到 http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 下载下面三个库的 whl,然后用pip install 来安装即可 pycurl,lxml,lib ...
- ssh命令、ping命令、traceroute 命令所使用的协议
在Node reboot or eviction: How to check if yourprivate interconnect CRS can transmit network heartbea ...
- openwrt mt7620 内存大小检测
单独编译内核: make V=s target/linux/install 相调函数调用流程: init/main.c : start_kernel() -> setup_arch(&c ...
- delphi中的HOOK [转贴]
按事件分类,有如下的几种常用类型的钩子: 1)键盘钩子可以监视各种键盘消息. 2)鼠标钩子可以监视各种鼠标消息. 3)外壳钩子可以监视各种Shell事件消息. 4)日志钩子可以记录从系统消息队列中取出 ...