首页 > 其他分享 >Luogu P4868 Preprefix sum

Luogu P4868 Preprefix sum

时间:2022-10-25 15:36:49浏览次数:67  
标签:P4868 return int Luogu sum tree build include ll


题目链接:​​传送门​

线段树维护前缀和
简单明了
修改就修改
当然还有更快的树状数组差分的做法


*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
struct node {
int l, r; ll w, f;
}tree[A];
ll s[A], delta;
int n, m, a[A], x, y;
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r;
if (l == r) {
tree[k].w = s[l];
return;
}
int m = (l + r) >> 1;
build(k << 1, l, m);
build(k << 1 | 1, m + 1, r);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
void down(int k) {
tree[k << 1].f += tree[k].f; tree[k << 1 | 1].f += tree[k].f;
tree[k << 1].w += tree[k].f * (tree[k << 1].r - tree[k << 1].l + 1);
tree[k << 1 | 1].w += tree[k].f * (tree[k << 1 | 1].r - tree[k << 1 | 1].l + 1);
tree[k].f = 0;
}
void change(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) {
tree[k].w += delta * ll(tree[k].r - tree[k].l + 1);
tree[k].f += delta;
return;
}
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1;
if (l <= m) change(k << 1, l, r);
if (r > m) change(k << 1 | 1, l, r);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
ll ask(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) return tree[k].w;
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1; ll ans = 0;
if (l <= m) ans += ask(k << 1, l, r);
if (r > m) ans += ask(k << 1 | 1, l, r);
return ans;
}

int main(int argc, char const *argv[]) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s[i] = s[i - 1] + a[i];
build(1, 1, n);
while (m--) {
char opt[7]; scanf("%s", opt);
if (opt[0] == 'Q') scanf("%d", &x), printf("%lld\n", ask(1, 1, x));
else scanf("%d%d", &x, &y), delta = y - a[x], a[x] = y, change(1, x, n);
}
return 0;
}


标签:P4868,return,int,Luogu,sum,tree,build,include,ll
From: https://blog.51cto.com/lyle/5794774

相关文章

  • Luogu P4514 上帝造题的七分钟
    题目链接:​​传送门​​二维树状数组区间加区间求和烦人的输入#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#include<......
  • Luogu P2455 [SDOI2006]线性方程组
    题目链接:​​传送门​​高斯消元可以去下面看一下​​​https://www.bilibili.com/video/av4688674​​​听视频比瞅博客有用得多这题算比较标准的板子了各种情况都有......
  • Kafka Server之kafka-console-consumer.bat
    KafkaServer之kafka-console-consumer.bat注意:博主使用kafka版本:kafka_2.12-3.3.1.tgzwindows版一、订阅主题全部消息(注意:Producer已经生产:0~4999共5000条消息)在k......
  • Luogu P3833 [SHOI2012]魔法树
    题目链接:​​传送门​​树剖板子区间加,子树查询树剖里查询的时候x和y地方小于号写反T了一会a,b写成dfn[a],dfn[b]竟然还有50分又WA了一会也就交了二十遍。#include<io......
  • Luogu P1438 无聊的数列
    题目链接:​​传送门​​还是区间加等差数列时隔8个月再写一遍这个题不会的来​这里#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include......
  • Luogu P3980 [NOI2008]志愿者招募
    题目链接:​​传送门​​别人家的建图~~~~好神奇很容易想到志愿者的起始时间和终止时间连边,费用就是他的费用但是每个点还有一个人数限制必须要有那么多个人也就是那么......
  • Luogu P2221 [HAOI2012]高速公路
    题目链接:​​传送门​​维护路径期望值,带区间修改看每条路径会被统计多少次贡献非常不显然是方案数就是上面的是分子下面的是分母现在要把上面的展开看怎么维护直接......
  • Luogu P2342 叠积木
    题目链接:​​传送门​​虽然看着很简单还是计较了好久!fa[]是这堆的最底下那块的编号siz[]是底块所在堆的sizeans[]就是ans,这块底下有多少块在find的时候沿路更新答案即......
  • Luogu P2150 [NOI2015]寿司晚宴
    题目链接:​​传送门​​太难了太难了题意就是问有多少种分案把一个到的排列分配为两组并使组间元素两两互质首先我们只需要考虑根号内的质因子对答案的影响,因为根号外的因......
  • Luogu P2515 [HAOI2010]软件安装
    题目链接:​​传送门​​很明显,如果图中有一个环那么这个环上的点必须都要选那我们一开始就直接缩点因为每个物品有价值有重量还有有重量限制所以是很明显的树上背包我......