Cod sursa(job #2763440)

Utilizator AlexZeuVasile Alexandru AlexZeu Data 14 iulie 2021 10:19:39
Problema Arbori indexati binar Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.54 kb
#include <bits/stdc++.h>
#define ll long long
#define nl '\n'
#define FOR(i, a, b) for (int i = a; i <= b; ++i) 
#define F0R(i, a, b) for (int i = a; i >= b; --i)
#define FORd(i, n) for (int i = 0; i < n; ++i)
#define F0Rd(i, n) for (int i = n - 1; i >= 0; --i)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mxN = 1e5 + 5;

int MaxArb[4 * mxN], N, Q;

ifstream fin("aib.in");
ofstream fout("aib.out");

void Update(int node, int Pos, int Left, int Right, int value) {
    if (Left == Right) {
        MaxArb[node] += value;
        return;
    }
    int mij = (Left + Right) / 2;
    if (mij >= Pos) {
        Update(2 * node, Pos, Left, mij, value);
    }
    else {
        Update(2 * node + 1, Pos, mij + 1, Right, value);
    }
    MaxArb[node] = MaxArb[2 * node] + MaxArb[2 * node + 1];
}

void Query(int node, int Left_req, int Right_req, int Left, int Right, int &sum) {
    if (Left_req <= Left && Right <= Right_req) {
        sum += MaxArb[node];
        return;
    }
    int mij = (Left + Right) / 2;
    if (mij >= Left_req) {
        Query(2 * node, Left_req, Right_req, Left, mij, sum);
    }
    if (mij < Right_req) {
        Query(2 * node + 1, Left_req, Right_req, mij + 1, Right, sum);
    }
}

void solve() {
    fin >> N >> Q;
    for (int i = 1; i <= N; ++i) {
        int x;
        fin >> x;
        Update(1, i, 1, N, x);
    }
    while (Q--) {
        int op, a, b;
        fin >> op;
        if (op == 2) {
            fin >> a;
            for (int i = 1; i <= N; ++i) {
                int sum = 0;
                Query(1, 1, i, 1, N, sum);
                if (sum == a) {
                    fout << i;
                    a = -1;
                    break;
                }
            }
            if (a != -1) {
                fout << -1;
            }
        }
        else if (op == 0) {
            fin >> a >> b;
            Update(1, a, 1, N, b);
        }
        else {
            fin >> a >> b;
            int sum = 0;
            Query(1, a, b, 1, N, sum);
            fout << sum;
        }
        fout << nl;
    }
}

int main() {
    fin.tie(0); fout.tie(0);
    ios::sync_with_stdio(0);
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

//read the question correctly (ll vs int)
//what's the meaning of the problem ? Think outside the BOX !!!
//edge cases ?
//make it simple
//write everything (observations, edge cases, ideas, steps, methods, ...)