Cod sursa(job #1877178)

Utilizator ClaudiuHHiticas Claudiu ClaudiuH Data 13 februarie 2017 01:34:57
Problema Datorii Scor 100
Compilator cpp Status done
Runda becreative11 Marime 1.34 kb
#include <fstream>
using namespace std;

const int lg = 100005;

int init[lg], t[4*lg+200], a, b, rez;

inline int max(int a,int b)
{
	return (a > b) ? a : b;
}
 
void construiesc(int st,int dr,int poz)
{
      if (st==dr)
      {
		  init[st]=poz;
		  return ;
       }
          
      int x=(st+dr)/2;
      construiesc(st,x,2*poz);
      construiesc(x+1,dr,2*poz+1);
}
 
void actualizare(int poz,int val)
{
     int x = init[poz];
     t[x] = val;
     x >>= 1;
     while(x)
     {
		 t[x] = t[x]+val;
		x>>=1;
	}
}
  
  
void actualizare2(int poz,int val)
{
     int x=init[poz];
     t[x]=t[x]-val;
    x>>=1;
     while(x)
     {t[x]=t[x]-val;
      x>>=1;
}
}

void interogare(int st,int dr,int poz)
{
     if (st>b||dr<a) return;
     if (a<=st&&dr<=b)
               rez=rez+t[poz];
          else
          if (st<dr)
               {int x=(st+dr)/2;
                 interogare(st,x,poz*2);
                 interogare(x+1,dr,poz*2+1);
                 }
}
  
int main()
{	int i, x, n, m;
    ifstream fin("datorii.in");
    ofstream fout("datorii.out");
    fin >> n >> m;
 
    construiesc(1,n,1);
       
     for(i = 1; i <= n; i++)
     {
		  fin>>x;
		  actualizare(i,x);
     }
     
	for(i=1;i<=m;i++)
	{
		fin>>x>>a>>b;
		if (x==1)  
		{
			rez=0;
			interogare(1,n,1);
			fout<<rez<<"\n";
		}

		else
		if (x==0)
			actualizare2(a,b);
	}

return 0;
}