https://www.luogu.org/problemnew/show/P4868

题目大意

单点修改,查询前缀前缀和。

分析

遇到了单点修改,前缀和,很明显是要树状数组维护解决问题。
请看以下我的数列的转换

\[s1+s2+s3+ \cdots +sn\]

\[a1+a1+a2+a1+a2+a3+ \cdots +an\]

\[a1*n+a2*(n-1)+a3*(n-2)+...an*1\]

\[(a1+a2+a3+...+an) \times N - (a2+a3^2+a4^3+...+an^{n-1})\]

ac代码

#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define N 100005
using namespace std;
template <typename T>
inline void read(T &x) {
    x = 0; T fl = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {
        if (ch == '-') fl = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    x *= fl;
}
struct bittree {
    #define lowbit(x) (x&(-x))
    ll tr[N];
    int n;
    void add(int k, ll val) {
        for (int i = k; i <= n; i += lowbit(i))
            tr[i] += val;
    }
    ll query(int x) {
        ll res = 0;
        for (int i = x; i; i -= lowbit(i))
            res += tr[i];
        return res;
    }
}tr1, tr2;
int n, m;
ll a[N];
int main() {
    read(n); read(m);
    tr1.n = tr2.n = n;
    for (int i = 1; i <= n; i ++) {
        read(a[i]);
        tr1.add(i, a[i]);
        tr2.add(i, a[i] * (i - 1));
    }
    while (m --) {
        char opt[10];
        scanf("%s", opt);
        ll x, y;
        if (opt[0] == 'Q') {
            read(x);
            printf("%lld\n", (ll)(tr1.query(x) * x) - 1ll * tr2.query(x));
        }
        else {
            read(x); read(y);
            tr1.add(x, y - a[x]);
            tr2.add(x, (y - a[x]) * (x - 1));
            a[x] = y;
        }
    }
    return 0;
}

[luogu4868]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. odoo 11 实现多个字段对应一个查询参数的查询

    在整理英语单词开发模块的过程中,有这样一个需求,就是我在查询界面里输入一个查询的值A,这个A可能是下面的任何一个值 1.一个英语单词  2.汉语文字  3.一个英语单词的部分 这里有两张表:engli ...

  2. [Spark][Python]RDD flatMap 操作例子

    RDD flatMap 操作例子: flatMap,对原RDD的每个元素(行)执行函数操作,然后把每行都“拍扁” [training@localhost ~]$ hdfs dfs -put cats. ...

  3. Verilog对数据进行四舍五入(round)与饱和(saturation)截位

    转自https://www.cnblogs.com/liujinggang/p/10549095.html 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件 ...

  4. slurm用户快速入门手册

    1. 概述2. 架构3. 命令3.1 sacct3.2 sattach3.4 sbatch3.5 sbcast3.6 scancel3.7 scontrol3.8 sinfo3.9 smap3.10 ...

  5. 我的AutoHotkey脚本

    #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. ; #Warn ; En ...

  6. cocoapod Podfile use frameworks swift/oc混编 could not build module xxx

    前置: 知名的pod: AFNetworking 我自己的pod:  AFNetworking+RX  3.1.0.18 里面有一段代码是: #import <Foundation/Founda ...

  7. Linux内核分析-创建新进程的过程

    分析Linux内核创建一个新进程的过程 task_struct结构体分析 struct task_struct{ volatile long state; //进程的状态 unsigned long ...

  8. 结对项目https://github.com/bxoing1994/test/blob/master/源代码

    所选项目名称:文本替换      结对人:曲承玉 github地址 :https://github.com/bxoing1994/test/blob/master/源代码 结对人github地址:ht ...

  9. Alpha冲刺随笔汇总

    项目Alpha冲刺(团队) Alpha冲刺随笔汇总 姓名 学号 博客链接 何守成 031602408 http://www.cnblogs.com/heshoucheng/ 黄锦峰 031602411 ...

  10. PAT 1026 程序运行时间

    https://pintia.cn/problem-sets/994805260223102976/problems/994805295203598336 要获得一个C语言程序的运行时间,常用的方法是 ...