Cod sursa(job #1443095)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 26 mai 2015 22:49:55
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#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< int > &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;
}