Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
3.1 kB
1
Indexable
Never
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using db = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<db, db>;
using tiii = tuple<int, int, int>;
using str = string;

#define vt vector
#define pb push_back
#define eb emplace_back
#define ins insert
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

struct segtree {
    
    vector<ll> tree;
    
    int size;
    
    void init(int n) {
        size = 1;
        while (size < n) size *= 2;
        tree.assign(2 * size - 1, 0);
    }
    
    void build(vt<int> &a, int x, int lx, int rx) {
        if (rx - lx == 1) {
            if (lx < a.size())
                tree[x] = a[lx];
        } else {
            int m = (rx + lx) / 2;
            build(a, 2 * x + 1, lx, m);
            build(a, 2 * x + 2, m, rx);
            tree[x] = tree[2 * x + 1] + tree[2 * x + 2];

        }
    }
    
    void build(vt<int> &a) {
        init((int)a.size());
        build(a, 0, 0, size);
    }
    
    void set(int i, int v, int x, int lx, int rx) {
        if (rx - lx == 1) {
            tree[x] = v;
            return;
        }
        int m = (lx + rx)/2;
        
        if (i < m) {
            set(i, v, 2 * x + 1, lx, m);
        }
        else {
            set(i, v, 2 * x + 2, m, rx);
        }
        
        tree[x] = tree[2 * x + 1] + tree[2 * x + 2];
    }
    
    void set(int i, int v) {
        set(i, v, 0, 0, size);
    }
    
    
    ll sum(int l, int r , int x, int lx, int rx) {
        if (l >= rx || lx >= r) {
            return 0;
        }
        
        if (lx >= l && rx <= r){
            return tree[x];
        }
        
        int m = (lx + rx) / 2;
        
        ll s1 = sum(l, r, 2 * x + 1, lx, m);
        ll s2 = sum(l, r, 2 * x + 2, m, rx);
        return s1 + s2;
    }
    
    ll sum(int l, int r){
        return sum(l, r, 0, 0, size);
    }
    
    
    
};

void solve() {
    int n, m;
    cin >> n >> m;
    vt<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    segtree st;
    st.build(a);
    for (int t = 0; t < m; ++t) {
        int c;
        cin >> c;
        if (c == 1) {
            int i, v;
            cin >> i >> v;
            st.set(i, v);
        }
        else {
            int l, r;
            cin >> l >> r;
            cout << st.sum(l, r) << '\n';
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
//     freopen("data.in", "r", stdin);
//     freopen("data.out", "w", stdout);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}