Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
4.3 kB
2
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 <deque>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <iomanip>
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 {
    
    struct node {
        int ch;
        int nch;
    };
    
    node zero = {0, 0};
    
    vt<node> tree;
    int size;
    
    void init(int n){
        size = 1;
        while (n > size) size *= 2;
        tree.assign(2 * size - 1, zero);
    }
    
    
    void build(vt<int> &a, int x, int lx, int rx) {
        if (rx == lx + 1) {
            // cout << x << ' ' << lx << ' ' << rx << endl;
            if (lx < size) {
                if (lx % 2 == 0) {
                    tree[x].ch = a[lx];
                    tree[x].nch = -a[lx];
                } else {
                    tree[x].nch = a[lx];
                    tree[x].ch = -a[lx];
                }
            }
        } else {
            int m = (lx + rx) / 2;
            // cout << x << ' ' << m << ' ' << x << ' ' << lx << ' ' << rx << endl;
            // int g;
            // cin >> g;
            build(a, 2 * x + 1, lx, m);
            build(a, 2 * x + 2, m, rx);
            tree[x].ch = tree[2 * x + 1].ch + tree[2 * x + 2].ch;
            tree[x].nch = tree[2 * x + 1].nch + tree[2 * x + 2].nch;
        }
    }
    
    void build(vt<int> &a) {
        init(sz(a));
        build(a, 0, 0, size);
    }
    
    void update(int i, int v, int x, int lx, int rx) {
        if (rx == lx + 1) {
            if (i % 2 == 0) {
                tree[x].ch = v;
                tree[x].nch = -v;
            } else {
                tree[x].nch = v;
                tree[x].ch = -v;
            }
            return;
        } else {
            int m = (lx + rx) / 2;
            if (i < m) {
                update(i, v, 2 * x + 1, lx, m);
            } else {
                update(i, v, 2 * x + 2, m, rx);
            }
            tree[x].ch = tree[2 * x + 1].ch + tree[2 * x + 2].ch;
            tree[x].nch = tree[2 * x + 1].nch + tree[2 * x + 2].nch;
            return;
        }
    }
    
    void update(int i, int v) {
        update(i, v, 0, 0, size);
    }
    
    ll sum(int l, int r, int x, int lx, int rx) {
        if (r <= lx || l >= rx) {
            return 0;
        }
        if (l <= lx && rx <= r) {
            if (l % 2 == 0) {
                return tree[x].ch;
            } else {
                return tree[x].nch;
            }
        }
        int m = (rx + lx) / 2;
        ll ans1 = sum(l, r, 2 * x + 1, lx, m);
        ll ans2 = sum(l, r, 2 * x + 2, m, rx);
        return ans1 + ans2;
    }
    
    
    ll sum(int l, int r) {
        return sum(l, r, 0, 0, size);
    }
};

void solve() {
    int n;
    cin >> n;
    vt<int> a(n);
    for (int i = 0; i < n; ++i) cin >> a[i];
    segtree st;
    st.build(a);
    
    
    // for (auto i : st.tree) {
    //     cout << '{' << i.ch << ", " << i.nch << '}' << ' ';
    // }
    // cout << endl;
    int m;
    cin >> m;
    while (m--) {
        int f;
        cin >> f;
        if (f == 0) {
            int i, v;
            cin >> i >> v;
            i--;
            st.update(i, v);
        } else {
            int l, r;
            cin >> l >> r;
            l--; r;
            cout << st.sum(l, r) << endl;
        }
    }
}

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;
}