#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct Node
{
int key, heap, tot;
Node *left, *right;
} *root;
int getT(Node *root)
{
if (root == NULL)
return 0;
return root->tot;
}
void Update(Node *root)
{
if (root == NULL)
return;
root->tot = getT(root->left) + getT(root->right) + 1;
}
Node *Merge(Node *p, Node *q)
{
if (p == NULL)
return q;
if (q == NULL)
return p;
if (p->heap < q->heap)
{
p->right = Merge(p->right, q);
Update(p);
return p;
}
else
{
q->left = Merge(p, q->left);
Update(q);
return q;
}
}
void Delete(Node *&root)
{
root = Merge(root->left, root->right);
Update(root);
}
void find1(Node *&root, int k)
{
if (root == NULL)
return;
if (k == root->key)
{
Delete(root);
return;
}
if (k < root->key)
find1(root->left, k);
else
find1(root->right, k);
Update(root);
}
int find2(Node *root, int k)
{
if (root == NULL)
return -1;
int left = getT(root->left);
if (left + 1 == k)
return root->key;
if (left >= k)
return find2(root->left, k);
else
return find2(root->right, k - left - 1);
}
int find3(Node *root, int k)
{
if (root == NULL)
return -1;
if (k <= root->key)
return find3(root->left, k);
else
return max(find3(root->right, k), root->key);
}
void Insert(Node *&root, int k)
{
if (root == NULL)
{
root = new Node;
root->key = k;
root->heap = rand();
root->tot = 1;
root->left = root->right = NULL;
return;
}
root->tot++;
if (k < root->key)
{
Insert(root->left, k);
if (root->left->heap < root->heap)
{
Node *tmp = root->left;
root->left = tmp->right;
tmp->right = root;
Update(root);
Update(tmp);
root = tmp;
}
}
else
{
Insert(root->right, k);
if (root->right->heap < root->heap)
{
Node *tmp = root->right;
root->right = tmp->left;
tmp->left = root;
Update(root);
Update(tmp);
root = tmp;
}
}
}
int main(int argc, const char *argv[])
{
return 0;
}
温馨提示:内容为网友见解,仅供参考