이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
class SegTree {
struct Node {
int level;
int edit;
int nb, ne;
Node *lc, *rc;
Node(int level, int edit, int nb, int ne, Node *lc, Node *rc) :
level(level), edit(edit), nb(nb), ne(ne), lc(lc), rc(rc) { }
};
vector<Node*> roots;
int idx;
Node* build(int nb, int ne) {
if (nb + 1 == ne) return new Node(INT_MAX, false, nb, ne, NULL, NULL);
Node *lc = build(nb, (nb + ne) / 2);
Node *rc = build((nb + ne) / 2, ne);
return new Node(INT_MAX, false, nb, ne, lc, rc);
}
int find_op(int level, Node *node) {
if (node->nb + 1 == node->ne) return node->nb;
if (node->rc->level < level) return find_op(level, node->rc);
return find_op(level, node->lc);
}
Node *copy(int l, int r, Node *source, Node *target) {
if (source->ne <= l || source->nb >= r) return target;
if (l <= source->nb && source->ne <= r) return source;
Node *lc = copy(l, r, source->lc, target->lc);
Node *rc = copy(l, r, source->rc, target->rc);
return new Node(min(lc->level, rc->level), rc->edit ?: lc->edit, source->nb, source->ne, lc, rc);
}
Node *update(int pos, int op, Node *node) {
if (node->ne <= pos || node->nb > pos) return node;
if (node->nb + 1 == node->ne)
return new Node(op < 0 ? -op : 0, op > 0 ? op : 0, node->nb, node->ne, NULL, NULL);
Node *lc = node->lc;
Node *rc = node->rc;
if (pos >= (node->nb + node->ne) / 2) rc = update(pos, op, node->rc);
else lc = update(pos, op, node->lc);
return new Node(min(lc->level, rc->level), rc->edit ?: lc->edit, node->nb, node->ne, lc, rc);
}
public:
int apply(int op) {
if (op > 0) {
roots.push_back(update(idx, op, roots.back()));
} else {
int undone = find_op(-op, roots.back());
Node *temp = copy(0, undone + 1, roots[undone], roots.back());
roots.push_back(update(idx, op, temp));
}
idx++;
return roots.back()->edit;
}
SegTree(int size) : idx(0) {
roots.push_back(build(0, size));
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n; cin >> n;
SegTree segtree(n);
while (n--) {
int a; cin >> a;
cout << segtree.apply(a) << '\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... |