Cod sursa(job #1256215)

Utilizator LegionHagiu Stefan Legion Data 5 noiembrie 2014 22:00:36
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream in("datorii.in");
	ofstream out("datorii.out");
	int n,y,k, i, zerouri[15001], numere[15001], m,tot,d;
	unsigned int t,x,xa,ya;
	in >> n;
	in >> m;
	for (t = 1; t <= 15000; t++)
	{
		numere[t] = 0;
		x = t;
		k = 0;
		while (((x & 1) == 0) && ((x | 0) != 0)){ k++; x=x >> 1; }
		zerouri[t] = k; 
	}
	for (i = 1; i <= n; i++)
	{
		in >> t;
		k = zerouri[i];
		for (x = i; x <= n; x += k)
		{
			numere[x] += t;
			if (k>0)k += k;
			else k += 1;
		}
	}
	for (d = 1; d <= m; d++)
	{
		in >> t;
		if (t == 0)
		{
			in >> x;
			in >> y;
			k = zerouri[x];
			for (i = x; i <= n; i+=k)
			{
				numere[i] -= y;
				if (k > 0)k += k;
				else k += 1;
			}
		}
		else
		{
			tot = 0;
			in >> xa;
			in >> ya;
			x = xa-1;
			while (x > 0)
			{
				tot -= numere[x];
				x = (x >> zerouri[x])-1;
			}
			x = ya;
			while (x > 0)
			{
				tot += numere[x];
				x = (x >> zerouri[x])-1;
			}
			out << tot << "\n";
		}
	}
	return 0;
	
}