Pagini recente » Cod sursa (job #2967080) | Cod sursa (job #2096118) | Cod sursa (job #2326033) | Cod sursa (job #3248467) | Cod sursa (job #2754363)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
const int N = 15001; //limit for array size
int segTree[2 * N];
void constructTree(int n)
{
for (int i = n-1; i > 0; i--)
{
segTree[i] = segTree[2 * i] + segTree[2 * i + 1];
}
}
void update(int position, int value,int n)
{
segTree[n + position] -= value;
position = position + n;
for (int i = position; i > 1; i = i / 2)
{
int parent_index = i / 2;
segTree[parent_index] = segTree[2 * parent_index] + segTree[2 * parent_index + 1];
}
}
int findSum(int left_index, int right_index,int n)
{
int result = 0;
for (left_index += n, right_index += n; left_index < right_index; left_index = left_index / 2, right_index = right_index / 2)
{
if (left_index & 1)
{
result += segTree[left_index++];
}
if (right_index & 1)
{
result += segTree[--right_index];
}
}
return result;
}
int main()
{
int n, m, operation;
fin >> n >> m;
for (int i = n; i < n*2; i++)
{
fin >> segTree[i];
}
constructTree(n);
for (int i = 0; i < m; i++)
{
fin >> operation;
if (operation == 0)
{
int position, value;
fin >> position >> value;
update(position - 1, value, n);
}
else
{
int left_index, right_index;
fin >> left_index >> right_index;
fout << findSum(left_index - 1, right_index,n) << "\n";
}
}
}