1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <bits/stdc++.h> #include <random> constexpr int N = 2e5 + 10; using namespace std;
struct Node { int l, r; int siz; int val, key; int max; bool revs; }; vector<Node> fhq; int cnt, root; std::mt19937_64 rnd(23333);
int create(int val) { cnt++; fhq[cnt].val = val, fhq[cnt].max = 0; fhq[cnt].key = rnd(); fhq[cnt].siz = 1; return cnt; } void update_size(int now) { int lft = fhq[now].l; int rt = fhq[now].r; fhq[now].siz = fhq[lft].siz + fhq[rt].siz + 1; fhq[now].max = std::max({fhq[lft].max, fhq[lft].val, fhq[rt].val, fhq[rt].max}); } void split_by_size(int now, int siz, int &x, int &y) { if (!now) x = y = 0; else { int &l = fhq[now].l; int &r = fhq[now].r; if (fhq[fhq[now].l].siz < siz) { x = now; split_by_size(fhq[now].r, siz - fhq[fhq[now].l].siz - 1, fhq[now].r, y); } else { y = now; split_by_size(fhq[now].l, siz, x, fhq[now].l); } update_size(now); } } int merge(int x, int y) { if (!x || !y) return x + y; if (fhq[x].key < fhq[y].key) { fhq[x].r = merge(fhq[x].r, y); update_size(x); return x; } else { fhq[y].l = merge(x, fhq[y].l); update_size(y); return y; } } int locateMax(int p) { if (p == 0) return 0; int l = fhq[p].l, r = fhq[p].r;
int lmax = std::max({fhq[l].val, fhq[l].max}); int rmax = std::max({fhq[r].val, fhq[r].max});
if (fhq[p].val > lmax && fhq[p].val > rmax) return 1 + fhq[l].siz; else if (lmax > fhq[p].val && lmax > rmax) return locateMax(l); else return fhq[l].siz + 1 + locateMax(r); }
int n, a[N];
int main() { std::cin >> n, fhq.resize(N * 2); a[0] = 0, a[n + 1] = 0;
for (int i = 1; i <= n; i++) { std::cin >> a[i]; root = merge(root, create(a[i])); }
std::vector<int> v; for (int i = 1, l, r; i <= n; i++) { int id = locateMax(root); split_by_size(root, id - 1, l, r);
v.emplace_back((n - i + 1) - id + 1); root = merge(r, l); split_by_size(root, 1, r, root); } std::reverse(v.begin(), v.end()); for (auto f : v) std::cout << f << " "; std::cout << "\n"; }
|