Mai intai trebuie sa te autentifici.
Cod sursa(job #2507129)
Utilizator | Data | 9 decembrie 2019 17:57:25 | |
---|---|---|---|
Problema | Zeap | Scor | 40 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva de probleme | Marime | 3.51 kb |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
ifstream fin ("zeap.in");
ofstream fout ("zeap.out");
struct Treap {
int value;
long long priority;
Treap* left;
Treap* right;
int mx;
int mn;
int dif;
}*root;
Treap* emptyTreap = new Treap {0, 0, NULL, NULL, -INF, INF, INF};
Treap* jonule (Treap* root) {
if (root == emptyTreap)
return emptyTreap;
root -> mn = min(root->value, min(root->left->mn, root->right->mn));
root -> mx = max(root->value, max(root->left->mx, root->right->mx));
int t1, t2;
t1 = t2 = INF;
if (root ->left != emptyTreap)
t1 = root->value - root->left->mx;
if (root->right != emptyTreap)
t2 = root->right->mn - root->value;
root->dif = min(min(t1, t2), min(root->left->dif, root->right->dif));
return root;
}
pair<Treap*, Treap*> split (Treap* root, int x) {
if (root == emptyTreap)
return {emptyTreap, emptyTreap};
if (root->value < x) {
auto p = split(root->right, x);
Treap* hatz = new Treap {root->value, root->priority, root->left, p.first, 0, 0, 0};
hatz = jonule(hatz);
p.second = jonule(p.second);
return {hatz, p.second};
}
if (root->value >= x) {
auto p = split(root->left, x);
Treap* hatz = new Treap {root->value, root->priority, p.second, root->right, 0, 0, 0};
hatz = jonule(hatz);
p.first = jonule(p.first);
return {p.first, hatz};
}
}
Treap* merge (Treap *first, Treap* second) {
if (first == emptyTreap)
return second;
if (second == emptyTreap)
return first;
if (second ->priority <= first->priority) {
auto p = merge(first -> right, second);
Treap * hatz = new Treap {first->value,first->priority, first->left, p, 0, 0, 0};
hatz = jonule(hatz);
return hatz;
}
else if (second ->priority > first->priority) {
auto p =merge(first, second->left);
Treap* hatz = new Treap {second->value, second->priority, p, second->right, 0, 0, 0};
hatz = jonule(hatz);
return hatz;
}
}
int sz = 0;
Treap* insert(Treap* root, int value) {
sz++;
Treap* newV = new Treap {
value,
((long long)rand() << 31) | rand(),
emptyTreap,
emptyTreap,
value,
value,
INF,
};
auto p = split(root, value);
Treap* t = merge(merge(p.first, newV) ,p.second);
return t;
}
Treap* erase (Treap* root, int value) {
sz--;
auto p1 = split(root, value);
auto p2 = split(p1.second, value + 1);
return merge(p1.first, p2.second);
}
bool find (Treap* root, int value) {
if(root == emptyTreap)
return 0;
if(value == root->value)
return 1;
if(value < root->value)
return find(root->left, value);
else
return find(root->right, value);
}
int n, q, v[100002], x, t;
int main()
{
root = emptyTreap;
while (1) {
string s;
fin >> s;
if (!s.size())
break;
if (s == "I") {
fin >> x;
if (!find(root, x))
root = insert(root, x);
}
else if (s == "S") {
fin >>x;
if (!find(root, x))
fout << "-1\n";
else
root = erase(root, x);
}
else if (s == "C") {
fin >> x;
fout << find(root, x) << "\n";
}
else if (s == "MAX") {
if (sz < 2)
fout << "-1\n";
else
fout << root -> mx - root ->mn << "\n";
}
else if (s == "MIN") {
if (sz < 2)
fout << "-1\n";
else
fout << root->dif << "\n";
}
else
break;
}
return 0;
}