Cod sursa(job #1856524)

Utilizator jason2013Andronache Riccardo jason2013 Data 25 ianuarie 2017 00:03:29
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include<bits/stdc++.h>
#define in "aib.in"
#define out "aib.out"

using namespace std;

const int NMAX = 100001;
int N,M, a[NMAX];

inline void updateIndexedTree(int position, int value)
{
    if(position <= N)
    {
        a[position] += value;
        position = position + (position & (-position));
    }
}

inline int Query(int p)
{
    int sum = 0;
    while(p <= N)
    {
        sum += a[p];
        p = p - (p & (-p));
    }
    return sum;
}

inline int BS(int a)
{
    int st, dr, mid, p, x;
    st = 1;
    dr = N;
    p = -1;
    while(st <= dr)
    {
        mid = (st + dr) / 2;
        x = Query(mid);
        if(x == a)
        {
            p = mid;
            dr = mid - 1;
        }
        else if( x > a)
            dr = mid - 1;
        else st = mid + 1;
    }
    return p;
}

void citire()
{
    freopen(in, "r", stdin);
    freopen(out, "w", stdout);

    scanf("%d%d", &N, &M);
    int x, option, pos, a, b, k;
    for(int i = 1; i <= N; i++)
    {
        scanf("%d", &x);
        updateIndexedTree(i, x);
    }

    for(int i = 1; i <= M; i ++)
    {
        scanf("%d", &option);
        if(option == 0)
        {
            scanf("%d%d", &pos, &x);
            updateIndexedTree(pos, x);
            break;
        }
        else if(option == 1)
        {
            scanf("%d%d", &a, &b);
            printf("%d/n", Query(a) - Query(b-1));
        }
        else
        {
            scanf("%d", &k);
            printf("%d\n", BS(k));
        }
    }
}

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