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
题目来源
思路:
思路就是这么个思路:维护(n-i+1)*a[i]和a【i】的前缀和就好了。
(n-bb+1-(cc-bb+1))这个就是多算的一部分。
long long
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=100010;
const int MAXN=100010;
LL a[maxn];
LL C[maxn],C2[maxn];
int lowbit(LL x){
return x&(-x);
}
void add(int id,LL x){
while(id<MAXN){
C[id]+=x;
id+=lowbit(id);
}
}
void add2(int id,int x){
while(id<MAXN){
C2[id]+=x;
id+=lowbit(id);
}
}
LL sum(int id){
LL ans=0;
while(id>0){
ans+=C[id];
id-=lowbit(id);
}
return ans;
}
LL sum2(int id){
LL ans=0;
while(id>0){
ans+=C2[id];
id-=lowbit(id);
}
return ans;
}
int main()
{
LL aa,bb,cc;
LL n,m;
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
add(i,(n-i+1)*a[i]);
add2(i,a[i]);
}
while(m--){
scanf("%lld%lld%lld",&aa,&bb,&cc);
LL haha=0;
if(aa==1)///说明是求和
{
///cout<<"sum(cc):"<<sum(cc)<<" "<<"sum(bb):"<<sum(bb)<<endl;
/// cout<<"&&&:"<<n-bb+1-(cc-bb+1)<<endl;
haha=sum(cc)-sum(bb-1)-(n-bb+1-(cc-bb+1))*(sum2(cc)-sum2(bb-1));
printf("%lld\n",haha);
}
else{///说明是更新.bb位置更新为cc
add(bb,-(n-bb+1)*a[bb]);
add2(bb,-a[bb]);
a[bb]=cc;
add(bb,(n-bb+1)*cc);
add2(bb,cc);
}
}
return 0;
}