Cod sursa(job #1730530)

Utilizator MihalachiRazvanMihalachi Razvan MihalachiRazvan Data 17 iulie 2016 01:01:03
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <iostream>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
#define MAX 100001
#define dim 100001
long long i,n,m,j,t,q,p,v,s,k,binar,a,ki,bi[dim];
int getparent(int x)
{
    return x-( (x ^ (x - 1)) & x );
}
int getnext(int x)
{
    return x+( (x ^ (x - 1)) & x );
}
void up(int x,int y)
{
   j=x;
   while(j<=n)
   {
       bi[j]=bi[j]+y;
       j=getnext(j);
   }
}
int suma(int x)
{
    if(x==0)
        return 0;
    else
        return bi[x]+bi[getparent(x)];
}
int main()
{
    fin>>n>>m;
         for(i=1;i<=n;i++)
    {
        fin>>a;
        up(i,a);
    }
          for(i=1;i<=m;i++)
          {
              fin>>binar>>p>>q;
              if(binar==1)
              {
                  if(p==1)
                    fout<<suma(q);
                  else
                    fout<<suma(q)-suma(p-1);
                    fout<<"\n";
              }
             else
                 up(p,-q);
          }

     fin.close();
     fout.close();
    return 0;
}