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的更多相关文章

  1. [bzoj3155]Preprefix sum(树状数组)

    3155: Preprefix sum Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 1183  Solved: 546[Submit][Status] ...

  2. BZOJ 3155: Preprefix sum( 线段树 )

    刷刷水题... 前缀和的前缀和...显然树状数组可以写...然而我不会, 只能写线段树了 把改变成加, 然后线段树维护前缀和, 某点p加, 会影响前缀和pre(x)(p≤x≤n), 对[p, n]这段 ...

  3. Preprefix sum BZOJ 3155 树状数组

    题目描述 前缀和(prefix sum)Si=∑k=1iaiS_i=\sum_{k=1}^i a_iSi​=∑k=1i​ai​. 前前缀和(preprefix sum) 则把SiS_iSi​作为原序列 ...

  4. 3155: Preprefix sum

    3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这 ...

  5. 差分+树状数组【p4868】Preprefix sum

    Description 前缀和(prefix sum)\(S_i=\sum_{k=1}^i a_i\). 前前缀和(preprefix sum) 则把\(S_i\)作为原序列再进行前缀和.记再次求得前 ...

  6. 树状数组【bzoj3155】: Preprefix sum

    3155: Preprefix sum 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3155 把给出的a_i当成查分数组d_i做就可以了 ...

  7. 2021.08.09 P4868 Preprefix sum(树状数组)

    2021.08.09 P4868 Preprefix sum(树状数组) P4868 Preprefix sum - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 前缀和(pr ...

  8. BZOJ3155: Preprefix sum

    题解: 写过树状数组搞区间修改和区间求和的就可以秒出吧... 代码: #include<cstdio> #include<cstdlib> #include<cmath& ...

  9. BZOJ 3155: Preprefix sum

    大意:给一个数组,先求出SUM[I],然后动态的求出1-I的SUM[I]的和, 这题得化公式: 树状数组维护两个和:SUM(A[I])(1<=I<=X); SUM(A[I]*(N-I+1) ...

随机推荐

  1. java成神之——enum枚举操作

    枚举 声明 枚举遍历 枚举在switch中使用 枚举比较 枚举静态构造方法 使用类来模拟枚举 枚举中定义抽象方法 枚举实现接口 单例模式 使用静态代码快 EnumSet EnumMap 结语 枚举 声 ...

  2. 第六章 MySQL函数(待续)

    ············

  3. ndarray的创建与数据类型

    ndarray 多维数组(N Dimension Array) NumPy数组是一个多维的数组对象(矩阵),称为ndarray,具有矢量算术运算能力和复杂的广播能力,并具有执行速度快和节省空间的特点. ...

  4. 使用CallableStatement接口调用存储过程

    直接上下代码: package com.learn.jdbc.chap07; import java.sql.CallableStatement; import java.sql.Connection ...

  5. leetcode852

    int peakIndexInMountainArray(vector<int>& A) { int Len = A.size(); ; ; i < Len - ; i++) ...

  6. CentOS-6.4 编译安装ffmpeg加x264以及rtmp

    CentOS 6.4-64位下编译ffmpeg几个简单步骤: 1.编译前环境准备: 2.下载源码: 3.编译,安装: ----------------------------------------- ...

  7. linux启动优化:mdev&nbsp;-s自从…

    转载请说明出处:http://control.blog.sina.com.cn/admin/article/article_add.php 近期工作中遇到的问题如下: linux启动后加载根文件系统时 ...

  8. 【FZU2280】Magic

    题意 给出n个字符串,每个字符串有一个权值wi 有q个操作,操作有两种 1 x y 将字符串x的权值变为y 2 x 查询操作,输出以字符串x为后缀,且权值小于等于wx的字符串个数.其中n<=10 ...

  9. 【HDU 6031]】 Innumerable Ancestors

    题意 有一棵有n个结点的树,这里有m个询问,每个询问给出两个非空的结点集合A和B,有些结点可能同时在这两个集合当中.你需要从A和B中分别选择一个节点x和y(可以是同一个结点)你的目标是使LCA(x,y ...

  10. 刷题向》POJ2823 单调队列裸题(<不会做,请自裁>系列)

    最近BZOJ炸了,而我的博客上又更新了一些基本知识,所以这里刷一些裸题,用以丰富知识性博客 POJ2823   滑动的窗口 这是一道经典的单调队题,我记得我刚学的时候就是用这道题作为单调队列的例题,算 ...