답안 #443554

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
443554 2021-07-10T18:59:42 Z valerikk Santa Claus (RMI19_santa) C++17
90 / 100
251 ms 13920 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

const int N = 96068 + 123;

int n;
int x[N], type[N], val[N];
int res[N];
int sum[4 * N], min_sum[4 * N];
int taken[N];

void update(int i) {
    sum[i] = sum[2 * i] + sum[2 * i + 1];
    min_sum[i] = min(min_sum[2 * i], sum[2 * i] + min_sum[2 * i + 1]);
}

bool check() {
    return min_sum[1] >= 0;
}

void add(int i, int l, int r, int pos, int delta) {
    if (r - l == 1) {
        sum[i] += delta;
        min_sum[i] += delta;
    } else {
        int mid = (l + r) / 2;
        if (pos < mid) {
            add(2 * i, l, mid, pos, delta);
        } else {
            add(2 * i + 1, mid, r, pos, delta);
        }
        update(i);
    }
}

void clear_tree() {
    for (int i = 0; i < 4 * (n + 1); i++) {
        sum[i] = 0;
        min_sum[i] = 0;
    }
}


void go(int l, int t) {
    if (type[l] == 1) {
        add(1, 0, n + 1, val[l], -1 * t);
        if (taken[l] != -1) {
            add(1, 0, n + 1, taken[l], 1 * t);
        }
    }

}

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> type[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> val[i];
    }

    int need = 0;
    for (int i = 0; i < n; i++) {
        if (type[i] == 0) {
            need++;
            type[i] = -1;
        }
    }

    multiset<int> s;
    for (int i = 0; i < n; i++) {
        taken[i] = -1;
        if (type[i] == -1) {
            s.insert(val[i]);
        } else {
            auto it = s.lower_bound(val[i]);
            if (it != s.end()) {
                taken[i] = *it;
                s.erase(it);
            }
        }
    }

    clear_tree();
    int r = n;
    for (int i = 0; i < n; i++) {
        if (type[i] == -1) {
            need--;
        }
        add(1, 0, n + 1, val[i], type[i]);
        if (need == 0 && check()) {
            r = i;
            break;
        }
    }
    for (int i = 0; i < r; i++) {
        res[i] = -1;
    }
    if (r != n) {
        int l = 0;

        for (; r < n; r++) {
            while (l < r) {
                go(l, 1);
                if (!check()) {
                    go(l, -1);
                    break;
                }

                l++;
            }

            res[r] = 2 * x[r] - x[l];
            if (r + 1 != n) {
                add(1, 0, n + 1, val[r + 1], 1);
            }
        }
    }

    for (int i = 0; i < n; i++) {
        cout << res[i] << " ";
    }
    cout << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 7 ms 452 KB Output is correct
4 Correct 17 ms 588 KB Output is correct
5 Correct 39 ms 1312 KB Output is correct
6 Correct 64 ms 2420 KB Output is correct
7 Correct 125 ms 4392 KB Output is correct
8 Correct 192 ms 6592 KB Output is correct
9 Correct 251 ms 10260 KB Output is correct
10 Runtime error 167 ms 13920 KB Execution killed with signal 11