Pagini recente » Cod sursa (job #1479497) | Cod sursa (job #2653814) | Cod sursa (job #2942473) | Cod sursa (job #229258) | Cod sursa (job #2847026)
#include <vector>
#include <cstdint>
#include <cassert>
namespace ja::AdvancedDataStructures::FenwickTree {
#ifndef jassert
#define jassert(expr, msg, ...) assert(expr && msg)
#endif // jassert
#ifndef jlog
#define jlog ((void)0)
#endif // jlog
template < typename DATA, typename OPS >
class FenwickTree {
private:
std::vector<DATA> data;
OPS op{};
inline int32_t lsb(int32_t x) {
return x & (-x);
}
inline void _update(int32_t poz, DATA val) {
jassert(1 <= poz && poz < data.size(), "poz out of bounds", poz);
for(int32_t i = poz; i < data.size(); i += lsb(i))
data[i] = op.add(data[i], val);
}
inline DATA _query(int32_t poz) {
jassert(0 <= poz && poz < data.size(), "poz out of bounds", poz);
DATA ret = op.zero();
for(int32_t i = poz; i > 0; i -= lsb(i))
ret = op.add(ret, data[i]);
return ret;
}
public:
FenwickTree(int32_t n) {
jassert(0 <= n, "n should probably be positive", n);
data.resize(n + 3, op.zero());
};
void update(int32_t position, DATA quantity) {
jassert(1 <= position && position < data.size(), "position is out of bounds", position);
_update(position, quantity);
}
DATA query(int32_t left, int32_t right) {
jassert(1 <= left && left <= right && right < data.size(), "left and right are not correct", left, right);
return op.sub(_query(right), _query(left - 1));
}
};
};
#include <bits/stdc++.h>
using namespace std;
#ifdef INFOARENA
ifstream in("datorii.in");
ofstream out("datorii.out");
#define cin in
#define cout out
#endif // INFOARENA
struct ops {
inline int64_t zero() const {
return 0;
};
inline int64_t add(int64_t a, int64_t b) const {
return a + b;
}
int64_t sub(int64_t a, int64_t b) const {
return a - b;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
namespace ft = ja::AdvancedDataStructures::FenwickTree;
ft::FenwickTree<int64_t, ops> aib(15000);
int n, m; cin >> n >> m;
for(int i = 1; i <= n; i++) {
int x; cin >> x;
aib.update(i, x);
}
while(m--) {
int op, a, b; cin >> op >> a >> b;
if(op == 0) { aib.update(a, -b); }
if(op == 1) { cout << aib.query(a, b) << '\n'; }
}
}