Cod sursa(job #2722993)

Utilizator CiboAndreiAndrei Cibo CiboAndrei Data 13 martie 2021 14:26:57
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.1 kb
#include <bits/stdc++.h>

using namespace std;

//#define f cin
//#define g cout
//ifstream f("data.in");
//ofstream g("data.out");
ifstream f("aib.in");
ofstream g("aib.out");

//#define int long long

const int dim = 1e5 + 2;
const int mod = 100003;
#define pii pair <int, int>

int m, n;
int aib[dim];

void nos(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

int readInt () {
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

void update(int x, int val){
    for(; x <= n; x += x & -x)
        aib[x] += val;
}

int sum(int x){
    int s = 0;

    for(; x > 0; x -= x & -x)
        s += aib[x];

    return s;
}

void sum(int st, int dr){
    g << sum(dr) - sum(st - 1) << '\n';
}

void caubin(int x){
    int st = 1, dr = n, m, p = -1;

    while(st <= dr){
        m = (st + dr) / 2;
        int s = sum(m);

        if(s == x)
            p = m;
        if(s < x)
            st = m + 1;
        else dr = m - 1;
    }

    g << p << '\n';
}

void read(){
    f >> n >> m;
    int x;
    for(int i = 1; i <= n; ++i){
        f >> x;
        update(i, x);
    }
}

void solve(){
    int p, a, b;

    for(int i = 1; i <= m; ++i){
        f >> p;

        switch(p){
            case 0:
                f >> a >> b;
                update(a, b);
                break;

            case 1:
                f >> a >> b;
                sum(a, b);
                break;

            case 2:
                f >> a;
                caubin(a);
                break;
        }
    }
}

void restart(){

}

int32_t main()
{
    nos();
    read();
    solve();
    //restart();

    return 0;
}