【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 ...
随机推荐
- 利用BURPSUITE检测CSRF漏洞
CSRF漏洞的手动判定:修改referer头或直接删除referer头,看在提交表单时,网站是否还是正常响应. 下面演示用Burpsuite对CSRF进行鉴定. 抓包. 成功修改密码完成漏洞的利用.
- Ubuntu 16.04安装CrossOver容器来安装QQ(终极解决办法,亲测有效)
说明:此版本的QQ基本完美,但是有个缺点就是历史记录有些会显示乱码! 注意:此方法能完美解决这篇文章http://www.cnblogs.com/EasonJim/p/7118693.html的所有问 ...
- Spring的IoC容器概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/ioc-containers.html: IoC容器 Spring容器是Spring框架的核心.容器 ...
- phpQuery用法总结
项目下载地址:http://code.google.com/p/phpquery/ 获取内容的方法: 第一种:newDocumentFile phpQuery::newDocumentFile($ur ...
- idea tomcat 配置
昨天我们讲了如何新建多模块项目:idea创建maven多模块项目 本节课,我们讲如何配置tomcat,使昨天配置的web项目,JRapid.Admin可以运行起来.具体步骤如下 第一步 第二步 第三步 ...
- Android Studio一些常用的快捷键
光标移动和窗口切换:1.esc:光标从功能窗口回到编辑窗口 2.alt+num:打开指定的功能窗口,重复操作关闭该窗口. 3.alt+←→:切换编辑的文件. 4.ctrl+home/end:跳转到文件 ...
- Shannon-Fano-Elias编码的C语言实现
Shannon-Fano-Elias编码 一.理论分析 Shannon-Fano-Elias编码是利用累积分布函数来分配码字. 不失一般性,假定取X={1,2,-m}.如果对于全部的x,有p(x)&g ...
- 【转载】5种网络IO模型
同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问题其实不同的人给出 ...
- 为什么说JAVA中要慎重使用继承 C# 语言历史版本特性(C# 1.0到C# 8.0汇总) SQL Server事务 事务日志 SQL Server 锁详解 软件架构之 23种设计模式 Oracle与Sqlserver:Order by NULL值介绍 asp.net MVC漏油配置总结
为什么说JAVA中要慎重使用继承 这篇文章的主题并非鼓励不使用继承,而是仅从使用继承带来的问题出发,讨论继承机制不太好的地方,从而在使用时慎重选择,避开可能遇到的坑. JAVA中使用到继承就会有两 ...
- linux 命令之 watch
watch能够帮你监測一个命令的执行结果,省得你一遍遍的手动执行.在Linux下.watch是周期性的执行下个程序.并全屏显示执行结果.你能够拿他来监測你想要的一切命令的结果变化,比方 tail 一个 ...