Submission #426902

#TimeUsernameProblemLanguageResultExecution timeMemory
426902snasibov05RMQ (NOI17_rmq)C++14
0 / 100
1 ms204 KiB
#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] = k; t[v*2+1] = max(t[v*2+1], k); lazy[v*2+1] = k; } 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] = val; 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] = min(t[v*2], t[v*2+1]); } pii getMin(int v, int tl, int tr, int l, int r){ if (tl == l && tr == r){ return {t[v], tl}; } push(v); int tm = (tl + tr) / 2; pii res = {oo, 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) { pii x = tree.getMin(1, 0, n-1, i, i); if (x.f == -1) st.insert(x.s); else a[x.f].pb(x.s); } for (int i = 0; i < n && flag; ++i) { if (st.empty()) { flag = false; break; } for (auto x : a[i]) st.insert(x); int pos = *st.lower_bound(arr[i].f); if (pos > arr[i].s) { flag = false; break; } st.erase(pos); ans[pos] = i; tree.update(1, 0, n-1, pos, pos, oo); } if (flag) for (auto x : ans) cout << x << " "; else for (int i = 0; i < n; ++i) cout << "-1 "; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...