-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPersistentQueue.cpp
More file actions
77 lines (64 loc) · 1.61 KB
/
PersistentQueue.cpp
File metadata and controls
77 lines (64 loc) · 1.61 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
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int LOG = 19;
class PersistentQueue {
struct Node {
int val;
vector<int> up;
Node() {
up.resize(LOG);
}
};
struct Version {
int lastNode, size;
Version(int lastNode, int size): lastNode(lastNode), size(size) {}
};
vector<Node> nodes;
vector<Version> versions;
public:
PersistentQueue() {
nodes.push_back(Node());
versions.push_back(Version(0, 0));
}
int pop(int v) {
versions.push_back(Version(versions[v].lastNode, versions[v].size - 1));
int node = versions[v].lastNode;
int k = versions[v].size - 1;
for (int i = LOG - 1; i >= 0; i--) {
if (k & (1 << i)) {
node = nodes[node].up[i];
}
}
return nodes[node].val;
}
void push(int v, int x) {
versions.push_back(Version(nodes.size(), versions[v].size + 1));
nodes.push_back(Node());
auto &node = nodes.back();
node.val = x;
node.up[0] = versions[v].lastNode;
for (int i = 1; i < LOG; i++) {
node.up[i] = nodes[node.up[i - 1]].up[i - 1];
}
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> q;
PersistentQueue persistentQueue;
while (q--) {
int op, v;
cin >> op >> v;
v++;
if (op == 0) {
int x;
cin >> x;
persistentQueue.push(v, x);
} else {
cout << persistentQueue.pop(v) << '\n';
}
}
}