Cod sursa(job #3277943)

Utilizator ridicheTudor Diaconu ridiche Data 18 februarie 2025 10:51:47
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#define case break; case

using namespace std;

ifstream in;
ofstream ou;

int n, m, aib[100005];

void update(int pos, int x)
{
	while (pos <= n)
	{
		aib[pos] += x;
		pos += pos & -pos;
	}
}

int query(int pos)
{
	int r = 0;
	while (pos > 0)
	{
		r += aib[pos];
		pos -= pos & -pos;
	}
	return r;
}

int main()
{
	in.open("aib.in");
	ou.open("aib.out");
	in >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		int x;
		in >> x;
		update(i, x);
	}
	for (int i = 0; i < m; i++)
	{
		int t;
		in >> t;
		switch(t)
		{
			case 0:
			{
				int a, b;
				in >> a >> b;
				update(a, b);
			}
			case 1:
			{
				int a, b;
				in >> a >> b;
				ou << query(b) - query(a-1) << "\n";
			}
			case 2:
			{
				int x;
				in >> x;
				int st = 1, dr = n;
				while (st < dr)
				{
					int mij = (st + dr) / 2;
					if (query(mij) >= x)
					{
						dr = mij;
					}
					else
					{
						st = mij+1;
					}
				}
				ou << st << "\n";
			}
		}
	}
}