Cod sursa(job #718211)

Utilizator andu04Popa Andi andu04 Data 20 martie 2012 17:05:09
Problema Arbori indexati binar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.9 kb
#include <iostream>
#include <cstdio>
#define maxim(a,b) ((a>b)?a:b)
using namespace std;

int n,a,b,op,m,pozitie,k=600000;
long arb[210000],nr,val,suma;

void update(int nod,int st,int dr)
{
    if (st==dr)
    {
        arb[nod]+=nr;
        return;
    }
    int mijloc=(st+dr)/2;
    if (pozitie<=mijloc)
        update(2*nod,st,mijloc);
    else
        update(2*nod+1,mijloc+1,dr);
    arb[nod]+=nr;
}
void afla(int nod,int st,int dr)
{
    if (st>=a && dr<=b)
    {
        suma+=arb[nod];
        return;
    }
    int mijloc=(st+dr)/2;
    if (a<=mijloc)
        afla(2*nod,st,mijloc);
    if (b>mijloc)
        afla(2*nod+1,mijloc+1,dr);
}
void op2(int nod,int st,int dr)
{
    if (arb[nod]==val)
    {
        val-=arb[nod];
        k=maxim(st,dr);
        return;
    }
    int mijloc=(st+dr)/2;
    if (arb[nod]>val)
    {
        if (arb[2*nod]>=val)
            op2(2*nod,st,mijloc);
        else
        if (arb[2*nod]<val)
        {
            val-=arb[2*nod];
            op2(2*nod+1,mijloc+1,dr);
        }
    }
}
void citire()
{
    freopen("aib.in","r",stdin);
    freopen("aib.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
    {
        scanf("%ld",&nr);
        pozitie=i;
        update(1,1,n);
    }
    for (int i=1;i<=m;i++)
    {
        scanf("%d",&op);
        if (op==0)
        {
            scanf("%d%ld",&a,&val);
            nr=val;
            pozitie=a;
            update(1,1,n);
        }
        if (op==1)
        {
            suma=0;
            scanf("%d%d",&a,&b);
            afla(1,1,n);
            printf("%ld\n",suma);
        }
        if (op==2)
        {
            k=600000;
            scanf("%ld",&val);
            op2(1,1,n);
            if (val)
                k=-1;
            printf("%d\n",k);
        }
    }
}

int main()
{
    citire();
    return 0;
}