#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cassert>
using namespace std;
#define oo 1000000000
#define pb push_back
#define pii pair<int, int>
#define f first
#define s second
struct range{
int l, r;
int val;
bool operator<(range r1) const{
return val < r1.val;
}
};
struct SegmentTree{
vector<int> t;
vector<int> lazy;
SegmentTree(int n){
t.resize(n * 4 + 5);
t.assign(n * 4 + 5, -1);
lazy.resize(n * 4 + 5);
lazy.assign(n * 4 + 5, -1);
}
void push(int v){
if (lazy[v] == -1) return;
int k = lazy[v]; lazy[v] = -1;
t[v*2] = max(t[v*2], k); lazy[v*2] = max(k, lazy[v*2]);
t[v*2+1] = max(t[v*2+1], k); lazy[v*2+1] = max(k, lazy[v*2+1]);
}
void update(int v, int tl, int tr, int l, int r, int val){
if (tl == l && tr == r){
t[v] = max(t[v], val);
lazy[v] = max(val, lazy[v]);
return;
}
push(v);
int tm = (tl + tr) / 2;
if (l <= tm) update(v*2, tl, tm, l, min(r, tm), val);
if (r > tm) update(v*2+1, tm+1, tr, max(l, tm+1), r, val);
t[v] = max(t[v*2], t[v*2+1]);
}
int getMin(int v, int tl, int tr, int l, int r){
if (tl == l && tr == r){
return t[v];
}
push(v);
int tm = (tl + tr) / 2;
int res = oo;
if (l <= tm) res = min(res, getMin(v*2, tl, tm, l, min(r, tm)));
if (r > tm) res = min(res, getMin(v*2+1, tm+1, tr, max(l, tm+1), r));
return res;
}
};
int main() {
int n, q; cin >> n >> q;
vector<range> v(q);
for (int i = 0; i < q; ++i) {
cin >> v[i].l >> v[i].r >> v[i].val;
}
sort(v.rbegin(), v.rend());
vector<int> ans(n, -1);
vector<int> mn(n, -1);
vector<pii> arr(n);
SegmentTree tree(n);
int k = 0;
bool flag = true;
for (int i = n-1; i >= 0; --i) {
int l = 0, r = n-1;
int x = n-1, y = 0;
while (k < q && v[k].val == i){
l = max(v[k].l, l); x = min(x, v[k].l);
r = min(v[k].r, r); y = max(y, v[k].r);
k++;
}
if (l > r) flag = false;
if (x <= y) tree.update(1, 0, n-1, x, y, i);
arr[i] = {l, r};
}
vector<vector<int>> a(n);
set<int> st;
for (int i = 0; i < n; ++i) {
int x = tree.getMin(1, 0, n-1, i, i);
if (x == -1) st.insert(i);
else a[x].pb(i);
}
for (int i = 0; i < n && flag; ++i) {
for (auto x : a[i]) st.insert(x);
auto it = st.lower_bound(arr[i].f);
if (it == st.end()){
flag = false;
break;
}
int pos = *it;
if (pos > arr[i].s){
flag = false;
break;
}
st.erase(pos);
ans[pos] = i;
}
if (flag) for (auto x : ans) cout << x << " ";
else for (int i = 0; i < n; ++i) cout << "-1 ";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
292 KB |
Output is correct |
24 |
Correct |
2 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
292 KB |
Output is correct |
24 |
Correct |
2 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
140 ms |
11304 KB |
Output is correct |
28 |
Correct |
119 ms |
12324 KB |
Output is correct |
29 |
Correct |
97 ms |
11632 KB |
Output is correct |
30 |
Correct |
125 ms |
12996 KB |
Output is correct |
31 |
Correct |
102 ms |
10112 KB |
Output is correct |
32 |
Correct |
112 ms |
10048 KB |
Output is correct |
33 |
Correct |
51 ms |
10560 KB |
Output is correct |
34 |
Correct |
37 ms |
7300 KB |
Output is correct |
35 |
Correct |
61 ms |
12356 KB |
Output is correct |
36 |
Correct |
109 ms |
9096 KB |
Output is correct |
37 |
Correct |
142 ms |
9396 KB |
Output is correct |
38 |
Correct |
20 ms |
5492 KB |
Output is correct |
39 |
Correct |
90 ms |
8752 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |