Cod sursa(job #2748536)

Utilizator osoagentOso Agent osoagent Data 1 mai 2021 12:51:23
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.97 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using oset = tree<T, null_type, less<T>,
				  rb_tree_tag, tree_order_statistics_node_update>;

#ifdef Wi_TEST
	template<typename T1, typename T2>
	ostream& operator<<(ostream& out, pair<T1,T2> p) {
		out << "(" << p.first << ", " << p.second << ")";
		out.flush();
		return out;
	}
	#define deb(x) cout << #x << " = " << x << endl;
#else
	#define deb(x)
	#define cin fin
	#define cout fout
#endif

const long long MOD = 1e9+7;

#define N 100005

unsigned aint[4*N];

void update(int nod, int st, int dr, int poz, unsigned val) {
	if(st == dr)
		aint[nod] += val;
	else {
		int mij = (st + dr) >> 1;
		if(poz <= mij)
			update(2*nod, st, mij, poz, val);
		else
			update(2*nod+1, mij+1, dr, poz, val);
		aint[nod] = aint[2*nod] + aint[2*nod+1];
	}
}

unsigned query(int nod, int st, int dr, int l, int r) {
	if(l <= st && dr <= r)
		return aint[nod];
	else {
		int mij = (st + dr) >> 1;
		unsigned rez = 0;
		if(l <= mij)
			rez = query(2*nod, st, mij, l, r);
		if(r > mij)
			rez += query(2*nod+1, mij+1, dr, l, r);
		return rez;
	}
}

int queryPoz(int nod, int st, int dr, unsigned val) {
	if(aint[nod] == val)
		return dr;
	else {
		int mij = (st + dr) >> 1;
		if(val <= aint[2*nod])
			return queryPoz(2*nod, st, mij, val);
		else
			return queryPoz(2*nod+1, mij+1, dr, val-aint[2*nod]);
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	
	ifstream fin("aib.in");
	ofstream fout("aib.out");
	
	int n, m;
	unsigned x, a, b, c;
	cin >> n >> m;
	
	for(int i = 1; i <= n; ++i) {
		cin >> x;
		update(1, 1, n, i, x);
	}
	
	while(m--) {
		cin >> a >> b;
		if(a == 0) {
			cin >> c;
			update(1, 1, n, b, c);
		} else if(a == 1) {
			cin >> c;
			cout << query(1, 1, n, b, c) << '\n';
		} else {
			int aux = queryPoz(1, 1, n, b);
			if(query(1, 1, n, 1, aux) == b)
				cout << aux << '\n';
			else
				cout << "-1\n";
		}
	}
	
	return 0;
}