-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1143.cpp
54 lines (49 loc) · 1.12 KB
/
1143.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, h[N], ST[N << 2];
void build(int id, int l, int r) {
if (l == r) {
ST[id] = h[l];
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
ST[id] = max(ST[id << 1], ST[id << 1 | 1]);
}
void update(int id, int l, int r, int u) {
if (l == r) {
ST[id] = h[u];
return;
}
int mid = (l + r) >> 1;
if (u <= mid) update(id << 1, l, mid, u);
else update(id << 1 | 1, mid + 1, r, u);
ST[id] = max(ST[id << 1], ST[id << 1 | 1]);
}
int get(int id, int l, int r, int x) {
if (ST[id] < x) return 0;
if (l == r) return l;
int mid = (l + r) >> 1;
if (ST[id << 1] >= x) return get(id << 1, l, mid, x);
else return get(id << 1 | 1, mid + 1, r, x);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> h[i];
}
build(1, 1, n);
for (int i = 1, r; i <= m; ++i) {
cin >> r;
int p = get(1, 1, n, r);
cout << p << " \n"[i == m];
if (p) {
h[p] -= r;
update(1, 1, n, p);
}
}
return 0;
}