Skip to content

Commit 63ef25b

Browse files
committed
G: Persistent priority queue
1 parent 8511703 commit 63ef25b

File tree

1 file changed

+94
-0
lines changed
  • Misc/Tsinghua University Bootcamp in Summer 2024/Tsinghua Bootcamp 2024. Day 2

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @file G.cpp
3+
* @author Macesuted (i@macesuted.moe)
4+
* @date 2024-08-28
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
#define maxn 200005
20+
21+
class LeftistTree {
22+
private:
23+
struct Node {
24+
Node *l, *r;
25+
int dist, val;
26+
Node(int v) { l = r = nullptr, dist = 0, val = v; }
27+
};
28+
29+
Node *root;
30+
31+
int getDist(Node *p) { return p ? p->dist : -1; }
32+
void merge(Node *&p, Node *t1, Node *t2) {
33+
if (!t1) return p = t2, void();
34+
if (!t2) return p = t1, void();
35+
if (t1->val > t2->val) swap(t1, t2);
36+
p = new Node(*t1), merge(p->r, t1->r, t2);
37+
if (getDist(p->l) < getDist(p->r)) swap(p->l, p->r);
38+
p->dist = getDist(p->r) + 1;
39+
return;
40+
}
41+
42+
public:
43+
LeftistTree(void) { root = nullptr; }
44+
45+
void push(int v) { return merge(root, root, new Node(v)); }
46+
void pop(void) {
47+
Node *p = root->l, *q = root->r;
48+
return merge(root, p, q);
49+
}
50+
void merge(LeftistTree *t) { return merge(root, root, t->root); }
51+
int top(void) { return root ? root->val : -1; }
52+
};
53+
54+
LeftistTree *LFT[maxn];
55+
56+
void solve(void) {
57+
int n, lst = 0;
58+
cin >> n;
59+
LFT[0] = new LeftistTree();
60+
for (int i = 1; i <= n; i++) {
61+
int op, a, b;
62+
cin >> op >> a, LFT[i] = new LeftistTree(*LFT[(a + lst) % i]);
63+
if (op == 1)
64+
cin >> b, LFT[i]->push((b + 17 * lst) % int(1e9 + 1));
65+
else if (op == 2)
66+
cin >> b, LFT[i]->merge(LFT[(b + 13 * lst) % i]);
67+
68+
int ans = LFT[i]->top();
69+
if (ans == -1)
70+
cout << "empty" << endl;
71+
else {
72+
cout << ans << endl, lst = (lst + ans) % 239017;
73+
if (op == 3) LFT[i]->pop();
74+
}
75+
}
76+
return;
77+
}
78+
79+
bool mem2;
80+
81+
int main() {
82+
ios::sync_with_stdio(false), cin.tie(nullptr);
83+
#ifdef LOCAL
84+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
85+
#endif
86+
87+
int _ = 1;
88+
while (_--) solve();
89+
90+
#ifdef LOCAL
91+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
92+
#endif
93+
return 0;
94+
}

0 commit comments

Comments
 (0)