144 lines
3.2 KiB
C++
144 lines
3.2 KiB
C++
#include <bits/stdc++.h>
|
|
|
|
class LinkList {
|
|
struct Node {
|
|
int val;
|
|
Node *next;
|
|
Node() : val(0), next(nullptr) {}
|
|
Node(int __val, Node *__next) : val(__val), next(__next) {}
|
|
};
|
|
int count = 0;
|
|
Node *head = nullptr;
|
|
|
|
public:
|
|
LinkList() { ; }
|
|
~LinkList() {
|
|
Node *p = head;
|
|
while (p != nullptr) {
|
|
Node *next_p = p->next;
|
|
delete p;
|
|
p = next_p;
|
|
}
|
|
}
|
|
void push(int new_val) {
|
|
if (count == 0) {
|
|
head = new Node(new_val, nullptr);
|
|
count = 1;
|
|
return;
|
|
}
|
|
Node *cur = head, *last_cur = nullptr;
|
|
while (cur != nullptr) {
|
|
if (cur->val >= new_val) {
|
|
if (last_cur != nullptr) {
|
|
last_cur->next = new Node(new_val, cur);
|
|
} else {
|
|
head = new Node(new_val, cur);
|
|
}
|
|
count++;
|
|
return;
|
|
}
|
|
last_cur = cur;
|
|
cur = cur->next;
|
|
}
|
|
// std::cerr << last_cur << std::endl;
|
|
last_cur->next = new Node(new_val, cur);
|
|
count++;
|
|
}
|
|
int getKth(int idx) {
|
|
if (idx >= count) return -1;
|
|
Node *cur = head;
|
|
for (int i = 0; i < idx && cur != nullptr; i++) cur = cur->next;
|
|
return cur->val;
|
|
}
|
|
void merge(LinkList *oth) {
|
|
count += oth->count;
|
|
Node *cur_this = head, *cur_oth = oth->head;
|
|
head = nullptr;
|
|
Node *head_store = nullptr;
|
|
while (cur_this != nullptr && cur_oth != nullptr) {
|
|
if (cur_this->val <= cur_oth->val) {
|
|
if (head == nullptr) {
|
|
head_store = head = cur_this;
|
|
} else {
|
|
head->next = cur_this;
|
|
head = head->next;
|
|
}
|
|
cur_this = cur_this->next;
|
|
} else {
|
|
if (head == nullptr) {
|
|
head_store = head = cur_oth;
|
|
} else {
|
|
head->next = cur_oth;
|
|
head = head->next;
|
|
}
|
|
cur_oth = cur_oth->next;
|
|
}
|
|
}
|
|
while (cur_this != nullptr) {
|
|
if (head == nullptr) {
|
|
head_store = head = cur_this;
|
|
} else {
|
|
head->next = cur_this;
|
|
head = head->next;
|
|
}
|
|
cur_this = cur_this->next;
|
|
}
|
|
while (cur_oth != nullptr) {
|
|
if (head == nullptr) {
|
|
head_store = head = cur_oth;
|
|
} else {
|
|
head->next = cur_oth;
|
|
head = head->next;
|
|
}
|
|
cur_oth = cur_oth->next;
|
|
}
|
|
if (head != nullptr) head->next = nullptr;
|
|
head = head_store;
|
|
oth->count = 0;
|
|
oth->head = nullptr;
|
|
}
|
|
};
|
|
|
|
class LinkListArray {
|
|
private:
|
|
int len;
|
|
LinkList **lists;
|
|
|
|
public:
|
|
LinkListArray(int n) : len(n) {
|
|
lists = new LinkList *[n];
|
|
for (int i = 0; i < n; ++i) lists[i] = new LinkList;
|
|
}
|
|
~LinkListArray() {
|
|
for (int i = 0; i < len; ++i) {
|
|
delete lists[i];
|
|
}
|
|
delete[] lists;
|
|
}
|
|
|
|
void insertNumber(int n, int x) { lists[n]->push(x); }
|
|
int queryNumber(int n, int k) { return lists[n]->getKth(k); }
|
|
void mergeLists(int p, int q) { lists[p]->merge(lists[q]); }
|
|
};
|
|
|
|
int main() {
|
|
#ifdef local
|
|
freopen("pro.in", "r", stdin);
|
|
#endif
|
|
int len, m;
|
|
scanf("%d%d", &len, &m);
|
|
LinkListArray a = LinkListArray(len);
|
|
for (int i = 0, op, s1, s2; i < m; ++i) {
|
|
scanf("%d%d%d", &op, &s1, &s2);
|
|
if (op == 0) {
|
|
a.insertNumber(s1, s2);
|
|
}
|
|
if (op == 1) {
|
|
printf("%d\n", a.queryNumber(s1, s2));
|
|
}
|
|
if (op == 2) {
|
|
a.mergeLists(s1, s2);
|
|
}
|
|
}
|
|
return 0;
|
|
} |