Cod sursa(job #313469)

Utilizator SleepyOverlordPatcas Csaba SleepyOverlord Data 9 mai 2009 04:27:50
Problema Arbori indexati binar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
//Code by Patcas Csaba
//Time complexity:
//Space complexity:
//Method:
//Working time:

#include <stdio.h>
#include <ctype.h>
#include <vector>
#include <string> 
#include <set> 
#include <map> 
#include <iostream> 
#include <sstream> 
#include <numeric> 
#include <algorithm> 
#include <cmath> 
#include <queue> 
#include <bitset> 
#include <time.h>
#include <assert.h>

using namespace std;

#define in_file "aib.in"
#define out_file "aib.out"

#define VI vector <int>
#define FORN(i,n) for(int i=0;i<n;++i)
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define S size()
#define B begin() 
#define E end() 
#define X first
#define RS resize
#define Y second
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(),x.end()
#define repeat do{ 
#define until(x) }while(!(x)); 

int n, m;
VI aib;

void update(int ind, int val)
{
	while (ind<=n)
	{
		aib[ind]+=val;
		ind+=(ind & -ind);
	}
}

int query(int ind)
{
	int ans=0;
	while (ind)
	{
		ans+=aib[ind];
		ind-=(ind & -ind);
	}
	return ans;
}

int main()
{
	FILE* fin=fopen(in_file,"r");
	FILE* fout=fopen(out_file,"w");
	fscanf(fin,"%d%d",&n,&m);
	aib.RS(n+1);
	FOR(i,1,n) 
	{
		int x;
		fscanf(fin,"%d",&x);
		update(i,x);
	}
	FORN(i,m)
	{
		int x, y, z;
		fscanf(fin,"%d%d",&x,&y);
		if (x!=2) fscanf(fin,"%d",&z);
		if (x==0) update(y,z);
		else 
			if (x==1)
			{
				int s1=query(z);
				int s2=query(y-1);
				fprintf(fout,"%d\n",s1-s2);
			}
			else
			{
				int pow2=1, i, sum;
				while (pow2<=n && aib[pow2]<=y) pow2<<=1;
				pow2>>=1;
				sum=aib[pow2];
				for (i=pow2; pow2; pow2>>=1)
					if (i+pow2<=n && sum+aib[i+pow2]<=y) 
					{
						i+=pow2;
						sum+=aib[i];
					}
				if (sum==y) fprintf(fout,"%d\n",i);
				else fprintf(fout,"-1\n");
			}
					
	}
	fclose(fin);
	fclose(fout);

	return 0;
}