Cod sursa(job #337105)

Utilizator crisojogcristian ojog crisojog Data 2 august 2009 16:23:46
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.9 kb
#include<stdio.h>
long long c[100010],v[100010],s,temp,n,m,i,j,k,t,ind,poz,val,st,dr,s1,s2,cod;
void read()
{
	scanf("%lld%lld",&n,&m);
	for(i=1;i<=n;++i)
	{
		scanf("%lld",&val);
		v[i]=val;
		s+=val;
		ind=i;
		poz=0;
		while(ind<=n)
		{
			c[ind]+=val;
			temp=ind&(1<<poz);
			while(temp == 0)
			{
		 		poz++;
				temp=ind&(1<<poz);
			}
			ind+=1<<poz;
			poz++;
		}
	}
}
void rez()
{
	for(k=1;k<=m;++k)
	{
		scanf("%lld",&cod);
		if(cod==0)
		{
			scanf("%lld%lld",&ind,&val);
			poz=0;
			s+=val;
			while(ind<=n)
			{
				c[ind]+=val;
				temp=ind&(1<<poz);
				while(temp == 0)
				{
					poz++;
					temp=ind&(1<<poz);
				}
				ind+=1<<poz;
				poz++;
			}
		}
		if(cod==1)
		{
			scanf("%lld%lld",&st,&dr);
			s1=0;
			while(dr>0)
			{
				s1+=c[dr];
				temp=dr&(1<<poz);
				while(temp == 0)
				{
					poz++;
					temp=dr&(1<<poz);
				}
				dr=dr-(1<<poz);
				poz++;
			}
			st=st-1;
			s2=0;
			poz=0;
			while(st>0)
			{
				s2+=c[st];
				temp=st&(1<<poz);
				while(temp == 0)
				{
					poz++;
					temp=st&(1<<poz);
				}
				st=st-(1<<poz);
				poz++;
			}
			printf("%lld\n",s1-s2);
		}
		if(cod==2)
		{
			t=0;
			scanf("%ld",&temp);
			if(temp>s) printf("-1\n");
			else
				if(temp==s) printf("%lld\n",n);
			else
			{
				for(i=1;i<=n;i=i*2)
				{
					if(c[i]==temp) printf("%lld\n",i),t=1;
					else if(c[i]>temp) break;
				}
				if(t==0)
				{
					s1=c[i/2];
					for(j=i/2+1;j<=i;++j)
					{
						s1+=v[j];
						if(s1==temp) printf("%lld\n",j),t=1;
					}
					if(t==0) printf("-1\n");
				}
				if(i>n)
				{
					i=i/2;
					for(j=i;j<=n;++j)
					{
						s1+=v[j];
						if(s1==temp) printf("%lld\n",j),t=1;
					}
					if(t==0) printf("-1\n");
				}
			}
		}
	}
}
int main()
{
	freopen("aib.in","r",stdin);
	freopen("aib.out","w",stdout);
	read();
	rez();
	return 0;
	
	
}