#include <bits/stdc++.h>
using namespace std;
template <typename T, class F = function<T(const T&, const T&)>>
class SparseTable {
public:
int n;
vector<vector<T>> mat;
F func;
SparseTable(const vector<T>& a, const F& f) : func(f) {
n = static_cast<int>(a.size());
int max_log = 32 - __builtin_clz(n);
mat.resize(max_log);
mat[0] = a;
for (int j = 1; j < max_log; j++) {
mat[j].resize(n - (1 << j) + 1);
for (int i = 0; i <= n - (1 << j); i++) {
mat[j][i] = func(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]);
}
}
}
T get(int from, int to) const {
assert(0 <= from && from <= to && to <= n - 1);
int lg = 32 - __builtin_clz(to - from + 1) - 1;
return func(mat[lg][from], mat[lg][to - (1 << lg) + 1]);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> l(q);
vector<int> r(q);
vector<int> a(q);
for (int i = 0; i < q; i++) {
cin >> l[i] >> r[i] >> a[i];
}
vector<int> L(n, 0), R(n, n - 1);
vector<bool> was(n);
for (int i = 0; i < q; i++) {
L[a[i]] = max(L[a[i]], l[i]);
R[a[i]] = min(R[a[i]], r[i]);
was[a[i]] = true;
}
auto No = [&]() {
for (int i = 0; i < n; i++) {
cout << -1 << " ";
}
cout << '\n';
exit(0);
};
for (int i = 0; i < n; i++) {
if (L[i] > R[i]) {
No();
}
}
vector<vector<vector<int>>> qs(n, vector<vector<int>>(2));
for (int i = 0; i < q; i++) {
qs[l[i]][0].push_back(a[i]);
qs[r[i]][1].push_back(a[i]);
}
vector<int> bound(n);
multiset<int> st;
for (int i = 0; i < n; i++) {
for (int v : qs[i][0]) {
st.insert(v);
}
bound[i] = (st.empty() ? 0 : *prev(st.end()));
for (int v : qs[i][1]) {
st.erase(st.find(v));
}
}
SparseTable<int> mn(bound, [&](int i, int j) { return min(i, j); });
for (int i = 0; i < n; i++) {
if (!was[i]) {
continue;
}
if (mn.get(L[i], R[i]) != i) {
No();
}
}
vector<set<int>> pos(n);
for (int i = 0; i < n; i++) {
pos[bound[i]].insert(i);
}
vector<int> res(n, -1);
for (int i = 0; i < n; i++) {
if (!was[i]) {
continue;
}
auto it = pos[i].lower_bound(L[i]);
if (it == pos[i].end() || *it > R[i]) {
No();
}
res[*it] = i;
}
vector<int> order;
for (int i = 0; i < n; i++) {
if (res[i] == -1) {
order.push_back(i);
}
}
sort(order.begin(), order.end(), [&](int i, int j) {
return bound[i] > bound[j];
});
int ptr = 0;
for (int i = n - 1; i >= 0; i--) {
if (was[i]) {
continue;
}
res[order[ptr]] = i;
ptr += 1;
}
mn = SparseTable<int>(res, [&](int i, int j) { return min(i, j); });
for (int i = 0; i < q; i++) {
if (mn.get(l[i], r[i]) != a[i]) {
No();
}
}
for (int i = 0; i < n; i++) {
cout << res[i] << " ";
}
cout << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
460 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
460 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
77 ms |
27372 KB |
Output is correct |
28 |
Correct |
70 ms |
30408 KB |
Output is correct |
29 |
Correct |
60 ms |
29128 KB |
Output is correct |
30 |
Correct |
102 ms |
32204 KB |
Output is correct |
31 |
Correct |
58 ms |
24572 KB |
Output is correct |
32 |
Correct |
64 ms |
23456 KB |
Output is correct |
33 |
Correct |
33 ms |
26316 KB |
Output is correct |
34 |
Correct |
20 ms |
17884 KB |
Output is correct |
35 |
Correct |
47 ms |
30980 KB |
Output is correct |
36 |
Correct |
13 ms |
2904 KB |
Output is correct |
37 |
Correct |
17 ms |
3676 KB |
Output is correct |
38 |
Correct |
11 ms |
10840 KB |
Output is correct |
39 |
Correct |
15 ms |
3164 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |