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].
Unfortunately, the longer he learns, the fewer he gets.
That means, if he reads books from ll to rr, he will get 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] (LL is the length of [ ll, rr ] that equals to r - l + 1r−l+1).
Now Ryuji has qq questions, you should answer him:
11. If the question type is 11, you should answer how much knowledge he will get after he reads books [ ll, rr ].
22. If the question type is 22, Ryuji will change the ith book's knowledge to a new value.
First line contains two integers nn and qq (nn, q \le 100000q≤100000).
The next line contains n integers represent a[i]( a[i] \le 1e9)a[i](a[i]≤1e9) .
Then in next qq line each line contains three integers aa, bb, cc, if a = 1a=1, it means question type is 11, and bb, cc represents [ ll , rr ]. if a = 2a=2 , it means question type is 22 , and bb, cc means Ryuji changes the bth book' knowledge to cc
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
样例大意: 第一个行是说要输入5个数据,3个命令。
第二行是五个数据
后三行既是命令和数据操作,第一个是 1 就是查询 [ l , r ]套公式 a[r]a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] 的结果
若是2 就是修改 第 l 位 为 r
所以这个题明显使用线段树或者树状数组来做,但是有当时并没有想到如何来做这个题目,最后经过别人指点,发现可以这样做:
维护两个树状数组,一个数组 b 正常的去存1到 n 的数据,另一个数组 a 则按照题目给的公式存进树状数组就相当于预处理好1到n的查询结果。然后我们会发现,在执行 1 操作的时候,结果就 = a数组l到r的和减去b数组l到r的和乘上((n-l+1)-(r-l+1))
#include
using namespace std;
const long long N=5e5+10;
long long a[N],b[N],c[N];
int lowbit(int x)
{
return x&(-x);
}
void adda(long long x,long long v){
for(long long i=x;i0;i-=lowbit(i)){
sum+=a[i];
}
return sum;
}
long long sumb(long long x){
long long sum=0;
for(long long i=x;i>0;i-=lowbit(i)){
sum+=b[i];
}
return sum;
}
int main()
{
long long n,m;
cin>>n>>m;
for(long long i=0;i>c[i];
adda(i+1,c[i]*(n-i));
addb(i+1,c[i]);
}
for(long long i=0;i>f>>x>>y;
if(f==1)
{
int k;
if((n-x+1-(y-x+1))<=0) k=0;
else k=(n-x+1-(y-x+1));
cout<<(suma(y)-suma(x-1))-(sumb(y)-sumb(x-1))*k<