-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm_Treap.cpp
More file actions
94 lines (94 loc) · 2.06 KB
/
Algorithm_Treap.cpp
File metadata and controls
94 lines (94 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <cstdio>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
class treap
{
public:
class treapnode{
public:
treapnode *lr[2],*p;
int Priority,Value;
treapnode(treapnode* f,int val){
p=f,Priority=rand()%1000000007,Value=val,lr[0]=NULL,lr[1]=NULL;
}
int lorr(treapnode *x){return (lr[0]==x)? 0 : 1;}
};
treapnode *root;
treap(){
root=NULL;
srand(time(NULL));
}
treapnode* srch(treapnode *p,treapnode *c,int s){
if(c==NULL)return p;
else if(s==c->Value)return c;
else if(s<c->Value)return srch(c,c->lr[0],s);
else if(s>c->Value)return srch(c,c->lr[1],s);
}
void rotate_l(treapnode *x){
treapnode *y=x->lr[1];
if(root==x)root=y;
y->p=x->p;
x->lr[1]=y->lr[0];
y->lr[0]=x;
x->p=y;
if(y->p!=NULL){y->p->lr[y->p->lorr(x)]=y;}
if(x->lr[1]!=NULL){x->lr[1]->p=x;}
}
void rotate_r(treapnode *x){
treapnode *y=x->lr[0];
if(root==x)root=y;
y->p=x->p;
x->lr[0]=y->lr[1];
y->lr[1]=x;
x->p=y;
if(y->p!=NULL){y->p->lr[y->p->lorr(x)]=y;}
if(x->lr[0]!=NULL){x->lr[0]->p=x;}
}
void siftup(treapnode *c){
while(c->p!=NULL&&c->p->Priority<c->Priority){(c->p->lr[0]==c)? rotate_r(c->p):rotate_l(c->p);}
}
void insert(int s){
treapnode *tmp=srch(NULL,root,s);
if(tmp==NULL)root=new treapnode(tmp,s);
else if(tmp->Value>s)tmp->lr[0]=new treapnode(tmp,s),siftup(tmp->lr[0]);
else if(tmp->Value<s)tmp->lr[1]=new treapnode(tmp,s),siftup(tmp->lr[1]);
}
bool find(int s){
treapnode *tmp=srch(NULL,root,s);
return (tmp!=NULL&&tmp->Value==s);
}
};
treap tp;
int main()
{
int n,t,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
tp.insert(t);
}
for(int i=1;i<=m;i++)
{
scanf("%d",&t);
if(tp.find(t))
printf("y\n");
else
printf("n\n");
}
}
/*
void inorder(treapnode *c)
{
if(c==NULL)return;
inorder(c->lr[0]);
if(c->lr[0]!=NULL)printf("%d %d | ",c->lr[0]->Value,c->lr[0]->Priority);
else printf("0 0 | ");
printf("%d %d",c->Value,c->Priority);
if(c->lr[1]!=NULL)printf(" | %d %d\n",c->lr[1]->Value,c->lr[1]->Priority);
else printf(" | 0 0\n");
inorder(c->lr[1]);
}
*/