Submission #426848

#TimeUsernameProblemLanguageResultExecution timeMemory
426848snasibov05RMQ (NOI17_rmq)C++14
0 / 100
1 ms204 KiB
#include <iostream> #include <vector> #include <algorithm> 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<pii> t; vector<pii> lazy; SegmentTree(int n){ t.resize(n * 4 + 5); build(1, 0, n-1); lazy.resize(n * 4 + 5); lazy.assign(n * 4 + 5, {-1, -1}); } void build(int v, int tl, int tr){ if (tl == tr) { t[v] = {-1, tl}; return; } int tm = (tl + tr) / 2; build(v*2, tl, tm); build(v*2+1, tm+1, tr); t[v] = min(t[v*2], t[v*2+1]); } void push(int v, int tl, int tr){ if (lazy[v] == make_pair(-1, -1)) return; pii k = lazy[v]; lazy[v] = {-1, -1}; k.s = tl; t[v*2] = max(t[v*2], k); lazy[v*2] = k; k.s = tr; 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, l}); lazy[v] = {val, l}; return; } push(v, tl, tr); 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]; } push(v, tl, tr); 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}; } for (int i = 0; i < n && flag; ++i) { int pos = tree.getMin(1, 0, n-1, arr[i].f, arr[i].s).s; ans[pos] = i; tree.update(1, 0, n-1, pos, pos, oo); } for (auto x : ans) cout << x << " "; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...