This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
const int maxElem = 3e5 + 5;
int nbElem;
int level[maxElem];
int lastEdit[maxElem];
int parent[maxElem];
struct comp {
bool operator()(int a, int b) {
return level[a] < level[b];
}
};
using PQ = priority_queue<int, vector<int>, comp>;
PQ hidden[maxElem];
void merge(PQ &dest, PQ &eaten) {
if (dest.size() < eaten.size()) dest.swap(eaten);
while (!eaten.empty()) {
dest.push(eaten.top());
eaten.pop();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> nbElem;
for (int iElem = 1; iElem <= nbElem; ++iElem) {
cin >> lastEdit[iElem];
level[iElem] = max(0, -lastEdit[iElem]);
}
PQ cur;
for (int iElem = nbElem; iElem >= 1; --iElem) {
PQ nxt;
while (!cur.empty() && level[cur.top()] > level[iElem]) {
parent[cur.top()] = iElem-1;
merge(nxt, hidden[cur.top()]);
cur.pop();
}
nxt.push(iElem);
hidden[iElem].swap(cur);
cur.swap(nxt);
}
for (int iElem = 1; iElem <= nbElem; ++iElem) {
if (level[iElem] > 0) {
lastEdit[iElem] = lastEdit[parent[iElem]];
}
cout << lastEdit[iElem] << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |