Pagini recente » Cod sursa (job #516100) | Cod sursa (job #767174) | Cod sursa (job #495798) | Cod sursa (job #113021) | Cod sursa (job #2465377)
#include <stdio.h>
#include <iostream>
#include <fstream>
#define maxNumber 15005
using namespace std;
int number_of_nodes, queries;
class Binary_Tree{
public:
// data structures
int nodes[maxNumber]; // stores the values for every node
// constructors
void UpdateAtInsertion(int index, int value); // changes the value of the nodes in the binary tree at insertion
void Update(int index, int value);
int Sum(int position); // the sum of the elements
};
void Binary_Tree :: UpdateAtInsertion(int index, int value) {
while(index <= number_of_nodes)
{
nodes[index] += value;
index += (index) & (-index);
}
}
int Binary_Tree :: Sum(int position) {
int result = 0;
while(position)
{
result += nodes[position];
position -= (position) & (-position);
}
return result;
}
void Binary_Tree::Update(int index, int value) {
while(index <= number_of_nodes)
{
nodes[index] += value;
index += (index) & (-index);
}
}
int main()
{
ifstream fin("datorii.in");
ofstream fout("datorii.out");
//freopen("datorii.in","r",stdin);
//freopen("datorii.out","w",stdout);
//scanf("%d%d",&number_of_nodes,&queries);
fin >> number_of_nodes >> queries;
Binary_Tree binaryTree;
int add, type, day, val, inter1, inter2;
for(int i = 1 ; i <= number_of_nodes; i++)
{
fin >> add;
//scanf("%d",&add);
binaryTree.Update(i,add);
}
for(int i = 0 ; i < queries; i++)
{
fin >> type;
//scanf("%d",&type);
if(type == 0) {
fin >> day >> val;
//scanf("%d%d", &day, &val);
binaryTree.Update(day, -val);
}
else
{
fin >> inter1 >> inter2;
//scanf("%d%d",&inter1,&inter2);
fout << binaryTree.Sum(inter2) - binaryTree.Sum(inter1 - 1) << "\n";
}
}
return 0;
}