Cod sursa(job #2527155)

Utilizator RedXtreme45Catalin RedXtreme45 Data 19 ianuarie 2020 18:40:46
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#define Nmax 100001
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int n,m;
int aib[Nmax],v[Nmax];
void creare(int poz,int val){
    while (poz<=n)
    {
        aib[poz]+=val;
        poz+=(poz&(-poz));
    }
}
int suma(int poz)
{
    int S=0;
    while (poz>0)
    {
        S+=aib[poz];
        poz-=(poz&(-poz));
    }
    return S;
}
int cautare(int S)
{
    int p=1,u=n;
    while (p<=u)
    {
        int m=(p+u)/2;
        int x=suma(m);
        if (S<x)
        {
            u=m;
        }
        if (S>x)
        {
            p=m+1;
        }
        if (S==x)
            return m;
    }
    return -1;
}
int main()
{
    int i,b,a,j;
    fin>>n>>m;
    for (i=1;i<=n;i++)
    {
        fin>>v[i];
        creare(i,v[i]);
    }
    for (i=1;i<=m;i++)
    {
        int p;
        fin>>p;
        if (p==0)
        {
            int a,b;
            fin>>a>>b;
            creare(a,b);
        }
        if (p==1)
        {
            int a,b;
            fin>>a>>b;
            a--;
            fout<<suma(b)-suma(a)<<"\n";
        }
        if (p==2)
        {
            int a;
            fin>>a;
            fout<<cautare(a)<<"\n";
        }
    }
    return 0;
}