Cod sursa(job #2753335)

Utilizator VanillaSoltan Marian Vanilla Data 22 mai 2021 14:36:59
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.86 kb
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
typedef long long int64;
typedef vector<int> vec;
typedef vector<int64> vec64;
    string __fname = "aib";
    ifstream in (__fname + ".in");
    ofstream out (__fname + ".out");
    #define cin in
    #define cout out
#define ss cout << " ";
#define nn cout << "\n";
#define ct(x) cout << x;
#define cts(x) cout << x << " ";
#define ctn(x) cout << x << "\n";
#define db(x) cout << "> " << #x << ": " << x << "\n";
#define qr queries();
void solve(int);
void YN(bool b){if (b){ctn("YES");}else{ctn ("NO");}};
void yn(bool b){if (b){ctn("Yes");}else{ctn ("No");}};
void queries(){int n;cin >> n;for (int i = 1; i <= n; i++) solve(i);}
int64 ceildiv(int64 a, int64 b) {return a / b + !!(a % b);}
// // // // // // // // // // // // // // // // // // // // // // 
/*                  TEMPLATE - VANILLA                         */
// // // // // // // // // // // // // // // // // // // // // //
const int maxn = 200200;
const int64 mod = 1000000007;
const double pi = 3.14159265359;
const int ddx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int ddy[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
 
void solve(int id){

    return;
}
 
template <class T> class bit {

    private:
        vector <T> arr;
        int n;

    public: 
        bit(int maxval) {
            n = maxval;
            arr.resize(maxval);
        }

        void update (int pos, T val) {
            for (int i = pos; i < n; i+= (i & -i)) {
                arr[i] += val;
            }
        }

        T query (int k) {
            T s = 0;
            for (int i = k; i>= 1; i-= (i & -i)) {
                s+=arr[i];
            }
            return s;
        }
};
 
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n,k;
    cin >> n >> k;
    vec64 a (n + 1);
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    bit <int64> bit (n + 2);
    for (int i = 1; i <= n; i++){
        bit.update(i, a[i]);
    }
    while (k--) {
        // db(k);
        int64 t, x, y;
        cin >> t >> x;
        if (t == 0) {
            cin >> y;
            a[x]+=y;
            bit.update(x, y);
        }
        else if (t == 1) {
            cin >> y;
            ctn(bit.query(y) - bit.query(x - 1));
        }
        else {
            int64 l = 1, r = n;
            bool f = 0;
            while (l <= r) {
                int64 mid = (l + r) / 2;
                int64 q = bit.query(mid);
                // db(q);
                if (q < x) {
                    l = mid + 1;
                }
                else if (q > x) {
                    r = mid - 1;
                }
                else {
                    f = 1;
                    ctn(mid);
                    break;
                }
            }
            if (!f) {
                ctn(-1);
            }
        }
    }
    return 0;
}