Pagini recente » Cod sursa (job #253068) | Cod sursa (job #1932655) | Cod sursa (job #1068366) | Cod sursa (job #1749909) | Cod sursa (job #798265)
Cod sursa(job #798265)
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream in("datorii.in");
ofstream out("datorii.out");
int N, M;
in >> N >> M;
vector<int> debt(N);
for(int i = 0; i < N; ++i)
{
int temp, place = i + 1;
in >> temp;
while(place <= N)
{
debt[place - 1] += temp;
int k = 0;
while(!(place & (1 << k))) //Computer k
++k;
place += (1 << k);
}
}
for(int i = 0; i < M; ++i)
{
int type;
in >> type;
if(type == 0)
{
int paid, day, k = 0;
in >> paid >> day;
while(day <= N)
{
debt[day - 1] -= paid;
k = 0;
while(!(day & (1 << k))) //Computer k
++k;
day += (1 << k);
}
}
if(type == 1)
{
int left, right, sum_left = 0, sum_right = 0, k = 0;
in >> left >> right;
left -= 1;
//compute left sum
while(left > 0)
{
sum_left += debt[left - 1];
k = 0;
while(!(left & (1 << k))) //Computer k
++k;
left -= (1 << k);
}
//computer right sum
while(right > 0)
{
sum_right += debt[right - 1];
k = 0;
while(!(right & (1 << k))) //Computer k
++k;
right -= (1 << k);
}
out << sum_right - sum_left << "\n";
}
}
return 0;
}