Cod sursa(job #1164118)

Utilizator UVS_Elfus_Deneo_KiraUVS-Elfus-Dutzul-Kira UVS_Elfus_Deneo_Kira Data 1 aprilie 2014 20:58:21
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include<fstream>
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
#define FOR(a,b,c) for(int a=b;a<=c;++a)
#include<cstring>
#include<bitset>
#include<cmath>
#include<iomanip>
#include<queue>
#define f cin
#define g cout
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define mod 1000000007
#define ll unsigned long long
#define bit (1<<18)
#define inf 500000000
#define N 100100
#define mod  1999999973
#define inu "dfs.in"
#define outu "dfs.out"
using namespace std;
ifstream f(inu);
ofstream g(outu);
//int dx[]={0,0,0,1,-1};
//int dy[]={0,1,-1,0,0};
int H[N],n,a,b,x,po,t,m;
void upd(int po)
{
	for(;po<=n;po+=po&-po) H[po]+=x;
}
long long sum(int po)
{
	int S=0;
	for(;po;po-=po&-po) S+=H[po];
	return S;
}
int cb(int x)
{
	int st=1,dr=n,mij,sol=-1;
	long long cs;
	while(st<=dr)
	{
		mij=(st+dr)>>1;
		cs=sum(mij);
		if(cs<x)
			st=mij+1;
		else
			dr=mij-1;
		if(cs==x)
			sol=mij;
	}
	return sol;
}

int main ()
{
	f>>n>>m;
	FOR(i,1,n)
	{
		f>>x;
		upd(i);
	}
	FOR(i,1,m)
	{
		f>>t;
		if(!t)
		{
			f>>po>>x;
			upd(po);
		}
		if(t==1)
		{
			f>>a>>b;
			g<<sum(b)-sum(a-1)<<"\n";
		}
		if(t==2)
		{
			f>>x;
			g<<cb(x)<<"\n";
		}
	}
	return 0;
}