forked from spaghetti-source/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretroactive_queue.cc
More file actions
169 lines (163 loc) · 4.32 KB
/
retroactive_queue.cc
File metadata and controls
169 lines (163 loc) · 4.32 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// Fully Retroactive Queue
//
// Description:
// It maintains a list of actions ("push" and "pop").
// We can insert/erase the actions and ask status ("front") in
// any position of the list.
//
// To implement this structure, we maintain the list by a BST
// and keep track the number of pushs/pops in each position
// by using the range addition and range minimum.
//
// Complexity:
// O(n log n).
//
// References:
// E. Demaine, J. Iacono, and S. Langerman (2007):
// "Retroactive data structures". ACM Transactions of Algorithms,
// vol.3, no.2, pp.1--21.
//
#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
template <class T>
struct RetroactiveQueue {
struct Node { // Splay Tree
int type; // 0: none, 1: push, 2: pop
T value;
Node *child[2], *parent;
int a_push, d_push, a_pop, d_pop, min_remain;
} *head;
int remain(Node *x) {
return !x ? 0 : x->a_push + x->d_push - x->a_pop - x->d_pop;
}
RetroactiveQueue() : head(new Node({0})) { }
Node *update(Node *x) {
if (!x) return x;
x->min_remain = remain(x);
for (int i: {0, 1})
if (x->child[i])
x->min_remain = min(x->min_remain, x->child[i]->min_remain);
return x;
}
Node *pushdown(Node *x) {
if (!x) return x;
for (int i: {0, 1}) {
if (x->child[i]) {
x->child[i]->d_push += x->d_push;
x->child[i]->d_pop += x->d_pop;
}
}
x->a_push += x->d_push;
x->a_pop += x->d_pop;
x->d_push = x->d_pop = 0;
return x;
}
int dir(Node *x) { return x->parent && x->parent->child[1] == x; }
void link(Node *x, Node *y, int d) {
if (x) x->child[d] = y;
if (y) y->parent = x;
}
void rot(Node *x) {
int d = dir(x);
Node *p = x->parent;
pushdown(p->parent); pushdown(p); pushdown(x);
link(p->parent, x, dir(p));
link(p, x->child[!d], d);
link(x, p, !d);
update(p); update(x);
}
void splay(Node *x) {
if (!x) return;
while (x->parent) {
if (x->parent->parent) {
if (dir(x) == dir(x->parent)) rot(x->parent);
else rot(x);
}
rot(x);
}
pushdown(x);
}
Node *insert(Node *x, Node *y) {
splay(x);
y->child[0] = x;
x->parent = y;
y->child[1] = x->child[1];
x->child[1] = 0;
if (y->child[1]) {
y->child[1]->parent = y;
if (y->type == 1) y->child[1]->d_push += 1;
else if (y->type == 2) y->child[1]->d_pop += 1;
}
y->a_push = x->a_push + x->d_push + (y->type & 1);
y->a_pop = x->a_pop + x->d_pop + (y->type >> 1);
update(y->child[0]);
update(y->child[1]);
update(y);
return y;
}
Node *insert_push(Node *x, T a) { return insert(x, new Node({1, a})); }
Node *insert_pop(Node *x) { return insert(x, new Node({2})); }
Node *erase(Node *x) {
splay(x);
Node *y = x->child[1];
if (!y) {
x = x->child[0];
x->parent = 0;
return x;
}
if (x->type == 1) y->d_push -= 1;
else if (x->type == 2) y->d_pop -= 1;
y->parent = 0;
update(y);
while (y->child[0]) y = y->child[0];
splay(y);
y->child[0] = x->child[0];
if (y->child[0]) y->child[0]->parent = y;
update(y->child[0]);
update(y);
return y;
}
bool valid(Node *x) { splay(x); return x->min_remain >= 0; }
T front(Node *x) {
splay(x);
int k = x->a_pop + x->d_pop;
for (Node *y = x; y; ) {
pushdown(y);
if (y->a_push > k) {
x = y;
y = y->child[0];
} else {
y = y->child[1];
}
}
return x->value;
}
void display() {
splay(head);
display(head, 0);
}
void display(Node *x, int tab = 0) {
if (!x) return;
display(x->child[0], tab+2);
cout << string(tab, ' ') << x->type << " " << x->value << " " << x->a_push << " " << x->d_push << " " << x->a_pop << " " << x->d_pop << " " << x->min_remain << endl;
display(x->child[1], tab+2);
}
};
int main() {
RetroactiveQueue<int> que;
auto i1 = que.insert_push(que.head, 10);
auto i2 = que.insert_push(i1, 20);
auto i3 = que.insert_push(i2, 30);
auto i4 = que.insert_pop(i3);
//auto i5 = que.insert_pop(i2);
que.erase(i1);
que.display();
cout << que.front(i1) << endl;
cout << que.front(i2) << endl;
cout << que.front(i3) << endl;
cout << que.front(i4) << endl;
}