Pagini recente » Cod sursa (job #166713) | Cod sursa (job #985103) | Cod sursa (job #1189537) | Cod sursa (job #1409475) | Cod sursa (job #2465371)
#include <stdio.h>
#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()
{
freopen("datorii.in","r",stdin);
freopen("datorii.out","w",stdout);
scanf("%d%d",&number_of_nodes,&queries);
Binary_Tree binaryTree;
int add, type, day, val, inter1, inter2;
for(int i = 1 ; i <= number_of_nodes; i++)
{
scanf("%d",&add);
binaryTree.UpdateAtInsertion(i,add);
}
for(int i = 0 ; i < queries; i++)
{
scanf("%d",&type);
if(type == 0) {
scanf("%d%d", &day, &val);
binaryTree.Update(day, val);
}
else
{
scanf("%d%d",&inter1,&inter2);
printf("%d\n",binaryTree.Sum(inter2) - binaryTree.Sum(inter1 - 1));
}
}
return 0;
}