Pagini recente » Cod sursa (job #2744677) | Cod sursa (job #2797998) | Cod sursa (job #2924047) | Cod sursa (job #2535605) | Cod sursa (job #3142363)
#include <vector>
#include <iostream>
#include <fstream>
class BinaryIndexTree {
public:
using t = int;
explicit BinaryIndexTree(size_t N): N_{N}, tree_(N, 0) {}
void update(const t pos, const t value) {
for (t i = pos; i <= N_; i += lsb(i)) {
tree_[i - 1] += value;
}
}
t query(const t pos) const {
t sum = 0;
for (auto i = pos; i >= 1; i -= lsb(i)) {
sum += tree_[i - 1];
}
return sum;
}
int findPosFor(const t sum) const {
size_t idx = 0;
size_t bitMask;
t ssum = sum;
for (bitMask = 1; bitMask <= N_; bitMask <<= 1);
for (; bitMask; bitMask >>= 1) {
if ( (idx + bitMask) <= N_ && tree_[idx + bitMask - 1] < ssum) {
idx += bitMask;
ssum -= tree_[idx - 1];
}
}
if (query(idx + 1) != sum) {
return -1;
}
return idx + 1;
}
private:
constexpr t lsb(t x) const {return (x & (-x));}
const size_t N_;
std::vector<t> tree_;
};
int main() {
std::ifstream in{"aib.in"};
std::ofstream out{"aib.out"};
int N, M;
int op, a, b;
in >> N >> M;
BinaryIndexTree t(N);
for (int i = 1; i <= N; ++i) {
in >> a;
t.update(i, a);
}
for (int i = 1; i <= M; ++i) {
in >> op >> a;
if (op == 2) {
out << t.findPosFor(a) << '\n';
} else {
in >> b;
if (op == 1) {
out << ( t.query(b) - t.query(a - 1)) << '\n';
} else {
t.update(a, b);
}
}
}
return 0;
}