Cod sursa(job #2289289)

Utilizator RAZVAN_NISTORNistor Razvan RAZVAN_NISTOR Data 24 noiembrie 2018 12:44:19
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <bits/stdc++.h>
#define Nmx 100005
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int v[Nmx],A[Nmx];
int n,m,poz,start,stop;
void build(int l,int r,int nod)
{
    if(l==r)
        A[nod]=v[l];
    else
    {
        int m=(l+r)/2;
        build(l,m,2*nod);
        build(m+1,r,2*nod+1);
        A[nod]=A[2*nod]+A[2*nod+1];
    }
}
void update(int poz,int val)
{
    while(poz<=n)
    {
        A[poz]+=val;
    poz+=(poz&(-poz));
    }

}
int query(int poz)
{
    int sum=0;
    while(poz>0)
    {
     sum+=A[poz];
     poz-=(poz&(-poz));
    }
    return sum;
}
int cauta_bin(int k)
{
    int l=1,r=n,m,val;
    while(l<=r)
    {
        m=(l+r)/2;
        val=query(m);
        if(val==k)return m;
        if(val<k)l=m+1;
        else
            r=m-1;
    }
    return -1;
}
int main()
{
    int i,x,y;
    int t;
    f>>n>>m;
    for(i=1;i<=n;i++)
        {
            f>>x;
            update(i,x);
        }
    for(i=1;i<=m;i++)
    {
       f>>t;
        if(t==0)
        {
            f>>x>>y;
            update(x,y);
        }
        else
            if(t==1)
            {
                f>>x>>y;
                g<<query(y)-query(x-1)<<"\n";
            }
            else
            {
                f>>x;
                g<<cauta_bin(x)<<"\n";
            }
    }
    return 0;
}