Cod sursa(job #2533378)

Utilizator rares404AlShaytan - Balasescu Rares rares404 Data 28 ianuarie 2020 22:38:20
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.94 kb
#include <bits/stdc++.h>

const int MV = 1e5 ;

std::fstream in ("aib.in", std::ios::in) ;
std::fstream out ("aib.out", std::ios::out) ;

using i64 = long long ;

class BinaryIndexedTree {
private :
        std::vector<i64> aib ;
        int sz ;
public :
        BinaryIndexedTree(int _ = 0) {
                this ->sz = _ ;
                aib.resize(sz + 1) ;
        }
        void update(int poz, i64 val) {
                for (int i = poz ; i <= sz ; i += (i & -i)) {
                        aib[i] += val ;
                }
        }
        i64 query(int poz) {
                i64 ret(0) ;
                for (int i = poz ; i > 0 ; i -= (i & -i)) {
                        ret += (i64)aib[i] ;
                }
                return ret ;
        }
        i64 BinarySearch(i64 val) {
                int ret(0) ;
                for (int step(1 << 30) ; step && val ; step >>= 1) {
                        if (ret + step <= sz && aib[ret + step] < val) {
                                val -= aib[ret + step] ;
                                ret |= step ;
                        }
                }
                return ret + 1 ;
        }
};

int v[MV + 5] ;

int main() {
        int n, m ;
        in >> n >> m ;
        BinaryIndexedTree aib(n) ;
        int val, type, a, b ;
        for (int i = 1 ; i <= n ; ++ i) {
                in >> v[i] ;
                aib.update(i, (i64)v[i]) ;
        }
        for (int i = 1 ; i <= m ; ++ i) {
                in >> type ;
                if (type == 0) {
                        in >> a >> b ;
                        aib.update(a, (i64)b) ;
                } else if (type == 1) {
                        in >> a >> b ;
                        out << aib.query(b) - aib.query(a - 1) << '\n' ;
                } else {
                        in >> a ;
                        out << aib.BinarySearch(a) << '\n' ;
                }
        }
}