Cod sursa(job #989365)

Utilizator meehighStancu Mihai meehigh Data 25 august 2013 15:43:02
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>

using namespace std;

#define nmax 15001

long a[nmax],n,m;
 
long suma(long i)
{
  long j=0;
  while (i>0)
    {
      j+=a[i];
      i-=i&(i^(i-1));
    }
  return j;
}
 
void add(long i,long x)
{
  while (i<=n)
    {
      a[i]+=x;
      i+=i&(i^(i-1));
    }
}
 
int main()
{ 
   long i,x,y,z;
   ifstream fin("datorii.in");
   ofstream fout("datorii.out");

   fin >> n >> m;
   for (i=1;i<=n;i++)
     {
       fin >> x;
       add(i,x);
     }
   for (i=1;i<=m;i++)
     {
       fin >> x >> y >> z;
       if (x==0)
         {
           add(y,-z);
         }
        else
         {
           fout << suma(z)-suma(y-1) << endl;
         }
     }

   fin.close();
   fout.close();

   return 0;
}