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. 第十二章 MySQL触发器(待续)

    ······

  2. Unable to correct problems, you have held broken package

    其实这篇接着上文(一),主要是解决samba安装的问题,中间又是一路曲折.不过这个问题也算是比较典型,有必要记录一下. #apt-get install smb* 安装失败.其实顺利的话,直接一条这样 ...

  3. 主表当中明细表字段的金额计算问题,操作控件是在gridview+aspnetPage

    做这个例子,主要是我在工作当中遇到一个主表的明细表的操作计算问题,也用了不少时间.操作计算的方式是这样的. 这个功能是在.net语言当中实现,操作过程当点击添加行,添加第一行时,当我输入金额的时候,累 ...

  4. IdentityHashMap

    区别与其他的键不能重复的容器,IdentityHashMap允许key值重复,但是——key必须是两个不同的对象,即对于k1和k2,当k1==k2时,IdentityHashMap认为两个key相等, ...

  5. Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart)

    在接触WebService时值得收藏的一篇文章: 在调试Axis1.4访问WebService服务时,出现以下错误: Unable to find required classes (javax.ac ...

  6. 如何用navicat premium 链接Oracel数据库

    1.连接->Oracle 2.填写配置信息 3.填写完成后连接测试,发现报错 : TNS:listener does not currently know of service requeste ...

  7. mysql日期获取

    获取当前日期在本周的周一:select subdate(curdate(),date_format(curdate(),'%w')-1) 获取当前日期在本周的周日:select subdate(cur ...

  8. python if __name__ == '__main__' 作用

    转载:https://stackoverflow.com/questions/419163/what-does-if-name-main-do When your script is run by p ...

  9. 如何安全退出已调用多个Activity的应用

    对于单一Activity的应用来说,退出很简单,直接finish()即可.当然,也可以用killProcess()和System.exit()这样的方法. 但是,对于多Activity的应用来说,在打 ...

  10. Web网站的几个QPS

    评价一个网站的"大小",处于视角的不同,有很多种衡量的方法,类似文章数,页面数之类的数据非常明显,也没有什么可以争议的.但对于并发来说,争议非常之多,这里就从一个技术的角度开始,谈 ...