Pagini recente » Cod sursa (job #2761901) | Borderou de evaluare (job #506768) | Borderou de evaluare (job #2016232) | Cod sursa (job #1967211) | Cod sursa (job #3231311)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
struct AIB {
std::vector<int> aib;
int n;
AIB() = default;
explicit AIB(int n_) : n(n_) {
aib.assign(n_, 0);
}
explicit AIB(const std::vector<int> &a) : AIB(a.size()) {
for(int i = 0; i < a.size(); i++) {
aib[i] += a[i];
int r = i | (i + 1);
if(r < n) aib[r] += aib[i];
}
}
int sum(int r) {
int res = 0;
while(r >= 0) {
res += aib[r];
r = (r & (r+1)) - 1;
}
return res;
}
int sum(int l, int r) {
return sum(r) - sum(l-1);
}
void update(int idx, int val) {
while(idx < n) {
aib[idx] += val;
idx = (idx | (idx + 1));
}
}
};
#define ASD
const std::string file = "datorii";
int main() {
#ifdef QWE
std::ifstream fin("../test.in");
std::ofstream fout("../output.out");
#endif
#ifdef ASD
std::ifstream fin(file + ".in");
std::ofstream fout(file + ".out");
#endif
int n, m;
fin >> n >> m;
std::vector<int> v(n);
for(int i = 0; i < n; i++) {
fin >> v[i];
}
AIB aib{v};
for(int i = 0; i < m; i++) {
int op, a, b;
fin >> op >> a >> b;
if(op == 1) {
fout << aib.sum(a - 1, b - 1) << '\n';
}
else {
aib.update(a - 1, -b);
}
}
fin.close();
fout.close();
return 0;
}