10.20 Bzoj 3155 Preprefix sum(树状数组)

Bzoj 3155 Preprefix sum

题意:

长度为n的序列,m次操作
操作有两种:1.询问前缀和的前缀和;2.修改某个ai的值

解题思路:

S 1 = a 1 S_1=a_1 S1=a1
S 2 = a 1 + a 2 S_2=a_1+a_2 S2=a1+a2
. . . ... ...
S n = a 1 + a 2 + . . . + a n S_n=a_1+a_2+...+a_n Sn=a1+a2+...+an
− − − − − − − − − − − − − − − − − − − − --------------------
S S 1 = S 1 SS_1=S_1 SS1=S1
S S 2 = S 1 + S 2 SS_2=S_1+S_2 SS2=S1+S2
. . . ... ...
S S n = S 1 + S 2 + . . . + S n SS_n=S_1+S_2+...+S_n SSn=S1+S2+...+Sn
当修改ai为w时,Si~Sn都会发生变化,每个变化之后改变w-ai
SSn~SSi区间加上w-ai
到这里就可以直接利用树状数组存储S的值,进行区间修改,和区间查询即可,最后别忘了每次要更新ai的值

AC代码:

#include <bits/stdc++.h>
#define lowbit(x) (x&(-x))
#define endl '\n'
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
ll n, m;
ll a[maxn], suma[maxn], sum1[maxn], sum2[maxn];
void update(ll i, ll k) {//树状数组的区间操作模板函数
    ll x = i;
    while (i <= n) {
        sum1[i] += k;
        sum2[i] += k * (x - 1);
        i += lowbit(i);
    }
}
ll getsum(ll i) {
    ll res = 0, x = i;
    while (i > 0) res += x * sum1[i] - sum2[i], i -= lowbit(i);
    return res;
}
int main() {
    IOS;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) 
        cin >> a[i];
    for (int i = 1; i <= n; i++) {
        suma[i] = suma[i - 1] + a[i];
        update(i, suma[i] - suma[i - 1]);
    } 
    string oper; ll x, y;
    while (m--) {
        cin >> oper;
        if (oper == "Query") {
            cin >> x;
            cout << getsum(x) << endl;
        }
        else {
            cin >> x >> y;
            ll tmp = y - a[x];
            a[x] = y;//更新值
            update(x, tmp), update(n + 1, -tmp);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ContextCapture 10.20是一个软件程序,用于进行地理数据的捕捉和处理。安装ContextCapture 10.20需要按照以下步骤进行操作:首先,安装主程序。其次,在安装完成后,需要打补丁来确保软件的正常运行。最后,安装中文包以方便使用中文界面。 如果您已经完成了安装,可以在桌面上找到软件ContextCapture Center Master,并打开文件安装的位置。接下来,您需要将数据中提供的bentley.entliclib.dll文件放到该目录下,以替换原文件。 如果您还没有下载软件,请先准备好软件包数据。您可以在提供的链接中下载ContextCapture 10.20的软件包数据,并使用提取码进行解压。然后运行CCCenter-10.20.0.4117.x64-en文件来开始安装。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【CC精品教程】ContextCapture 10.20安装教程(附CC10.20安装包下载)](https://blog.csdn.net/lucky51222/article/details/132390301)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【板栗糖GIS】CC—ContextCapture10.20的安装教程](https://blog.csdn.net/weixin_46192906/article/details/127230148)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值