#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
/*
node:
value
left, right --> what is the range i am responsible for
index -> actual index in linearized vector
root is 0
index of left = 2*node + 1
index of right = 2*node + 2
l, r = 0 3 Exactly match
41
21 20
6 15 16 4
1 5 3 12 7 9 0 4
size array --> power of 2
fill array = neutral value
Data members:
sz
seg vector
Linearization
member functions:
build
update
query
*/
class segmentTree
{
#define mid (left + right) / 2
#define leftNode 2 * node + 1
#define rightNode 2 * node + 2
private:
int sz; // power of 2
vector<ll> seg;
void build(int left, int right, int node, const vector<ll> &arr)
{
if (left == right) // leaf node
{
if (left < arr.size())
seg[node] = arr[left];
return;
}
// Left segment
build(left, mid, leftNode, arr);
// Right segment
build(mid + 1, right, rightNode, arr);
seg[node] = seg[leftNode] + seg[rightNode];
}
void update(int left, int right, int node, int idx, ll val)
{
if (left == right) // leaf node
{
seg[node] += val;
return;
}
if (idx <= mid)
update(left, mid, leftNode, idx, val);
else
update(mid + 1, right, rightNode, idx, val);
seg[node] = seg[leftNode] + seg[rightNode];
}
ll query(int left, int right, int node, int leftQuery, int rightQuery)
{
if (right < leftQuery || left > rightQuery)
return 0; // neutral value
if (left >= leftQuery && right <= rightQuery)
return seg[node];
ll leftQ = query(left, mid, leftNode, leftQuery, rightQuery);
ll rightQ = query(mid + 1, right, rightNode, leftQuery, rightQuery);
return leftQ + rightQ;
}
public:
// Interface
// Constructor
segmentTree(const vector<ll> &arr)
{
int n = arr.size(); // 25
sz = 1;
while (sz < n)
sz <<= 1;
seg.resize(sz, 0); // Neutral
build(0, sz - 1, 0, arr);
}
void update(int idx, ll val)
{
update(0, sz - 1, 0, idx, val);
}
ll query(int left, int right)
{
return query(0, sz - 1, 0, left, right);
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("Output.txt", "w", stdout
); #endif //! ONLINE_JUDGE
int t = 1;
ll N;
// cin >> t;
while (t--)
{
cin >> N;
vector<ll> vc(N);
for (int i{}; i < N; i++)
cin >> vc[i];
segmentTree segTree(vc);
int Q;
cin >> Q;
while (Q--)
{
int q, left, right;
cin >> q;
if (q == 1)
{
int idx, val;
cin >> idx >> val;
segTree.update(idx, val);
}
else
{
cin >> left >> right;
cout << segTree.query(left, right) << endl;
}
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nIGludAojZGVmaW5lIGVuZGwgIlxuIgoKLyoKbm9kZToKdmFsdWUKbGVmdCwgcmlnaHQgLS0+IHdoYXQgaXMgdGhlIHJhbmdlIGkgYW0gcmVzcG9uc2libGUgZm9yCmluZGV4IC0+IGFjdHVhbCBpbmRleCBpbiBsaW5lYXJpemVkIHZlY3Rvcgpyb290IGlzIDAKaW5kZXggb2YgbGVmdCA9IDIqbm9kZSArIDEKaW5kZXggb2YgcmlnaHQgPSAyKm5vZGUgKyAyCgpsLCByID0gMCAzIEV4YWN0bHkgbWF0Y2gKCQkJCTQxCgkgICAyMSAgICAgICAgICAgICAgICAgIDIwCiAgNiAgICAgICAgIDE1ICAgICAgICAxNiAgICAgICAgNAoxICAgNSAgICAzICAgIDEyICAgIDcgICAgOSAgICAwICAgIDQKc2l6ZSBhcnJheSAtLT4gcG93ZXIgb2YgMgpmaWxsIGFycmF5ID0gbmV1dHJhbCB2YWx1ZQoKRGF0YSBtZW1iZXJzOgpzegpzZWcgdmVjdG9yCkxpbmVhcml6YXRpb24KbWVtYmVyIGZ1bmN0aW9uczoKYnVpbGQKdXBkYXRlCnF1ZXJ5CiovCgpjbGFzcyBzZWdtZW50VHJlZQp7CiNkZWZpbmUgbWlkIChsZWZ0ICsgcmlnaHQpIC8gMgojZGVmaW5lIGxlZnROb2RlIDIgKiBub2RlICsgMQojZGVmaW5lIHJpZ2h0Tm9kZSAyICogbm9kZSArIDIKcHJpdmF0ZToKCWludCBzejsgLy8gcG93ZXIgb2YgMgoJd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