Cod sursa(job #1268092)

Utilizator UVS_Elfus_Deneo_KiraUVS-Elfus-Dutzul-Kira UVS_Elfus_Deneo_Kira Data 20 noiembrie 2014 16:50:16
Problema Arbori indexati binar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
/*
    Look at me!
    Look at me!
    The monster inside me has grown this big!
*/
 
#include<fstream>
#include<iostream>
#include<cstdio>
#include<map>
#include<set>
#define FIT(a,b) for(vector<int >::iterator a=b.begin();a!=b.end();a++)
#define RIT(a,b) for(vector<int>::reverse_iterator a=b.end();a!=b.begin();++a)
#include<stack>
#define ROF(a,b,c) for(int a=b;a>=c;--a)
#include<vector>
#include<algorithm>
#define FOR(a,b,c) for(int a=b;a<=c;++a)
#define REP(a,b) for(register int a=0;a<b;++a)
#include<cstring>
#include<bitset>
#include<cmath>
#include<iomanip>
#include<queue>
#define debug cerr<<"OK";
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define inf 1<<30
#define eps 1.e-10
#define N 100100
#define mod 1999999973
using namespace std;
/*int dx[]={0,0,0,1,-1};
int dy[]={0,1,-1,0,0};*/
ifstream f("aib.in");
ofstream g("aib.out");
int A[N],n,tip,x,y,m;
void upd(int p,int x)
{
	for(;p<=n;p+=p&-p)
		A[p]+=x;
}
int sum(int p)
{
	int sum=0;
	for(;p;p-=p&-p)
		sum+=A[p];
	return sum;
}
int find(int x)
{
	int po=1;
	for(;po<=n;po<<=1);
	po>>=1;
	int cur=0;
	for(;po;po>>=1)
	if(A[cur+po]<=x&&cur+po<=n)
	{
		x-=A[cur+po];
		cur+=po;
	}
	if(x)
		cur=-1;
	return cur;
}
int main ()
{
	f>>n>>m;
	FOR(i,1,n)
	{
		f>>x;
		upd(i,x);
	}
	FOR(i,1,m)
	{
		f>>tip;
		if(!tip)
		{
			f>>x>>y;
			upd(x,y);
		}
		if(tip==1)
		{
			f>>x>>y;
			--x;
			g<<sum(y)-sum(x)<<"\n";
		}
		if(tip==2)
		{
			f>>x;
			g<<find(x)<<"\n";
		}
	}
    return 0;
}