Cod sursa(job #1443421)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 27 mai 2015 21:36:13
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <vector>
#include <fstream>
using namespace std;

class fenwick_tree{
	vector<int> v;
public:
	void update(int poz, const int val){
		for( ; poz < v.size(); poz += poz&-poz){
			v[poz] += val; } }
	fenwick_tree(ifstream& f, const int n):
		v(n+1, 0){
		for(int i = 1, x; i <= n; ++i){
			f >> x;
			update(i, x); } }
	int query_fenwick(int poz){
		int rez = 0;
		for( ; poz > 0; poz -= poz&-poz){
			rez += v[poz]; }
		return rez; }
	int query_fenwick(const int a, const int b){
		return query_fenwick(b) - query_fenwick(a-1); } };

int main() {
	ifstream f("datorii.in");
	ofstream g("datorii.out");
	int n, m;
	f >> n >> m;
	fenwick_tree arb(f, n);
        for (int i = 0, t, a, b; i < m; ++i) {
		f >> t >> a >> b;
                switch (t) {
                case 0:
                        arb.update(a, -b);
                        break;
                case 1:
			g << arb.query_fenwick(a, b) << '\n';
                        break;
                }
        }
        return 0;
}