#include #include #include #include #include #include #include #include #include #include #include #include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) using namespace std; template void setmax(T & a, T const & b) { if (a < b) a = b; } template void setmin(T & a, T const & b) { if (b < a) a = b; } // TODO: generalize template struct starry_sky_tree { int n; vector a; // elements and the cache vector b; // lazied queries function append; // associative function foo; // function bar; function bar; T unit; template explicit starry_sky_tree(int a_n, T a_unit, F a_append, G a_foo, H a_bar) { n = pow(2,ceil(log2(a_n))); a.resize(2*n-1, a_unit); b.resize(2*n-1, a_unit); unit = a_unit; append = a_append; foo = a_foo; bar = a_bar; } void range_update(int l, int r, U z) { range_update(0, 0, n, l, r, z); } void range_update(int i, int il, int ir, int l, int r, U z) { if (l <= il and ir <= r) { b[i] = foo(b[i], z); a[i] = bar(a[i], z); } else if (ir <= l or r <= il) { // nop } else { range_update(2*i+1, il, (il+ir)/2, l, r, z); range_update(2*i+2, (il+ir)/2, ir, l, r, z); a[i] = bar( append(a[2*i+1], a[2*i+2]), b[i]); } } T range_concat(int l, int r) { return range_concat(0, 0, n, l, r); } T range_concat(int i, int il, int ir, int l, int r) { if (l <= il and ir <= r) { return a[i]; } else if (ir <= l or r <= il) { return unit; } else { return bar( append( range_concat(2*i+1, il, (il+ir)/2, l, r), range_concat(2*i+2, (il+ir)/2, ir, l, r)), b[i]); } } }; int main() { // input int n; cin >> n; vector as(n); repeat (i,n) cin >> as[i]; // compute map > q; repeat (i,n) { if (not q.count(as[i])) q[as[i]] = { i, i }; setmin(q[as[i]].first, i); setmax(q[as[i]].second, i); } auto f = [](int a, int b) { return max(a, b); }; starry_sky_tree t(n, 0, f, f, f); for (auto it : q) { int a = it.first; int l, r; tie(l, r) = it.second; t.range_update(l, r+1, a); } // output repeat (i,n) { if (i) cout << ' '; cout << t.range_concat(i, i+1); } cout << endl; return 0; }