Cod sursa(job #1719809)

Utilizator GabiTulbaGabi Tulba-Lecu GabiTulba Data 20 iunie 2016 13:49:42
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <cstdio>
#include <algorithm>
#define MAX 6000000
#define MAXN 100005
#define INF 2000000005
using namespace std;
 
char f[MAX];
int pos=0,M,N,v[33000],Type,V1,V2,h=1,sp=1;
void r(int &nr)
{
    nr=0;
    while(f[pos]<'0'||f[pos]>'9')
        pos++;
    while(f[pos]>='0'&&f[pos]<='9')
        nr=nr*10+f[pos++]-'0';
}
int POW(int el,int exp)
{
	int ret=1;
	for(int i=1;i<=exp;i++)
		ret*=el;
	return ret;
}
int getsum(int pos)
{
	int Sum=0,i=sp+pos-1;
	while(i>1)
	{
		if(i%2==1)
			Sum+=v[i-1];
		i/=2;
	}
	return Sum;
}
int main()
{
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
	fread(f,1,MAX,stdin);
	r(N);r(M);
	while(sp<N)
		sp*=2,h++;
	for(int i=0;i<N;i++)
	{
		r(v[sp+i]);
		int P=sp+i;
		while(P>1)
		{
			v[P/2]+=v[sp+i];
			P/=2;
		}
	}
	for(int i=1;i<=M;i++)
	{
		r(Type);r(V1);r(V2);
		if(Type==0)
		{
			int P=sp+V1-1;
			while(P>1)
			{
				v[P]-=V2;
				P/=2;
			}
		}
		else
		{
			printf("%d\n",getsum(V2)-getsum(V1)+v[sp+V2-1]);
		}
	}
	return 0;
}