树状数组

本文介绍了一种使用树状数组的数据结构来解决Ryuji同学面对的学习效率递减问题的方法。具体而言,Ryuji需要阅读n本书,每本书都有一定的知识价值,但连续阅读会使得获取的知识价值减少。文章提供了两种操作:一是查询从第l本书到第r本书连续阅读所获得的总知识价值;二是更新第i本书的知识价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

彻底入门

模板如下:

int lowb(int t)
{
    return t & (-t);
}

void add(int i,int v)
{
    for( ;i < n;ar[i] += v,i += lowb(i));
    return ;
}

int sum(int i)
{
    int s = 0;
    for(;i > 0;s += ar[i],i -= lowb(i));
    return s;
}

例题:ACM-ICPC 2018 徐州赛区网络预赛  H Ryuji doesn't want to study

 

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]a[i].

Unfortunately, the longer he learns, the fewer he gets.

That means, if he reads books from lll to rrr, he will get a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r]a[l] \times L + a[l+1] \times (L-1) + \cdots + a[r-1] \times 2 + a[r]a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (LLL is the length of [ lll, rrr ] that equals to r−l+1r - l + 1r−l+1).

Now Ryuji has qqq questions, you should answer him:

111. If the question type is 111, you should answer how much knowledge he will get after he reads books [ lll, rrr ].

222. If the question type is 222, Ryuji will change the ith book's knowledge to a new value.

Input

First line contains two integers nnn and qqq (nnn, q≤100000q \le 100000q≤100000).

The next line contains n integers represent a[i](a[i]≤1e9)a[i]( a[i] \le 1e9)a[i](a[i]≤1e9) .

Then in next qqq line each line contains three integers aaa, bbb, ccc, if a=1a = 1a=1, it means question type is 111, and bbb, ccc represents [ lll , rrr ]. if a=2a = 2a=2 , it means question type is 222 , and bbb, ccc means Ryuji changes the bth book' knowledge to ccc

Output

For each question, output one line with one integer represent the answer.

样例输入

5 3
1 2 3 4 5
1 1 3
2 5 0
1 4 5

样例输出

10
8

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值