Luogu 4868 Preprefix sum
类似于树状数组维护区间的方法。
每一次询问要求$\sum_{i = 1}^{n}\sum_{j = 1}^{i}a_j$。
展开一下:
$\sum_{i = 1}^{n}\sum_{j = 1}^{i}a_j = \sum_{i = 1}^{n}a_i * (n - i + 1) = (n + 1)\sum_{i = 1}^{n}a_i - \sum_{i = 1}^{n}a_i * i$
用两个树状数组分别维护一下$\sum_{i = 1}^{n}a_i$和$\sum_{i = 1}^{n}a_i * i$就可以了。
时间复杂度$O((n + q)logn)$。
Code:
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 1e5 + ; int n, qn;
ll a[N]; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} namespace Bit {
ll sum1[N], sum2[N]; #define lowbit(p) (p & (-p)) inline void modify(int p, ll v, ll *arr) {
for(; p <= n; p += lowbit(p))
arr[p] += v;
} inline ll query(int p, ll *arr) {
ll res = 0LL;
for(; p > ; p -= lowbit(p))
res += arr[p];
return res;
} } using namespace Bit; int main() {
read(n), read(qn);
for(int i = ; i <= n; i++) {
read(a[i]);
modify(i, a[i], sum1);
modify(i, 1LL * a[i] * i, sum2);
} for(char op[]; qn--; ) {
scanf("%s", op);
if(op[] == 'Q') {
int x; read(x);
printf("%lld\n", 1LL * (x + ) * query(x, sum1) - query(x, sum2));
} else {
int x; ll v;
read(x), read(v);
modify(x, -a[x], sum1), modify(x, -1LL * a[x] * x, sum2);
modify(x, v, sum1), modify(x, v * x, sum2);
a[x] = v;
}
} return ;
}
Luogu 4868 Preprefix sum的更多相关文章
- 2021.08.09 P4868 Preprefix sum(树状数组)
2021.08.09 P4868 Preprefix sum(树状数组) P4868 Preprefix sum - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 前缀和(pr ...
- [bzoj3155]Preprefix sum(树状数组)
3155: Preprefix sum Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 1183 Solved: 546[Submit][Status] ...
- BZOJ 3155: Preprefix sum( 线段树 )
刷刷水题... 前缀和的前缀和...显然树状数组可以写...然而我不会, 只能写线段树了 把改变成加, 然后线段树维护前缀和, 某点p加, 会影响前缀和pre(x)(p≤x≤n), 对[p, n]这段 ...
- Preprefix sum BZOJ 3155 树状数组
题目描述 前缀和(prefix sum)Si=∑k=1iaiS_i=\sum_{k=1}^i a_iSi=∑k=1iai. 前前缀和(preprefix sum) 则把SiS_iSi作为原序列 ...
- 3155: Preprefix sum
3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这 ...
- 差分+树状数组【p4868】Preprefix sum
Description 前缀和(prefix sum)\(S_i=\sum_{k=1}^i a_i\). 前前缀和(preprefix sum) 则把\(S_i\)作为原序列再进行前缀和.记再次求得前 ...
- 树状数组【bzoj3155】: Preprefix sum
3155: Preprefix sum 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3155 把给出的a_i当成查分数组d_i做就可以了 ...
- BZOJ3155: Preprefix sum
题解: 写过树状数组搞区间修改和区间求和的就可以秒出吧... 代码: #include<cstdio> #include<cstdlib> #include<cmath& ...
- BZOJ 3155: Preprefix sum
大意:给一个数组,先求出SUM[I],然后动态的求出1-I的SUM[I]的和, 这题得化公式: 树状数组维护两个和:SUM(A[I])(1<=I<=X); SUM(A[I]*(N-I+1) ...
随机推荐
- debian下使用ft232为stm32f429i-discovery烧写uboot和uImage
操作系统:debian 软件: openocd minicom 硬件: MiniUSB线.stm32f429i-discovery, WaveShare FT232串口模块(可以在淘宝上买到) 关 ...
- 常用CSS设置
主要内容: 一.容器类 二.文本类 三.特效类 一.容器类 1.background-image:url('img/02.gif'); 设置背景图(可以是动态图) 2.background-col ...
- 剑指offer之 调整奇数偶数数组位置
package Problem14; /* * 问题描述: * 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位与数组的前半部分,所有偶数位与数组的 * 后半部分 */ publ ...
- 算法(Algorithms)第4版 练习 2.2.9
package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdOut; publi ...
- 关于ansible变量的一个问题
ansible-playbook 使用with_items 时 items中 如果有变量 {} 外面可以用 “” items中 如果都是固定值,没有用到变量,{}最外面不要加 “” ,不然报错,mmp
- SQL truncate 、delete与drop区别及 MSSQL、MySQL 数据库删除大批量千万级百万级数据的优化
C#_Stopwatch 类 http://www.cnblogs.com/zhw511006/archive/2009/07/22/1528405.html http://blog.csdn.net ...
- Java -- 封装访问控制级别,包, instanceof 运算符, 初始化块
1. 可以用 package name1.name2; 显式的定义包名, *.class文件位置应该对应包 name1 name2 的目录下. 2. instanceof 运算符 Object obj ...
- Codeforces 358D Dima and Hares:dp【只考虑相邻元素】
题目链接:http://codeforces.com/problemset/problem/358/D 题意: 有n个物品A[i]摆成一排,你要按照某一个顺序将它们全部取走. 其中,取走A[i]的收益 ...
- 数据库+maven
1.mysql <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-ja ...
- 分享知识-快乐自己:Caused by: org.hibernate.tool.schema.extract.spi.SchemaExtractionException: More than one table found in namespace (, ) : Dept (XXX)
在命名空间(,)中找到多个表 - SchemaExtractionException? 问题: 尝试在Java应用程序中使用Hibernate将一些值保存到表中时,我一直面临着这个奇怪的异常. 但是, ...