#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;
}
}
for (int i = 0; i < n; i++) {
if (i < limit) {
cout << -1 << " ";
continue;
}
int low = 0, high = i, p = -1;
while (low <= high) {
int mid = (low + high) >> 1;
if (Check(x[mid], i)) {
p = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
if (p == -1) {
cout << -1 << " ";
} else {
cout << 2 * x[i] - x[p] << " ";
}
}
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
31 ms |
484 KB |
Output is correct |
3 |
Execution timed out |
1037 ms |
600 KB |
Time limit exceeded |
4 |
Execution timed out |
1069 ms |
604 KB |
Time limit exceeded |
5 |
Execution timed out |
1060 ms |
1116 KB |
Time limit exceeded |
6 |
Execution timed out |
1065 ms |
1628 KB |
Time limit exceeded |
7 |
Execution timed out |
1042 ms |
3160 KB |
Time limit exceeded |
8 |
Execution timed out |
1041 ms |
4444 KB |
Time limit exceeded |
9 |
Execution timed out |
1014 ms |
9564 KB |
Time limit exceeded |
10 |
Execution timed out |
1033 ms |
9552 KB |
Time limit exceeded |