Cod sursa(job #2227663)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 1 august 2018 13:14:58
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 15017;

int n;

int a[N];

int LSB(int x)
{
	return x & (-x);
}

void update(int poz, int val)
{
	for(int i = poz; i <= n; i += LSB(i))
		a[i] += val;
}

int sum(int nod)
{
	if(nod)
		return a[nod] + sum(nod - LSB(nod));
	return 0;
}

void B(int st, int dr)
{
	out << sum(dr) - sum(st - 1) << '\n';
}

int main()
{
	int q;
	in >> n >> q;
	for(int i = 1; i <= n; i++)
	{
		int x;
		in >> x;
		update(i, x);
	}
	while(q--)
	{
		bool c;
		in >> c;
		int a, b;
		in >> a >> b;
		switch(c)
		{
			case(0) : update(a, -b); break;
			case(1) : B(a, b);
		}
	}
}