题目链接:H. Ryuji doesn't want to study
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-------------"<<endl
typedef long long ll;
const ll maxn = 1e5+10;
const ll MAXN = 1e6+10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9+7;
const ll N = 1010;
ll n, q;
ll a[maxn];
struct node{
ll l, r, val, ans;
ll mid(){
return (l + r) >> 1;
}
}tree[maxn << 2];
void pushup(ll x){
tree[x].val = tree[x<<1].val + tree[x<<1|1].val;
tree[x].ans = tree[x<<1].ans + tree[x<<1|1].ans;
}
void build(ll x, ll l, ll r){
tree[x].l = l; tree[x].r = r;
if(l == r){
tree[x].val = a[l];
tree[x].ans = a[l] * (n-l+1);
return ;
}
ll mid = tree[x].mid();
build(x<<1, l, mid);
build(x<<1|1, mid+1, r);
pushup(x);
}
void update(ll x, ll pos, ll val){
if(tree[x].l == tree[x].r){
tree[x].val = val;
tree[x].ans = val * (n-tree[x].l+1);
return ;
}
ll mid = tree[x].mid();
if(mid >= pos) update(x<<1, pos, val);
else if(mid < pos) update(x<<1|1, pos, val);
pushup(x);
}
ll ans1 = 0, ans2 = 0;
void query(ll x, ll l, ll r){
if(tree[x].l >= l && tree[x].r <= r){
ans1 += tree[x].ans;
ans2 += tree[x].val;
return ;
}
ll mid = tree[x].mid();
if(mid >= l) query(x<<1, l, r);
if(mid < r) query(x<<1|1, l, r);
}
int main(){
while(scanf("%lld%lld", &n, &q) != EOF){
for(ll i=1; i<=n; i++) scanf("%lld", &a[i]);
build(1, 1, n);
while(q--){
ans1 = ans2 = 0;
ll x, y, z;
scanf("%lld%lld%lld", &x, &y, &z);
if(x == 1){
query(1, y, z);
printf("%lld\n", ans1 - ans2*(n-z));
}
else{
update(1, y, z);
}
}
}
return 0;
}