0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-14 04:18:48 +00:00

P1486 [NOI2004] 郁闷的出纳员

R70811692
This commit is contained in:
Baoshuo Ren 2022-03-07 15:25:42 +08:00
parent 26945cdc2e
commit 65b58c43b1
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -1,14 +1,90 @@
#include <algorithm> #include <cstdlib>
#include <iostream> #include <iostream>
#include <vector>
using std::cin; using std::cin;
using std::cout; using std::cout;
const char endl = '\n'; const char endl = '\n';
int n, min, k, c, cnt; const int N = 1e5;
int n, min, k, c, ans;
char op; char op;
std::vector<int> a;
// Treap
int root, cnt;
struct node {
int l, r, s, v, k;
node()
: l(0), r(0), s(0), v(0), k(rand()) {}
node(int _v)
: l(0), r(0), s(1), v(_v), k(rand()) {}
} tr[N];
void pushup(int u) {
tr[u].s = tr[tr[u].l].s + 1 + tr[tr[u].r].s;
}
std::pair<int, int> split(int p, int k) {
if (!p) return std::make_pair(0, 0);
if (k <= tr[tr[p].l].s) {
auto o = split(tr[p].l, k);
tr[p].l = o.second;
pushup(p);
o.second = p;
return o;
}
auto o = split(tr[p].r, k - tr[tr[p].l].s - 1);
tr[p].r = o.first;
pushup(p);
o.first = p;
return o;
}
std::pair<int, int> splitByValue(int p, int v) {
if (!p) return std::make_pair(0, 0);
if (v <= tr[p].v) {
auto o = splitByValue(tr[p].l, v);
tr[p].l = o.second;
pushup(p);
o.second = p;
return o;
}
auto o = splitByValue(tr[p].r, v);
tr[p].r = o.first;
pushup(p);
o.first = p;
return o;
}
int merge(int x, int y) {
if (!x || !y) return x | y;
if (tr[x].k > tr[y].k) {
tr[x].r = merge(tr[x].r, y);
pushup(x);
return x;
}
tr[y].l = merge(x, tr[y].l);
pushup(y);
return y;
}
void insert(int v) {
auto o = splitByValue(root, v);
int p = ++cnt;
tr[p] = node(v);
root = merge(o.first, merge(p, o.second));
}
int getKth(int k) {
auto x = split(root, k - 1);
auto y = split(x.second, 1);
int r = y.first;
root = merge(x.first, merge(y.first, y.second));
return tr[r].v;
}
int main() { int main() {
std::ios::sync_with_stdio(false); std::ios::sync_with_stdio(false);
@ -17,19 +93,19 @@ int main() {
cin >> op >> k; cin >> op >> k;
if (op == 'I') { if (op == 'I') {
if (k >= min) { if (k >= min) {
a.insert(std::lower_bound(a.begin(), a.end(), k - c), k - c); insert(k - c);
} }
} else if (op == 'A') { } else if (op == 'A') {
c += k; c += k;
} else if (op == 'S') { } else if (op == 'S') {
c -= k; c -= k;
auto it = std::lower_bound(a.begin(), a.end(), min - c); auto o = splitByValue(root, min - c);
cnt += it - a.begin(); root = o.second;
a.erase(a.begin(), std::lower_bound(a.begin(), a.end(), min - c)); ans += tr[o.first].s;
} else { // op == 'F' } else { // op == 'F'
cout << (a.size() < k ? -1 : *(a.end() - k) + c) << endl; cout << (tr[root].s < k ? -1 : getKth(tr[root].s - k + 1) + c) << endl;
} }
} }
cout << cnt << endl; cout << ans << endl;
return 0; return 0;
} }