Cod sursa(job #3309996)

Utilizator HunajaEero Hjickel Hunaja Data 11 septembrie 2025 09:14:06
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.91 kb
// AIB3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

//#include <iostream>
#include <fstream>
using namespace std;
ifstream cin("aib.in");
ofstream cout("aib.out");

struct operation {
	int type;
	int lft;
	int rght;
};
const int nmax = 100001;
int aib[nmax], v[nmax];
int n, k, m;
void update(int pos, int val) {
	for (int i = pos; i <= n; i += i & (-i))
		aib[i] += val;
}
int query(int pos) {
	int sum = 0;
	for (int i = pos; i > 0; i -= i & (-i))
		sum += aib[i];
	return sum;
}
int binary_search(int lft, int rght, int val) {
	int mid = (lft + rght) / 2;
	if (lft > rght) return -1;
	if (query(mid) == val)
		return mid;
	else if (query(mid) > val)
		binary_search(lft, mid - 1, val);
	else binary_search(mid + 1, rght, val);
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		cin >> v[i];
		update(i, v[i]);
								}
	for (int i = 1; i <= m; ++i) {
		operation op;
		cin >> op.type;
		if (op.type == 2) {
			cin >> op.lft;
			cout << binary_search(1, n, op.lft) << '\n';
							}
		else {
			cin >> op.lft >> op.rght;
			if (op.type == 0)
				update(op.lft, op.rght);
			else {
				int res = query(op.rght) - query(op.lft - 1);
				cout << res << '\n';
				}
		}
	}
	return 0;
}


// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file