Pagini recente » Cod sursa (job #2490398) | Cod sursa (job #1940105) | Cod sursa (job #2384862) | Cod sursa (job #2427292) | Cod sursa (job #2916378)
#include <fstream>
std::ifstream in("datorii.in");
std::ofstream out("datorii.out");
struct nod {
int sum, l, r;
nod *st, *dr;
};
nod *root = new nod;
int n, m, v[1001];
void cons(nod *root, int l, int r) {
root->l = l;
root->r = r;
if (l == r) {
root->sum = v[l];
} else {
root->st = new nod;
root->dr = new nod;
cons(root->st, l, (l + r) / 2);
cons(root->dr, (l + r) / 2 + 1, r);
root->sum = root->st->sum + root->dr->sum;
}
}
int sum(nod *root, int l, int r) {
if (root->r < l || root->l > r) return 0;
if (r >= root->r && l <= root->l) return root->sum;
return sum(root->st, l, r) + sum(root->dr, l, r);
}
void update(nod *root, int ind, int diff) {
root->sum += diff;
if (root->l == root->r) return;
if (ind <= (root->l + root->r) / 2)
update(root->st, ind, diff);
else
update(root->dr, ind, diff);
}
int main() {
in >> n >> m;
for (int i = 1; i <= n; i++) {
in >> v[i];
}
cons(root, 1, n);
for (int i = 0; i < m; i++) {
int c, a, b;
in >> c >> a >> b;
if (c == 0) {
update(root, a, -b);
} else if (c == 1) {
out << sum(root, a, b) << '\n';
}
}
}