text: update(i, i, sum); sum += i; } vector<long long> haha; for (int i = n - 1; i >= 0; i--) { long long lo = 1; long long hi = n; long long mid; long long ans; long long val; long long temp; while (lo <= hi) { mid = (lo + hi) / 2; if (vis[mid] == 1) { if (lfmost[find_set(mid)] - 1 >= lo) mid = lfmost[find_set(mid)] - 1; else if (rtmost[find_set(mid)] + 1 <= hi) mid = rtmost[find_set(mid)] + 1; else break; } val = B.query(mid, mid); if (val == v[i]) { ans = mid; hi = mid - 1; } else if (v[i] > val) { lo = mid + 1; } else if (v[i] < val) { hi = mid - 1; } } B.update(ans, n, -ans); vis[ans] = 1; make_set(ans); if (vis[ans] == 1 && vis[ans + 1] == 1) union_sets(ans, ans + 1); if (vis[ans] == 1 && vis[ans - 1] == 1) union_sets(ans, ans - 1); haha.push_back(ans); } reverse(haha.begin(), haha.end()); for (auto u : haha) { cout << u << " "; } }