Cod sursa(job #1442980)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 26 mai 2015 17:10:43
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
#include <vector>
using namespace std;

constexpr int fenwick_delta(const int x){
	return (x) & (-x); }

void add_to_fenwick(vector<int>& fenwick, int poz, const int val){
	for( ; poz < fenwick.size(); poz += fenwick_delta(poz)){
		fenwick[poz] += val; } }

int query_fenwick(const vector<int>& fenwick, int poz){
	int rez = 0;
	for( ; poz > 0; poz -= fenwick_delta(poz)){
		rez += fenwick[poz]; }
	return rez; }

void citeste_date(ifstream& f, int& m, vector<int>& fenwick){
	int n;
	f >> n >> m;
	fenwick.resize(n+1, 0);
	for(int i = 1, x; i <= n; ++i){
		f >> x;
		add_to_fenwick(fenwick, i, x); } }

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