Pagini recente » Rezultatele filtrării | Rezultatele filtrării | Rezultatele filtrării | Cod sursa (job #36011) | Cod sursa (job #1231465)
#include <iostream>
#include <fstream>
#define zeroes(x) ((x^(x-1))&x)
using namespace std;
int *aib;
int n;
int m;
void Pay(int pos,int ammount)
{
for (int i=pos;i<=n;i+=zeroes(i))
{
aib[i] -= ammount;
}
}
void Add(int pos,int ammount)
{
for (int i=pos;i<=n;i+=zeroes(i))
{
aib[i] += ammount;
}
}
int Compute(int pos)
{
int ret = 0;
for (int i=pos;i>0;i-=zeroes(i))
{
ret += aib[i];
}
return ret;
}
int main()
{
//open files
ifstream infile;
infile.open("datorii.in");
fstream outfile;
outfile.open("datorii.out");
//initialize inputs
infile >> n;
infile >> m;
aib = new int[n+1];
for (int i=1;i<=n;i++)
{
aib[i] = 0;
}
int index;
for (int i=1;i<=n;i++)
{
infile >> index;
Add(i,index);
}
//do operations;
for (int i =1 ;i<=m;i++)
{
char bit;
infile >> bit;
if (bit == '0')
{
int day,ammount;
infile >> day >> ammount;
Pay(day,ammount);
}
else
{
int dayFrom,toDay;
infile >> dayFrom >> toDay;
outfile << (Compute(toDay) - Compute(dayFrom-1)) << "\n";
}
}
return 0;
}