luoguP4868 Preprefix sum
https://www.luogu.org/problemnew/show/P4868
线段树上加等差数列,基础区间修改单点查询
等差数列具有可加性,当在同一段区间内时,首项相加公差相加即可
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &f) {
f = 0; T fu = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') fu = -1; c = getchar();}
while(c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + (c & 15); c = getchar();}
f *= fu;
}
typedef long long ll;
const int N = 100000 + 10;
struct Tree {
ll val[N << 2], sx[N << 2], gc[N << 2];
void update(int u) {
val[u] = val[u << 1] + val[u << 1 | 1];
}
void pushdown(int u, int l, int r) {
if(sx[u] || gc[u]) {
int mid = (l + r) >> 1;
ll llen = (mid - l + 1);
ll rlen = (r - mid);
val[u << 1] += (sx[u] + sx[u] + gc[u] * (llen - 1)) * llen / 2ll;
val[u << 1 | 1] += (sx[u] * 2 + gc[u] * llen * 2 + gc[u] * (rlen - 1)) * rlen / 2ll;
sx[u << 1] += sx[u]; sx[u << 1 | 1] += sx[u] + gc[u] * llen;
gc[u << 1] += gc[u]; gc[u << 1 | 1] += gc[u];
sx[u] = gc[u] = 0;
}
}
void change(int u, int l, int r, int L, int R, ll x, ll y) {
if(l <= L && R <= r) {
int len = (R - L + 1); x += (L - l) * y;
val[u] += (x + x + (len - 1) * y) * len / 2ll;
sx[u] += x; gc[u] += y;
return;
}
pushdown(u, L, R);
int mid = (L + R) >> 1;
if(mid >= l) change(u << 1, l, r, L, mid, x, y);
if(mid + 1 <= r) change(u << 1 | 1, l, r, mid + 1, R, x, y);
update(u);
}
ll query(int u, int l, int r, int L, int R) {
if(l <= L && R <= r) return val[u];
pushdown(u, L, R); int mid = (L + R) >> 1;
ll ans = 0;
if(mid >= l) ans += query(u << 1, l, r, L, mid);
if(mid + 1 <= r) ans += query(u << 1 | 1, l, r, mid + 1, R);
return ans;
}
}p;
char c[12];
int w[N];
int n, m;
int main() {
read(n); read(m);
for(int i = 1; i <= n; i++) {
read(w[i]);
p.change(1, i, n, 1, n, w[i], w[i]);
}
for(int i = 1; i <= m; i++) {
scanf("%s", c + 1);
if(c[1] == 'Q') {
int a; read(a);
printf("%lld\n", p.query(1, a, a, 1, n));
} else {
int a, b; read(a); read(b);
p.change(1, a, n, 1, n, -w[a], -w[a]);
w[a] = b;
p.change(1, a, n, 1, n, w[a], w[a]);
}
}
return 0;
}
luoguP4868 Preprefix sum的更多相关文章
- [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做就可以了 ...
- 2021.08.09 P4868 Preprefix sum(树状数组)
2021.08.09 P4868 Preprefix sum(树状数组) P4868 Preprefix sum - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 前缀和(pr ...
- 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) ...
随机推荐
- vs2010一运行就报错deven.exe assert failure 解决方法,卸载系统中.netFramework最新版本的(简体中文)
vs2010一运行就报错deven.exe assert failure 解决方法,卸载系统中.netFramework最新版本的(简体中文)
- apache 不解析 php
apache 不解析php 1.找到: AddType application/x-gzip .gz .tgz在其下面添加: AddType application/x-httpd-php .php ...
- 让你的javascript代码高大上
1,创造简短的写法 你可以这么写: var slice = Array.prototype.slice; slice.call(arguments); //转化成数组 也可以这么写:(ie9+) va ...
- 凸优化 Convex Optimization PDF 扫描文字识别版
凸优化理论 Convex Optimization 清华大学出版社 王书宁许窒黄晓霖译 Stephen Boyd Lieven Vandenbergt原著 2013 年l 月第1 版 下载链接 链接: ...
- MySQL(数据库)
数据库概念: 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库在实际应用中会遇到各式各样的数据库如nosql非关系数据库(memcached,redis,mangodb),RDBM ...
- Scala语言简介和开发环境配置
Scala语言的简介和开发环境搭建 Scala是一门结合了面向对象特征和函数式编程特征的语言,它是一个创新的编程语言产品.Scala可以做脚本(就像shell脚本一样),可以做服务端编程语言,可以写数 ...
- Windows下Git中正确显示中文的设置方法
Windows下Git中正确显示中文的设置方法 具体设置方法如下: 进入目录etc:$ cd /etc 1. 编辑 gitconfig 文件:$ vi gitconfig.在其中增加如下内容: [gu ...
- Java多线程-新特征-原子量
所谓的原子量即操作变量的操作是“原子的”,该操作不可再分,因此是线程安全的. 为何要使用原子变量呢,原因是多个线程对单个变量操作也会引起一些问题.在Java5之前,可以通过volatile.synch ...
- Python之面向过程和面向对象的区别
一.面向过程 1.面向过程:核心是过程二字,过程指的是解决问题的步骤,好比如设计一条流水线,是一种机械式的思维方式. 就是程序从上到下一步步执行,一步步从上到下,从头到尾的解决问题 .基本设计思路就是 ...
- Mask_RCNN caffe2
cd DETECTRON/detectron/ python2 tools/infer_simple_ip_camera.py \ --cfg configs/12_2017_baselines ...