-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaint.cpp
135 lines (97 loc) · 2.05 KB
/
aint.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <vector>
#include <random>
#include <time>
using namespace std;
struct Aint
{
vector <int> aint;
int n;
Aint(int n) : n(n), aint(2 * n + 1) { }
int Query(int poz)
{
int ans = -1e9;
poz += n;
while (poz) {
ans = max(ans, aint[poz]);
poz /= 2;
}
return ans;
}
// [st, dr]
void Update(int st, int dr, int val)
{
st += n, dr += n + 1;
while (st != dr) {
if (st & 1)
aint[st] = max(aint[st], val), st++;
if (!(dr & 1))
dr--, aint[dr] = max(aint[dr], val);
st /= 2, dr /= 2;
}
}
};
mt19937 rnd(time(0));
typedef struct Treap * Arbore;
Arbore NIL;
typedef pair <Arbore, Arbore> Paa;
struct Treap
{
// val -> valoarea
// prio -> prioritatea, aleasa random
// g -> greutate
int val, prio, g;
// Fiul stang si drept
Arbore st, dr;
Treap(int val) :
val(val), prio(rnd()), g(1), st(NILL), dr(NILL) { }
void RecalcDP() {
g = 1 + st->g + dr->g;
}
};
// Returneaza a unit cu b
// Atentie: Distruge pointerii a si b
Arbore Join(Arbore a, Arbore b)
{
if (a == NIL)
return b;
if (b == NIL)
return a;
if (a->prio > b->prio) {
a->dr = Join(a->dr, b);
a->RecalcDP();
return a;
}
// else
b->st = Join(a, b->st);
b->RecalcDP();
return b;
}
// (<= val, > val)
Paa SplitByValue(Arbore a, int val)
{
if (a == NILL)
return { NIL, NIL };
if (a->val <= val) {
Paa s = SplitByValue(a->dr, val);
a->dr = s.first;
a->RecalcDP();
return { a, s.second };
}
// else
Paa s = SplitByValue(a->st, val);
a->st = s.second;
a->RecalcDP();
return { s.first, a };
}
Arbore Insert(Arbore a, int val)
{
Arbore new_nod = new Treap(val);
Paa s = split(a, val);
return Join(Join(s.first, new_nod), s.second);
}
int main()
{
NIL = new Treap(0);
NIL->g = 0;
}