Link to this code:
https://cses.fi/paste/6078f1041f044303c342c6/#include <bits/stdc++.h>
template <typename T> class SegmentTree {
public:
int n;
T idt;
std::vector<T> seg;
std::function<T(T, T)> f;
SegmentTree(int n, std::function<T(T, T)> f = std::plus<T>(), T idt = T()) : n(n), idt(idt), f(f), seg(2 * n, idt) {}
void set(int idx, T x) {
for (seg[idx += n] = x, idx /= 2; idx > 0; idx /= 2) {
seg[idx] = f(seg[2 * idx], seg[2 * idx + 1]);
}
}
T query(int l, int r) {
T ansL = idt, ansR = idt;
for (l += n, r += n + 1; l < r; l /= 2, r /= 2) {
if (l & 1) ansL = f(ansL, seg[l++]);
if (r & 1) ansR = f(seg[--r], ansR);
}
return f(ansL, ansR);
}
int partition_point(int l, const std::function<bool(T)> &t) {
T p = idt;
for (l += n; t(f(p, seg[l])) and l; l /= 2) {
if (l & 1 and l != 1) p = f(p, seg[l++]);
}
if (!l) {
return n;
}
while (l < n) {
if (t(f(p, seg[l <<= 1]))) p = f(p, seg[l++]);
}
return l - n;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
const int64_t inf = 1e15;
int n, m;
std::cin >> n >> m;
SegmentTree<int64_t> st(n, [&](int64_t a, int64_t b) {
return std::max(a, b);
}, -inf);
for (int i = 0, e; i < n; ++i) {
std::cin >> e;
st.set(i, e);
}
while (m--) {
int r;
std::cin >> r;
int idx = st.partition_point(0, [&](int64_t max) {
return max < r;
});
if (idx == n) {
idx = -1;
}
std::cout << idx + 1 << ' ';
if (idx != -1) {
st.set(idx, st.query(idx, idx) - r);
}
}
std::cout << '\n';
}