Cod sursa(job #1339021)

Utilizator ooptNemes Alin oopt Data 10 februarie 2015 16:47:07
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
// datorii
#include <iostream>
#include <fstream>

#define NMax 15005

using namespace std;

ifstream f("datorii.in");
ofstream g("datorii.out");

int n, m;
int B[NMax];

inline int lastZeros(int x) {
	for (int i=0;i<20;i++)
		if (x & (1<<i))
			return i;

	return 0;
}

int query(int pos) {
	int sum = 0;
	while (pos >= 1) {
		sum+=B[pos];
		pos -= (1<<lastZeros(pos));
	}

	return sum;
}

void update(int value, int pos) {
	while (pos <= n) {
		B[pos]+=value;
		pos += (1<<lastZeros(pos));
	}
}

void read() {

	f>>n>>m;
	for (int i=1;i<=n;i++) {
		int x; f>>x;
		update(x,i);
	}

	for (int i=1;i<=m;i++) {
		int type; f>>type;
		if (type == 0) {
			int t,v;
			f>>t>>v;
			update(-v, t);
		} else if (type == 1) {
			int p,q;
			f>>p>>q;
			// Query
			g<<query(q)-query(p-1)<<'\n';
		}
	}
}

int main() {

	read();

	f.close(); g.close();
	return 0;
}