Cod sursa(job #320797)

Utilizator noworkCartas Bogdan nowork Data 5 iunie 2009 20:44:56
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include "fstream"
#include "iostream"

using namespace std;

#define NMAX 15000

long sum[NMAX],a[NMAX];
int n,m;

void construiesteArboreleIndexatBinar(int poz, int val)
{
  if (poz<=n)
  {
	  a[poz] += val;
	  int jump = poz & (-poz);  //2^ nr de zerouri terminale ale lui poz
	  construiesteArboreleIndexatBinar(poz + jump,val);
  }

}
long queryAIB(int poz)    //returneaza suma de la 1 la poz
{
	if (poz>0)
	{
		int jump = poz & (-poz);
		return a[poz] + queryAIB(poz - jump);
	}
	else return 0;
}
void updateAIB(int poz, int val)
{
	if (poz <= n)
	{
		a[poz] -= val;
		int jump = poz & (-poz);
		updateAIB(poz + jump, val);
	}
}
void citire()
{
	int i,bit,x,y;
	ifstream f("datorii.in");
	ofstream g("datorii.out");
	f>>n>>m;
	for (i=0;i<n;i++)
	{
		f>>x;
		construiesteArboreleIndexatBinar(i + 1, x);
	}

	for (i=0;i<m;i++)
	{
		f>>bit>>x>>y;
		if (bit == 1)
			g<<queryAIB(y) - queryAIB(x - 1)<<endl;
		if (bit == 0)
		{
			updateAIB(x,y);
		}
	}
	g.close();
	f.close();

}
int main()
{
	citire();
	return 0;
}