Cod sursa(job #1357431)

Utilizator rares96cheseliRares Cheseli rares96cheseli Data 23 februarie 2015 22:07:40
Problema Arbori indexati binar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <fstream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#define pb push_back
#define mp make_pair
#define sortvi(a) sort(a.begin(), a.end())
#define FOR(i, start, final) for (int i=start; i<=final; ++i)
#define ROF(i, start, final) for (int i=start; i>=final; --i)
using namespace std;

ifstream f("aib.in");
ofstream g("aib.out");

typedef long long ll;
typedef unsigned long long ull;
typedef pair < int, int> pi;
typedef vector< int > vi;
typedef vector< pair< int, int > > vpi;

int N, M, tip, a, b, aib[100005], x;

void upd(int poz, int val)
{
    while (poz<=N)
        aib[poz]+=val, poz+=poz&(-poz);
}

int query(int poz)
{
    int sum=0;
    while (poz)
        sum+=aib[poz], poz-=poz&(-poz);
    return sum;
}

int main()
{
    f>>N>>M;
    for (int i=1; i<=N; ++i)
        f>>x, upd(i, x);

    while (M--)
    {
        f>>tip>>a;
        if (tip<2)
        {
            f>>b;
            if (tip==0) upd(a, b);
                else g<<query(b)-query(a-1)<<'\n';
            continue;
        }

        int poz=0, lg=(1<<30), q;
        for (; lg; lg>>=1)
            if (poz+lg<=N && aib[poz+lg]<=a)
            {
                poz+=lg; a-=aib[poz];
                if (a==0) break;
            }
        if (a==0) g<<poz<<'\n';
            else g<<-1<<'\n';
    }
    return 0;
}