Cod sursa(job #2328153)

Utilizator StefanZamfirStefan Zamfir StefanZamfir Data 25 ianuarie 2019 14:33:10
Problema Arbori indexati binar Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.51 kb
/*


      _             _    ___     ___    _  __       __
   __| |   __ _    / |  / _ \   / _ \  (_)/ /    _  \ \
  / _` |  / _` |   | | | | | | | | | |   / /    (_)  | |
 | (_| | | (_| |   | | | |_| | | |_| |  / /_     _   | |
  \__,_|  \__,_|   |_|  \___/   \___/  /_/(_)   (_)  | |
                                                    /_/


 */

//#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <set>
#include <algorithm>
#include <bitset>
#include <time.h>
#include <tuple>
#include <fstream>
#include <iomanip>
#include <utility>

#pragma warning "da 100% din tine. :)"
#define nl '\n'
#define cnl cout << '\n';
#define pb(x) push_back(x)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ll long long
#define ull unsigned ll
#ifdef INFOARENA
#define ProblemName "aib"
#endif
#define MCONCAT(A, B) A B
#ifdef ProblemName
#define InFile MCONCAT(ProblemName, ".in")
#define OuFile MCONCAT(ProblemName, ".out")
#else
#define InFile "a.in"
#define OuFile "a.out"
#endif

using namespace std;
ifstream cin(InFile);
ofstream cout(OuFile);


template<class v, class type>
void print(v Vector, type nr) {
    for_each(all(Vector), [](type x) {
        cout << x << ' ';
    });
}

void nimic() {
    return;
}

int tree[100'002];
int n, m;

int sum(int k) {
    int s = 0;
    while (k >= 1) {
        s += tree[k];
        k -= k & -k;
    }
    return s;
}

void add(int k, int x) {
    while (k <= n) {
        tree[k] += x;
        k += k & -k;
    }
}

int search(int x) {
    int k = 0, step;
    for (step = n >> 1; step; step >>= 1) {
        while (step + k <= n && sum(step + k) <= x)
            k += step;
    }
    if (sum(k) == x) {
        return k;
    } else return -1;
}

const int mod = 32173;

int main() {
//    freopen(InFile, "r", stdin);
//    freopen(OuFile, "w", stdin);
    ios_base::sync_with_stdio(false);
    clock_t tStart = clock();
    cin >> n >> m;
    int num;
    for (int i = 1; i <= n; ++i) {
        cin >> num;
        add(i, num); //adaug la poz i num
        //inserare in logn
    }
    int op, p1, p2;
    for (int i = 0; i < m; ++i) {
        cin >> op;
        if (op == 0) {
            cin >> p1 >> p2;
            add(p1, p2);
        } else if (op == 1) {
            cin >> p1 >> p2;
            cout << sum(p2) - sum(p1 - 1) << nl;
        } else {
            cin >> p1;
            cout << search(p1) << nl;
        }
    }
    printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}