Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 2e5 + 10;
- int n;
- int h[maxn], r[maxn];
- int segment_tree[3 * maxn];
- void build_tree(int L, int R, int pos) {
- if(L == R) {
- segment_tree[pos] = h[L];
- }
- else {
- int middle = (L + R) / 2;
- build_tree(L, middle, 2 * pos);
- build_tree(middle + 1, R, 2 * pos + 1);
- segment_tree[pos] = max(segment_tree[2 * pos], segment_tree[2 * pos + 1]);
- }
- }
- void update(int L, int R, int pos, int idx, int value) {
- if(L == R) {
- segment_tree[pos] -= value;
- return;
- }
- int middle = (L + R) / 2;
- if(idx <= middle) {
- update(L, middle, 2 * pos, idx, value);
- }
- else {
- update(middle + 1, R, 2 * pos + 1, idx, value);
- }
- segment_tree[pos] = max(segment_tree[2 * pos], segment_tree[2 * pos + 1]);
- }
- int query(int L, int R, int pos, int x) {
- if(L == R) {
- return L + 1;
- }
- int middle = (L + R) / 2;
- if(segment_tree[2 * pos] >= x) {
- return query(L, middle, 2 * pos, x);
- }
- else {
- return query(middle + 1, R, 2 * pos + 1, x);
- }
- return 0;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- int m;
- cin >> n >> m;
- for(int i = 0; i < n; i++) {
- cin >> h[i];
- }
- for(int i = 0; i < m; i++) {
- cin >> r[i];
- }
- build_tree(0, n - 1, 1);
- for(int i = 0; i < m; i++) {
- if(segment_tree[1] < r[i]) {
- cout << 0 << " ";
- }
- else {
- int x = query(0, n - 1, 1, r[i]);
- update(0, n - 1, 1, x - 1, r[i]);
- cout << x << " ";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement