CSES - Shared codeLink to this code:
https://cses.fi/paste/e333bc3c6f40204ca4ac25/
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct Node{
int sum;
Node(){
sum = 0;
}
};
vector<Node> tree(8e5);
vector<int> arr;
Node def;
int n;
Node merge(Node a, Node b){
Node t;
t.sum = a.sum + b.sum;
return t;
}
void build(int index, int start, int end){
if(start == end){
tree[index].sum = arr[start];
return;
}
int mid = (start + end)/2, left = 2*index, right = 2*index + 1;
build(left, start, mid);
build(right, mid+1, end);
tree[index] = merge(tree[left], tree[right]);
}
Node update(int index, int start, int end, int pos, int val){
if(start == end){
tree[index].sum = val;
return tree[index];
}
int mid = (start + end)/2, left = 2*index, right = 2*index + 1;
if(mid >= pos) tree[index] = update(left, start, mid, pos, val);
else tree[index] = update(right, mid+1, end, pos, val);
tree[index] = merge(tree[left], tree[right]);
return tree[index];
}
Node query(int index, int start, int end, int l, int r){
if(start == l && end == r) return tree[index];
else if(l > r) return def;
int mid = (start + end)/2, left = 2*index, right = 2*index + 1;
return merge(query(left, start, mid, l, min(r, mid)), query(right, mid+1, end, max(l, mid+1), r));
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin>>n>>q;
for(int i = 0; i < n; i++){
int num;
cin>>num;
arr.push_back(num);
}
def.sum = 0;
build(1,0,n-1);
while(q--){
int p,a,b;
cin>>p>>a>>b;
if(p == 1) update(1,0,n-1,a-1,b);
else cout<<query(1,0,n-1,a-1,b-1).sum<<endl;
}
return 0;
}