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 <bits/stdc++.h>
using namespace std;
const int INF = 1e8;
const int JUMP_REQ_LEN = 450; // required len for the jump
struct Node {
int val = 0, lvl = 0;
// a - what should the node be if this was replaced
// b - where should you look for the lower level node
Node *a = nullptr, *b = nullptr;
Node *b_jump = nullptr;
int mn_jump = INF;
Node(int _val = 0, int _lvl = 0, Node* _a = nullptr, Node* _b = nullptr)
: val(_val), lvl(_lvl), a(_a), b(_b){
}
};
int n;
Node* root;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
root = new Node(0, n+1);
for(int i = 0; i < n; i++) {
int x; cin >> x;
if(x > 0) {
Node* curr = new Node(x, 0, root, root);
root = curr;
}
else {
int lvl = -x;
Node* curr = new Node(-1, lvl, root);
Node* it = root;
int jumped = 0;
while(it->b_jump != nullptr && it->mn_jump >= lvl) {
it = it->b_jump;
jumped++;
}
int cnt = 0;
while(it->lvl >= lvl) {
it = it->b;
cnt++;
}
// if(rand() % 32 == 1) {
// cerr << i << ":\n";
// cerr << "jumped: " << jumped << "\n";
// cerr << "w\\o jumping: " << cnt << "\n";
// }
curr->val = it->a->val;
curr->b = it->a;
root = curr;
}
cout << root->val << "\n";
Node* it = root;
int mn = INF;
int cnt = 0;
for(; it != nullptr && cnt < JUMP_REQ_LEN; cnt++) {
mn = min(mn, it->lvl);
it = it->b;
}
if(cnt == JUMP_REQ_LEN) {
root->b_jump = it;
root->mn_jump = mn;
}
}
return 0;
}
/*
7
2
5
-1
-3
-2
-4
-4
*/
# | 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... |