Pagini recente » Cod sursa (job #2643760) | Cod sursa (job #3128225) | Cod sursa (job #1817859) | Cod sursa (job #2417397) | Cod sursa (job #287318)
Cod sursa(job #287318)
#include <stdio.h>
#define IN "datorii.in"
#define OUT "datorii.out"
#define max 32000
int arb[max];
int n,m;
int elem=1;
int sol;
FILE *fin=fopen(IN,"r");
FILE *fout=fopen(OUT,"w");
void proceseaza(int,int,int,int);
int main()
{
int i;
int op,x,y;
int curent,poz;
fscanf(fin,"%d %d",&n,&m);
while(elem<n)
elem=(elem<<1);
for(i=1;i<=n;i++)
{
fscanf(fin,"%d",&x);
arb[elem+i-1]=x;
curent=elem+i-1;
while(curent!=1)
{
poz=curent/2;
arb[poz]+=x;
curent/=2;
}
}
for(i=1;i<=m;i++)
{
fscanf(fin,"%d %d %d",&op,&x,&y);
if(op==0)
{
arb[elem+x-1]-=y;
curent=elem+x-1;
while(curent!=1)
{
poz=curent/2;
arb[poz]-=y;
curent/=2;
}
}
else
if(op==1)
{
sol=0;
proceseaza(1,x+elem-1,y+elem-1,elem);
fprintf(fout,"%d\n",sol);
}
}
fclose(fin);
fclose(fout);
return 0;
}
void proceseaza(int nodc,int x,int y,int el)
{
if(x<=nodc*el && (nodc+1)*el-1<=y)
sol+=arb[nodc];
else
if((nodc*el<=x && x<=(nodc+1)*el-1) || (nodc*el<=y && y<=(nodc+1)*el-1))
if(el)
{
proceseaza(nodc*2,x,y,el/2);
proceseaza(nodc*2+1,x,y,el/2);
}
}