Pagini recente » Cod sursa (job #3253143) | Rating Mihnea Andreescu (segtreap) | Cod sursa (job #2082835) | Cod sursa (job #1833951) | Cod sursa (job #3168855)
#include <fstream>
std::ifstream fin("aib.in");
std::ofstream fout("aib.out");
int32_t N, M, aib[100005];
int32_t ub(int32_t x) {
return (x & (-x));
}
void add(int32_t a, int32_t x) {
for(int32_t i = a; i <= N; i += ub(i)) {
aib[i] += x;
}
}
int32_t sum(int32_t x) {
int32_t rez = 0;
for(int32_t i = x; i >= 1; i -= ub(i)) {
rez += aib[i];
}
return rez;
}
int main()
{
fin >> N >> M;
for(int32_t i = 1; i <= N; i++) {
int32_t x; fin >> x;
add(i, x);
}
for(int32_t i = 1; i <= M; i++) {
int32_t type; fin >> type;
//fout << "tip smecher " << type << "\n";
if(type == 0) {
int32_t a, b; fin >> a >> b;
add(a, b);
} else if(type == 1) {
int32_t a, b; fin >> a >> b;
fout << sum(b) - sum(a - 1) << "\n";
} else {
int32_t s; fin >> s;
int32_t pos = 0;
int32_t summ = 0;
for(int32_t bits = 17; bits >= 0; bits--) {
if(pos + (1 << bits) <= N && summ + aib[pos + (1 << bits)] < s) {
summ += aib[pos + (1 << bits)];
pos += (1 << bits);
}
}
if((pos + 1 <= N) && sum(pos + 1) == s) {
fout << pos + 1 << "\n";
} else {
fout << "-1\n";
}
}
}
return 0;
}