Submission #145534

#TimeUsernameProblemLanguageResultExecution timeMemory
145534VlatkoEditor (BOI15_edi)C++14
100 / 100
570 ms207140 KiB
#include <bits/stdc++.h>
using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 300010;

struct PersTree {
    // simple persistent segment tree
    // the find query is a bit simpler than normal
    struct Node {
        Node *l, *r;
        int minval;
        Node() : l(nullptr), r(nullptr), minval(maxn) {}
        Node(int val) : l(nullptr), r(nullptr), minval(val) {}
        Node(Node *l, Node *r) : l(l), r(r), minval(maxn) {
            if (l) minval = min(minval, l->minval);
            if (r) minval = min(minval, r->minval);
        }
    };
    vector<Node*> roots;
    Node* build(int tl, int tr) {
        if (tl == tr) return new Node();
        int tm = (tl + tr) / 2;
        return new Node(build(tl, tm), build(tm+1, tr));
    }
    Node* update(Node* v, int tl, int tr, int pos, int val) {
        if (tl == tr) return new Node(val);
        int tm = (tl + tr) / 2;
        if (pos <= tm) {
            return new Node(update(v->l, tl, tm, pos, val), v->r);
        } else {
            return new Node(v->l, update(v->r, tm+1, tr, pos, val));
        }
    }
    // find max. pos with val < wanted val
    // like doing prev(lower_bound) on an array
    int find(Node* v, int tl, int tr, int val) {
        if (tl == tr) return tl;
        int tm = (tl + tr) / 2;
        if (v->r->minval < val) {
            return find(v->r, tm+1, tr, val);
        } else {
            return find(v->l, tl, tm, val);
        }
    }
} tree;

int n;
int ans[maxn];

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    ans[0] = 0;
    tree.roots.push_back(tree.build(1, n));
    for (int i = 1; i <= n; ++i) {
        int a;
        cin >> a;
        if (a > 0) {
            // state now = the state right before
            // (but also add this action)
            ans[i] = a;
            tree.roots.push_back(tree.update(tree.roots[i-1], 1, n, i, 0));
        } else {
            a = -a;
            // state now = the state BEFORE the action we are undoing
            // (but also add this action)
            int j = tree.find(tree.roots[i-1], 1, n, a) - 1;
            ans[i] = ans[j];
            tree.roots.push_back(tree.update(tree.roots[j], 1, n, i, a));
        }
        cout << ans[i] << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...