Pagini recente » Cod sursa (job #1373907) | Cod sursa (job #1148301) | Cod sursa (job #454886) | Cod sursa (job #2211448) | Cod sursa (job #1251811)
/// Craciun Catalin
/// Datorii
#include <iostream>
#include <fstream>
#include <cmath>
const int NMax = 20000;
using namespace std;
ifstream f("datorii.in");
ofstream g("datorii.out");
int n, m;
int A[NMax];
inline int zeros(int x) {
int zeros = 0;
while (!(x & (1<<zeros)))
zeros++;
return zeros;
}
void insertInBinaryTree(int position, int value) {
while (position <= n) {
A[position] += value;
position += powl(2, zeros(position));
}
}
int queryBinaryTree(int right) {
int sum = 0;
while (right > 0) {
sum += A[right];
right -= powl(2,zeros(right));
}
return sum;
}
int main() {
f>>n>>m;
for (int i=1;i<=n;i++) {
int x; f>>x;
insertInBinaryTree(i, x);
}
for (int i=1;i<=m;i++) {
int type;
f>>type;
if (type == 0) {
int t,v;
f>>t>>v;
insertInBinaryTree(t,-v);
} else if (type == 1) {
int left, right;
f>>left>>right;
g<<queryBinaryTree(right) - queryBinaryTree(left-1)<<'\n';
}
}
f.close(); g.close();
return 0;
}