Cod sursa(job #3377627)

Utilizator Cezar2009Cezar Mihai Titihazan Cezar2009 Data 23 noiembrie 2026 20:18:11
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.87 kb
// https://www.infoarena.ro/problema/aib

#pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC optimize("O3,unroll-loops")
#pragma GCC target(                                                            \
    "avx512f,avx512bw,avx512dq,avx512cd,avx512vl,bmi,bmi2,lzcnt,popcnt,fma")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt,fma")

// #define _USE_MATH_DEFINES

#include <bits/stdc++.h>

using namespace std;

using int64 = int64_t;
using uint64 = uint64_t;
using int32 = int32_t;
using uint32 = uint32_t;
using int16 = int16_t;
using uint16 = uint16_t;
using pii = pair<int, int>;
using pll = pair<int64, int64>;

#define all(x) (x).begin(), (x).end()
#define allg(x) (x).begin(), (x).end(), greater<int>()
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rfor(i, st, dr) for (auto i = (st); i <= (dr); ++i)
#define for0(i, n) for (auto i = 0; i < (n); ++i)
#define rfor0(i, n) for (auto i = (n) - 1; i >= 0; --i)
#define for1(i, n) for (auto i = 1; i <= (n); ++i)
#define rfor1(i, n) for (auto i = (n); i >= 1; --i)
#define foreach(x, a) for (auto &x : a)
#define cforeach(x, a) for (const auto &x : a)
#define ft first
#define sd second
#define cendl cout << "\n"
#define fendl fout << "\n"
#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(nullptr);

#define int int64

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

// FILE* fin = fopen("", "r");
// FILE* fout = fopen("", "w");

const int NRMAX = 100000;
int aib[NRMAX + 1], v[NRMAX + 1], n;

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

int query(int p) {
  int rez = 0;
  for (int i = p; i >= 1; i -= i & (-i)) {
    rez += aib[i];
  }
  return rez;
}

int build() {
  for (int i = 1; i <= n; ++i) {
    aib[i] += v[i];

    if (i + (i & (-i)) <= n)
      aib[i + (i & (-i))] += aib[i];
  }

  int put = 1;
  while ((put << 1) <= n) {
    put <<= 1;
  }
  return put;
}

int32 main() {
  // FASTIO;

  int m, i, a, b, c;

  fin >> n >> m;
  // cout << n << " " << m << "\n";
  for (i = 1; i <= n; ++i) {
    fin >> v[i];
  }

  int put = build();

  while (m--) {
    fin >> c;
    if (c == 0) {
      fin >> a >> b;
      update(a, b);
    } else if (c == 1) {
      fin >> a >> b;
      fout << (query(b) - query(a - 1)) << "\n";
    } else {
      fin >> a;

      int pos = 0;
      int sum = 0;

      for (int p = put; p; p >>= 1) {
        int nxt = pos + p;

        if (nxt <= n && sum + aib[nxt] <= a) {
          pos = nxt;
          sum += aib[nxt];
        }
      }

      if (pos > 0 && sum == a)
        fout << pos << "\n";
      else
        fout << "-1\n";
    }
  }

  return 0;
}