Pagini recente » Cod sursa (job #853500) | Cod sursa (job #3323740) | Cod sursa (job #407660) | Cod sursa (job #2890239) | Cod sursa (job #3326481)
#include <bits/stdc++.h>
#define bit(x) (x & (-x))
using namespace std;
const int NMAX = 15000;
int aib[NMAX + 1];
int n, m;
int query(int pos) {
//suma de la [1, n]
int sum = 0;
while(pos > 0) {
sum += aib[pos];
pos -= bit(pos);
}
// for(int i = pos; i > 0; i -= bit(i)) {
// sum += aib[i];
// }
return sum;
}
void update(int pos, int val) {
while(pos <= n) {
aib[pos] += val;
pos += bit(pos);
}
// for(int i = pos; i <= n; i += bit(i)) {
// aib[pos] += val;
// }
}
int main() {
ifstream cin("datorii.in");
ofstream cout("datorii.out");
cin >> n >> m;
for(int i = 1; i <= n; i++) {
int x;
cin >> x;
update(i, x);
}
for(int i = 1; i <= m; i++) {
int op, x, y;
cin >> op >> x >> y;
if(op == 0) {
update(x, -y);
} else {
cout << query(y) - query(x - 1) << '\n';
}
}
return 0;
}
//x & (-x)
//x & (x ^ (x - 1))
// aib[i] = suma pe intervalul [i - 2^k + 1, i]
// unde k este pozitia celui mai nesemnificativ bit de 1 din i
// aib[12] = [12 - 2^2 + 1, 12] = [9, 12]
// 12 = 1100
// k = 2
// suma pe intervalul [1, 14]
// 14 = 1110
// aib[14] = [14 - 2 ^ 1 + 1, 14] = [13, 14]
// 12 = 1100
// aib[12] = [9, 12]
// 8 = 1000
// aib[8] = [1, 8]
// 0