Pagini recente » Cod sursa (job #2151661) | Cod sursa (job #631270) | Cod sursa (job #181388) | Cod sursa (job #1191990) | Cod sursa (job #1443098)
#include <cstdio>
#include <vector>
using namespace std;
constexpr int fenwick_delta(const int x) { return (x) & (-x); }
void add_to_fenwick(vector< short > &fenwick, int poz, const int val) {
for (; poz < fenwick.size(); poz += fenwick_delta(poz)) {
fenwick[poz] += val;
}
}
int query_fenwick(const vector< short > &fenwick, int poz) {
int rez = 0;
for (; poz > 0; poz -= fenwick_delta(poz)) {
rez += fenwick[poz];
}
return rez;
}
void citeste_date(FILE *f, int &m, vector< short > &fenwick) {
int n;
fscanf(f, " %d %d ", &n, &m);
fenwick.resize(n + 1, 0);
for (int i = 1, x; i <= n; ++i) {
fscanf(f, " %d ", &x);
add_to_fenwick(fenwick, i, x);
}
}
int main() {
FILE *f = fopen("datorii.in", "r"),
*g = fopen("datorii.out", "w");
int m;
vector< short > fenwick;
citeste_date(f, m, fenwick);
for (int i = 0, t, a, b; i < m; ++i) {
fscanf(f, " %d %d %d ", &t, &a, &b);
switch (t) {
case 0:
add_to_fenwick(fenwick, a, -b);
break;
case 1:
fprintf(g, "%d\n", (query_fenwick(fenwick, b) -
query_fenwick(fenwick, a - 1)));
break;
}
}
return 0;
}