QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#739758 | #9733. Heavy-light Decomposition | Woufan | WA | 1ms | 3848kb | C++17 | 1.4kb | 2024-11-12 22:59:13 | 2024-11-12 22:59:14 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n, k;
std::cin >> n >> k;
std::vector<int> p(n + 1);
std::vector<std::array<int, 2>> ch(k);
for (int i = 0; i < k; i++) {
std::cin >> ch[i][0] >> ch[i][1];
for (int j = ch[i][0] + 1; j <= ch[i][1]; j++) {
p[j] = j - 1;
}
}
std::sort(ch.begin(), ch.end(), [&](auto x, auto y) -> bool {
return x[1] - x[0] > y[1] - y[0];
});
p[ch[0][0]] = 0;
auto sz = [&](auto x) -> int {
return x[1] - x[0] + 1;
};
int idx = ch[0][1] - 1, curr = 1, j = k - 1;
while (idx >= ch[0][0] && j >= 1) {
int tmp = 0;
while (j >= 1 && idx >= ch[0][0] && curr >= sz(ch[j])) {
p[ch[j][0]] = idx;
tmp += sz(ch[j]);
j--;
}
curr += tmp;
while (j >= 1 && idx >= ch[0][0] && curr < sz(ch[j])) {
idx--, curr++;
}
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
cnt += p[i] == 0;
if (cnt > 1) {
std::cout << "IMPOSSIBLE" << '\n';
return;
}
}
for (int i = 1; i <= n; i++) {
std::cout << p[i] << " \n"[i == n];
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0), std::cout.tie(0);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3848kb
input:
3 12 5 1 5 9 11 7 8 6 6 12 12 4 3 1 1 4 4 2 3 2 2 1 1 2 2
output:
0 1 2 3 4 4 4 7 4 9 10 4 2 0 2 2 IMPOSSIBLE
result:
wrong answer 5 should be a heavy child, but the maximum one with size 3 and 1 found. (test case 1)