Pagini recente » Cod sursa (job #2923300) | Cod sursa (job #2804694) | Cod sursa (job #2295954) | Cod sursa (job #220706) | Cod sursa (job #715479)
Cod sursa(job #715479)
#include <fstream>
#include <cstring>
using namespace std;
long long C[16001];
long long N, M;
long long A[16001];
long long k, i, j;
ifstream fin ("datorii.in");
ofstream fout ("datorii.out");
void Citire ()
{
fin >> N >> M;
memset (C, 0, sizeof (C));
for (i = 1; i <= N; i++)
{
fin >> A[i];
k = 1;
j = i;
while (!(j & 1))
{
k <<= 1;
j >>= 1;
}
k = i - (1 << (k - 1)) + 1;
for (j = k; j <= i; j++)
{
C[i] += A[j];
}
}
}
void Scadere (long long T, long long V)
{
while (T <= N)
{
C[T] -= V;
k = 1;
j = T;
while (!(j & 1))
{
k <<= 1;
j >>= 1;
}
T += (1 << (k - 1));
}
}
void Interogare (long long P, long long Q)
{
long long S1 = 0;
long long S2 = 0;
while (P >= 1)
{
S1 += C[P];
k = 1;
j = P;
while (!(j & 1))
{
k <<= 1;
j >>= 1;
}
P -= (1 << (k - 1));
}
while (Q >= 1)
{
S2 += C[Q];
k = 1;
j = Q;
while (!(j & 1))
{
k <<= 1;
j >>= 1;
}
Q -= (1 << (k - 1));
}
S2 -= S1;
fout << S2 << "\n";
}
void Business ()
{
long long a;
long long b;
long long c;
for (i = 1; i <= M; i++)
{
fin >> a >> b >> c;
if (a == 0) Scadere (b, c);
else Interogare (b - 1, c);
}
fin.close ();
fout.close ();
}
int main ()
{
Citire ();
Business ();
return 0;
}