Cod sursa(job #2484652)

Utilizator Iulia25Hosu Iulia Iulia25 Data 31 octombrie 2019 12:39:07
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <fstream>

using namespace std;

ifstream fin ("datorii.in");
ofstream fout ("datorii.out");

int n, m, s, x, y, t;
int a[50000];

void Update(int poz, int val)	 {
  a[poz] += val;
  if (poz > 1)
		Update(poz / 2, val);
}

int Query(int st, int dr)	{
	int ans = 0;
  if (st & 1)
		ans += a[st], ++st;
  if (dr % 2 == 0)
		ans += a[dr], --dr;
	if (st < dr)
		ans += Query(st / 2, dr / 2);
	else if (st == dr)
		ans += a[st];
  return ans;
}

int main()	{
  fin >> n >> m;
  int s = 1, cn = n;
  while (n)	 {
		n >>= 1;
		s <<= 1;
  }
  if (s / 2 == cn)
		s >>= 1;
	n = cn;
  for (int i = 0; i < n; ++i)	{
    fin >> x;
    Update(i + s, x);
  }
  for (int i = 1; i <= m; ++i)	{
		fin >> t >> x >> y;
		if (t == 0)
			Update(x + s - 1, -y);
		else fout << Query(x + s - 1, y + s - 1) << '\n';
  }
  return 0;
}