Cod sursa(job #1164664)

Utilizator SilverGSilver Gains SilverG Data 2 aprilie 2014 11:20:07
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
/*
	Keep It Simple!
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<cstdio>

#define MaxN 100005

int Aib[MaxN],N,M;

void Add(int position, int value)
{
	while (position <= N)
	{
		Aib[position] += value;
		position += (position & -position);
	}
}

int Sum(int position)
{
	int S = 0;
	while (position)
	{
		S += Aib[position];
		position -= (position & -position);
	}
	return S;
}

int FirstSum(int value)
{
	int position = 1;
	while (Aib[position] != value && position <= N)
		position *= 2;
	if (Aib[position] == value)
		return position;
	else
		return -1;
}

int main()
{
	freopen("aib.in", "r", stdin);
	freopen("aib.out", "w", stdout);

	scanf("%d%d", &N, &M);

	int x,y,type;

	for (int i = 1; i <= N; i++)
	{
		scanf("%d", &x);
		Add(i, x);
	}
	for (int i = 1; i <= M; i++)
	{
		scanf("%d", &type);
		if (type == 0)
		{
			scanf("%d%d", &x, &y);
			Add(x, y);
		}
		else if (type == 1)
		{
			scanf("%d%d", &x, &y);
			printf("%d\n", Sum(y) - Sum(x - 1));
		}
		else if (type == 2)
		{
			scanf("%d", &x);
			printf("%d\n",FirstSum(x));
		}
	}
}