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
题目来源
题意:操作 1: 给一段区间 (l , r),求 a[l]*L+a[l+1]*L-1+ .................+a[r]*1 (L = ( r-l+1) )
操作 2:单点更新
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=1e6+5;
ll a[N],tree[N<<2],v[N<<2],sum[N]={0};
void sett(int l,int r,int rt){
if(l==r){
tree[rt]=sum[l];
return ;
}
int mid=l+r>>1;
sett(l,mid,rt<<1);
sett(mid+1,r,rt<<1|1);
pushup();
}
void fun(int l,int r,int rt){
if(v[rt]){
v[rt<<1]+=v[rt];
v[rt<<1|1]+=v[rt];
int mid=l+r>>1;
tree[rt<<1]+=v[rt]*(ll)(mid-l+1);
tree[rt<<1|1]+=v[rt]*(ll)(r-mid);
v[rt]=0;
}
}
void upset(int a,int b,ll vel,int l,int r,int rt){
if(a<=l&&b>=r){
tree[rt]+=vel*(ll)(r-l+1);
v[rt]+=vel;
return ;
}
fun(l,r,rt);
int mid=l+r>>1;
if(a<=mid)
upset(a,b,vel,l,mid,rt<<1);
if(b>mid)
upset(a,b,vel,mid+1,r,rt<<1|1);
pushup();
}
ll findd(int a,int b,int l,int r,int rt){
if(a<=l&&b>=r)
return tree[rt];
fun(l,r,rt);
ll ans=0;
int mid=l+r>>1;
if(a<=mid)
ans+=findd(a,b,l,mid,rt<<1);
if(b>mid)
ans+=findd(a,b,mid+1,r,rt<<1|1);
return ans;
}
int main(){
int n,q;
scanf("%d %d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
sum[i]=sum[i-1]+a[i]; // 线段树维护的 前缀和的和
}
sett(1,n,1);
while(q--){
int aa;
scanf("%d",&aa);
if(aa==1){
int bb,cc;
scanf("%d %d",&bb,&cc);
if(bb-1<=0) printf("%lld\n",findd(bb,cc,1,n,1));
else printf("%lld\n",findd(bb,cc,1,n,1)-(ll)(cc-bb+1)*findd(bb-1,bb-
1,1,n,1));
// bb ~ cc 区间值为要减去 区间长度*前一个节点的值
// 前一节点:它保存的 1 ~ 当前节点的值,在 bb ~ cc 中重复出现了 cc-bb+1 次
}
else{
int bb; ll cc;
scanf("%d %lld",&bb,&cc);
upset(bb,n,cc-a[bb],1,n,1);
a[bb]+=(cc-a[bb]); // 注意:从 a[bb] 更新到 cc, a[bb]需要加 cc-a[bb]
// 更新时,需要把 bb 结点后直到 n 的结点都更新了,因为维护的是前缀和
// 5 10
// 0 0 0 0 0
// 2 2 1
// 2 2 2
// 1 1 2
// 输出 2
}
}
return 0;
}