#include <bits/stdc++.h>
using namespace std;
struct node {
int sum;
int mn;
};
node Merge(node a, node b) {
node ret;
ret.sum = a.sum + b.sum;
ret.mn = min(a.mn, a.sum + b.mn);
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
vector<int> x(n);
for (int i = 0; i < n; i++) {
cin >> x[i];
}
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
auto xs = v;
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
v[i] = (int) (lower_bound(xs.begin(), xs.end(), v[i]) - xs.begin());
}
vector<node> st(8 * n);
function<void(int, int, int)> Build = [&](int id, int l, int r) {
st[id].sum = 0;
st[id].mn = 0;
if (l == r) {
return;
}
int mid = (l + r) >> 1;
Build(id * 2, l, mid);
Build(id * 2 + 1, mid + 1, r);
};
function<void(int, int, int, int, int)> Modify = [&](int id, int l, int r, int i, int v) {
if (l == r) {
st[id].sum += v;
st[id].mn += v;
return;
}
int mid = (l + r) >> 1;
if (i <= mid) {
Modify(id * 2, l, mid, i, v);
} else {
Modify(id * 2 + 1, mid + 1, r, i, v);
}
st[id] = Merge(st[id * 2], st[id * 2 + 1]);
};
auto Check = [&](int from, int to) {
Build(1, 0, n - 1);
for (int i = 0; i <= to; i++) {
if (x[i] >= from && h[i] == 1) {
Modify(1, 0, n - 1, v[i], +1);
}
}
for (int i = to; i >= 0; i--) {
if (x[i] >= from && h[i] == 1) {
continue;
}
if (h[i] == 0) {
Modify(1, 0, n - 1, v[i], -1);
} else {
Modify(1, 0, n - 1, v[i], +1);
}
if (st[1].mn < 0) {
return false;
}
}
return true;
};
int limit = 0;
for (int i = 0; i < n; i++) {
if (h[i] == 0) {
limit = i;
}
}
int ptr = 0;
for (int i = 0; i < n; i++) {
if (i < limit) {
cout << -1 << " ";
continue;
}
while (ptr < i && Check(x[ptr + 1], i)) {
ptr += 1;
}
if (!Check(x[ptr], i)) {
cout << -1 << " ";
} else {
cout << 2 * x[i] - x[ptr] << " ";
}
}
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
348 KB |
Output is correct |
2 |
Correct |
16 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1075 ms |
608 KB |
Time limit exceeded |
4 |
Execution timed out |
1058 ms |
860 KB |
Time limit exceeded |
5 |
Execution timed out |
1046 ms |
1116 KB |
Time limit exceeded |
6 |
Execution timed out |
1045 ms |
1880 KB |
Time limit exceeded |
7 |
Execution timed out |
1051 ms |
3160 KB |
Time limit exceeded |
8 |
Execution timed out |
1012 ms |
4496 KB |
Time limit exceeded |
9 |
Execution timed out |
1094 ms |
9552 KB |
Time limit exceeded |
10 |
Execution timed out |
1061 ms |
9556 KB |
Time limit exceeded |