This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |