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.
Input
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
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,m,第二行给你n个数,下面是m个询问,每个询问输入 op,l,r;
sum[i]=(n-i+1)*a[i];
op为操作类型:
op==1,表示让求sum[l]+sum[l+1]+.....+sum[r]的值,输出。
op==2 ,表示吧a[l]的值更新为r;
求(n-i+1)*a[i]前缀和ans1
求得a[i]前缀和ans2
ans=ans1-(n-r)*ans2;
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=100010;
ll a[maxn];
ll c1[maxn];
ll c2[maxn];
int n,m;
ll lowbit(ll x)
{
return x&(-x);
}
void updata1(int pos,ll x)
{
while(pos<=n)
{
c1[pos]+=x;
pos+=lowbit(pos);
}
}
ll getsum1(int pos)
{
ll s1=0;
while(pos>0)
{
s1+=c1[pos];
pos-=lowbit(pos);
}
return s1;
}
void updata2(int pos,ll x)
{
while(pos<=n)
{
c2[pos]+=x;
pos+=lowbit(pos);
}
}
ll getsum2(int pos)
{
ll s1=0;
while(pos>0)
{
s1+=c2[pos];
pos-=lowbit(pos);
}
return s1;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
updata1(i,a[i]);
updata2(i,(n-i+1)*a[i]);
}
int op,l,r;
while(m--)
{
scanf("%d%d%d",&op,&l,&r);
if(op==1)
{
ll ans1=getsum2(r)-getsum2(l-1);
ll ans2=getsum1(r)-getsum1(l-1);
printf("%lld\n",ans1-(n-r)*ans2);
}
else
{
ll temp=getsum1(l)-getsum1(l-1);//a[l]
updata1(l,r-temp);//
updata2(l,(n-l+1)*(r-temp));
}
}
}
}