problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p02282
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int n, pos; vector<int> pre, in, post; void rec(int l, int r) { if (l >= r) return; int root = pre[pos++]; int m = distance(in.begin(), find(in.begin(), in.end(), root)); rec(1, m); rec(m + 1, r); post.push_back(root); } void solve() { pos = 0; rec(0, pre.size()); for (int i = 0; i < n; i++) { if (i) cout << " "; cout << post[i]; } cout << endl; } int main() { int k; cin >> n; for (int i = 0; i < n; i++) { cin >> k; pre.push_back(k); } for (int i = 0; i < n; i++) { cin >> k; in.push_back(k); } solve(); return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int n, pos; vector<int> pre, in, post; void rec(int l, int r) { if (l >= r) return; int root = pre[pos++]; int m = distance(in.begin(), find(in.begin(), in.end(), root)); rec(l, m); rec(m + 1, r); post.push_back(root); } void solve() { pos = 0; rec(0, pre.size()); for (int i = 0; i < n; i++) { if (i) cout << " "; cout << post[i]; } cout << endl; } int main() { int k; cin >> n; for (int i = 0; i < n; i++) { cin >> k; pre.push_back(k); } for (int i = 0; i < n; i++) { cin >> k; in.push_back(k); } solve(); return 0; }
replace
14
15
14
15
-11
p02282
C++
Runtime Error
#include <iostream> using namespace std; #include <stdio.h> #include <stdlib.h> void to_post(int *A, int *B, int *C, int h) { int x = A[0]; C[h - 1] = x; if (h < 2) return; int m = 0; while (B[m] != x) m++; if (m > 0) to_post(A + 1, B, C, m); if (h - m - 1 > 0) to_post(A + m + 1, B + m + 1, C + m, h - m - 1); } int main() { int *A, *B, *C; int i, n; scanf("%d", &n); A = new int[n]; B = new int[n]; C = new int[n]; if (A == NULL || B == NULL || C == NULL) exit(EXIT_FAILURE); for (i = 0; i < n; i++) { scanf("%d", &A[i]); C[i] = 0; } for (i = 0; i < n; i++) { scanf("%d", &B[n]); } to_post(A, B, C, n); printf("%d", C[0]); for (i = 1; i < n; i++) { printf(" %d", C[i]); } printf("\n"); return 0; }
#include <iostream> using namespace std; #include <stdio.h> #include <stdlib.h> void to_post(int *A, int *B, int *C, int h) { int x = A[0]; C[h - 1] = x; if (h < 2) return; int m = 0; while (B[m] != x) m++; if (m > 0) to_post(A + 1, B, C, m); if (h - m - 1 > 0) to_post(A + m + 1, B + m + 1, C + m, h - m - 1); } int main() { int *A, *B, *C; int i, n; scanf("%d", &n); A = new int[n]; B = new int[n]; C = new int[n]; if (A == NULL || B == NULL || C == NULL) exit(EXIT_FAILURE); for (i = 0; i < n; i++) { scanf("%d", &A[i]); C[i] = 0; } for (i = 0; i < n; i++) { scanf("%d", &B[i]); } to_post(A, B, C, n); printf("%d", C[0]); for (i = 1; i < n; i++) { printf(" %d", C[i]); } printf("\n"); return 0; }
replace
35
36
35
36
-11
p02282
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> class BinaryTree { private: static const int NIL = -1; struct Node { int parent, sibling, left, right, degree, depth, height; Node() { parent = sibling = left = right = depth = height = NIL; degree = 0; } }; std::vector<Node> T; const int N = T.size(); int root; public: BinaryTree(int N) : T(N) { root = NIL; } void addNode(int p, int l, int r) { T[p].left = l; T[p].right = r; T[l].parent = T[r].parent = p; } void buildPreIn(std::vector<int> &pre, std::vector<int> &in) { setPreIn(pre, in, 0, pre.size()); } void initTree() { setRoot(); // setDepth(root, 0); // setHeight(root); // setSibling(); } int getDegree(int v) { return T[v].degree; } int getDepth(int v) { return T[v].depth; } int getHeight(int v) { return T[v].height; } int getParent(int v) { return T[v].parent; } std::pair<int, int> getChildren(int v) { return {T[v].left, T[v].right}; } int getSibling(int v) { return T[v].sibling; } int getType(int v) { if (T[v].parent == NIL) return NIL; if (T[v].left == NIL && T[v].right == NIL) return 1; else return 0; } std::vector<int> getInOrder() { std::vector<int> S; setInOrder(S, root); return S; } std::vector<int> getPostOrder() { std::vector<int> S; setPostOrder(S, root); return S; } std::vector<int> getPreOrder() { std::vector<int> S; setPreOrder(S, root); return S; } private: void setRoot() { for (int i = 0; i < N; ++i) if (T[i].parent == NIL) root = i; } void setDepth(int v, int d) { if (v == NIL) return; T[v].depth = d; setDepth(T[v].right, d + 1); setDepth(T[v].left, d + 1); } int setHeight(int v) { int h1 = 0, h2 = 0; if (T[v].left != NIL) h1 = setHeight(T[v].left) + 1; if (T[v].right != NIL) h2 = setHeight(T[v].right) + 1; return T[v].height = std::max(h1, h2); } void setSibling() { for (int i = 0; i < N; ++i) { if (T[i].parent == NIL) continue; if (T[T[i].parent].left != i && T[T[i].parent].left != NIL) T[i].sibling = T[T[i].parent].left; else if (T[T[i].parent].right != i && T[T[i].parent].right != NIL) T[i].sibling = T[T[i].parent].right; ++T[T[i].parent].degree; } } int setPreIn(std::vector<int> &pre, std::vector<int> &in, int l, int r) { static int pos = 0; if (l >= r) return NIL; int root = pre[pos++]; int mid = std::distance(in.begin(), std::find(in.begin(), in.end(), root)); int ln = setPreIn(pre, in, l, mid); int rn = setPreIn(pre, in, mid + 1, r); addNode(root, ln, rn); return root; } void setInOrder(std::vector<int> &S, int v) { if (v == NIL) return; setInOrder(S, T[v].left); S.push_back(v); setInOrder(S, T[v].right); } void setPostOrder(std::vector<int> &S, int v) { if (v == NIL) return; setPostOrder(S, T[v].left); setPostOrder(S, T[v].right); S.push_back(v); } void setPreOrder(std::vector<int> &S, int v) { if (v == NIL) return; S.push_back(v); setPreOrder(S, T[v].left); setPreOrder(S, T[v].right); } }; #include <iostream> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> pre(n), in(n); for (int i = 0; i < n; ++i) { int v; cin >> v; pre[i] = v - 1; } for (int i = 0; i < n; ++i) { int v; cin >> v; in[i] = v - 1; } BinaryTree T(n); T.buildPreIn(pre, in); T.initTree(); std::vector<int> v = T.getPostOrder(); for (int i = 0; i < v.size(); ++i) cout << (i ? " " : "") << v[i] + 1; cout << endl; }
#include <algorithm> #include <iostream> #include <vector> class BinaryTree { private: static const int NIL = -1; struct Node { int parent, sibling, left, right, degree, depth, height; Node() { parent = sibling = left = right = depth = height = NIL; degree = 0; } }; std::vector<Node> T; const int N = T.size(); int root; public: BinaryTree(int N) : T(N) { root = NIL; } void addNode(int p, int l, int r) { T[p].left = l; T[p].right = r; if (l != NIL) T[l].parent = p; if (r != NIL) T[r].parent = p; } void buildPreIn(std::vector<int> &pre, std::vector<int> &in) { setPreIn(pre, in, 0, pre.size()); } void initTree() { setRoot(); // setDepth(root, 0); // setHeight(root); // setSibling(); } int getDegree(int v) { return T[v].degree; } int getDepth(int v) { return T[v].depth; } int getHeight(int v) { return T[v].height; } int getParent(int v) { return T[v].parent; } std::pair<int, int> getChildren(int v) { return {T[v].left, T[v].right}; } int getSibling(int v) { return T[v].sibling; } int getType(int v) { if (T[v].parent == NIL) return NIL; if (T[v].left == NIL && T[v].right == NIL) return 1; else return 0; } std::vector<int> getInOrder() { std::vector<int> S; setInOrder(S, root); return S; } std::vector<int> getPostOrder() { std::vector<int> S; setPostOrder(S, root); return S; } std::vector<int> getPreOrder() { std::vector<int> S; setPreOrder(S, root); return S; } private: void setRoot() { for (int i = 0; i < N; ++i) if (T[i].parent == NIL) root = i; } void setDepth(int v, int d) { if (v == NIL) return; T[v].depth = d; setDepth(T[v].right, d + 1); setDepth(T[v].left, d + 1); } int setHeight(int v) { int h1 = 0, h2 = 0; if (T[v].left != NIL) h1 = setHeight(T[v].left) + 1; if (T[v].right != NIL) h2 = setHeight(T[v].right) + 1; return T[v].height = std::max(h1, h2); } void setSibling() { for (int i = 0; i < N; ++i) { if (T[i].parent == NIL) continue; if (T[T[i].parent].left != i && T[T[i].parent].left != NIL) T[i].sibling = T[T[i].parent].left; else if (T[T[i].parent].right != i && T[T[i].parent].right != NIL) T[i].sibling = T[T[i].parent].right; ++T[T[i].parent].degree; } } int setPreIn(std::vector<int> &pre, std::vector<int> &in, int l, int r) { static int pos = 0; if (l >= r) return NIL; int root = pre[pos++]; int mid = std::distance(in.begin(), std::find(in.begin(), in.end(), root)); int ln = setPreIn(pre, in, l, mid); int rn = setPreIn(pre, in, mid + 1, r); addNode(root, ln, rn); return root; } void setInOrder(std::vector<int> &S, int v) { if (v == NIL) return; setInOrder(S, T[v].left); S.push_back(v); setInOrder(S, T[v].right); } void setPostOrder(std::vector<int> &S, int v) { if (v == NIL) return; setPostOrder(S, T[v].left); setPostOrder(S, T[v].right); S.push_back(v); } void setPreOrder(std::vector<int> &S, int v) { if (v == NIL) return; S.push_back(v); setPreOrder(S, T[v].left); setPreOrder(S, T[v].right); } }; #include <iostream> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> pre(n), in(n); for (int i = 0; i < n; ++i) { int v; cin >> v; pre[i] = v - 1; } for (int i = 0; i < n; ++i) { int v; cin >> v; in[i] = v - 1; } BinaryTree T(n); T.buildPreIn(pre, in); T.initTree(); std::vector<int> v = T.getPostOrder(); for (int i = 0; i < v.size(); ++i) cout << (i ? " " : "") << v[i] + 1; cout << endl; }
replace
23
24
23
27
0
p02282
C++
Runtime Error
/*! if g++ -g alds_1_7_d.cpp -o alds_1_7_d.out; then ./alds_1_7_d.out < * alds_1_7_d.test; fi */ #include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef unsigned long long ull; typedef long long ll; vector<int> pre(0); vector<int> ipre(0); vector<int> mid(0); vector<int> imid(0); class node { public: int id, left, right; node() : id(-1), left(-1), right(-1) {} }; vector<node> tree(0); int doit(int pl, int pr, int ml, int mr) { if (pl == pr) return -1; int root_id = pre[pl]; int ml1 = ml; int mr1 = imid[root_id]; int ml2 = mr1 + 1; int mr2 = mr; int pl1 = pl + 1; int pr1 = pl1 + mr1 - ml1; int pl2 = pr1; int pr2 = pr; node &nd = tree[root_id - 1]; nd.id = root_id; nd.left = doit(pl1, pr1, ml1, mr1); nd.right = doit(pl2, pr2, ml2, mr2); return root_id; } vector<int> ret(0); void post(int root) { node &nd = tree[root - 1]; if (nd.left > 0) post(nd.left); if (nd.right > 0) post(nd.right); ret.push_back(nd.id); } int main() { int n; cin >> n; pre.resize(n); mid.resize(n); ipre.resize(n); imid.resize(n); tree.resize(n); for (int i = 0; i < n; i++) { cin >> pre[i]; } for (int i = 0; i < n; i++) { cin >> mid[i]; } for (int i = 0; i < n; i++) { ipre[pre[i]] = i; imid[mid[i]] = i; } int root_id = doit(0, n, 0, n); post(root_id); for (int i = 0; i < n - 1; i++) { cout << ret[i] << " "; } cout << ret.back() << endl; return 0; }
/*! if g++ -g alds_1_7_d.cpp -o alds_1_7_d.out; then ./alds_1_7_d.out < * alds_1_7_d.test; fi */ #include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef unsigned long long ull; typedef long long ll; vector<int> pre(0); vector<int> ipre(0); vector<int> mid(0); vector<int> imid(0); class node { public: int id, left, right; node() : id(-1), left(-1), right(-1) {} }; vector<node> tree(0); int doit(int pl, int pr, int ml, int mr) { if (pl == pr) return -1; int root_id = pre[pl]; int ml1 = ml; int mr1 = imid[root_id]; int ml2 = mr1 + 1; int mr2 = mr; int pl1 = pl + 1; int pr1 = pl1 + mr1 - ml1; int pl2 = pr1; int pr2 = pr; node &nd = tree[root_id - 1]; nd.id = root_id; nd.left = doit(pl1, pr1, ml1, mr1); nd.right = doit(pl2, pr2, ml2, mr2); return root_id; } vector<int> ret(0); void post(int root) { node &nd = tree[root - 1]; if (nd.left > 0) post(nd.left); if (nd.right > 0) post(nd.right); ret.push_back(nd.id); } int main() { int n; cin >> n; pre.resize(n); mid.resize(n); ipre.resize(n + 1); imid.resize(n + 1); tree.resize(n); for (int i = 0; i < n; i++) { cin >> pre[i]; } for (int i = 0; i < n; i++) { cin >> mid[i]; } for (int i = 0; i < n; i++) { ipre[pre[i]] = i; imid[mid[i]] = i; } int root_id = doit(0, n, 0, n); post(root_id); for (int i = 0; i < n - 1; i++) { cout << ret[i] << " "; } cout << ret.back() << endl; return 0; }
replace
79
81
79
81
0
p02282
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <list> const int MAX_N = 25; const int SINGLE_NODE = 1; const int NONE = -1; struct Node { int parent; int left; int right; bool visited; }; int reconstruct(std::list<int> *preorderNodes, std::list<int> *inorderNodes, int parent, struct Node *nodes[]) { if (inorderNodes->size() == SINGLE_NODE) { int v = inorderNodes->front(); inorderNodes->pop_front(); nodes[v]->parent = parent; preorderNodes->pop_front(); return v; } int v = preorderNodes->front(); preorderNodes->pop_front(); std::list<int> *leftNodes = new std::list<int>(); std::list<int> *rightNodes = new std::list<int>(); while (!inorderNodes->empty()) { int k = inorderNodes->front(); inorderNodes->pop_front(); if (k == v) { break; } else { leftNodes->push_back(k); } } while (!inorderNodes->empty()) { int k = inorderNodes->front(); inorderNodes->pop_front(); // if (k == v) { // break; // } else { rightNodes->push_back(k); // } } // std::cout << v << std::endl; if (!leftNodes->empty()) { // std::cout << "Left: "; // for (std::list<int>::iterator i = leftNodes->begin(); i != // leftNodes->end(); ++i) { // std::cout << (*i) << " "; // } // std::cout << std::endl; nodes[v]->left = reconstruct(preorderNodes, leftNodes, v, nodes); } if (!rightNodes->empty()) { // std::cout << "Right: "; // for (std::list<int>::iterator i = rightNodes->begin(); i != // rightNodes->end(); ++i) { // std::cout << (*i) << " "; // } // std::cout << std::endl; nodes[v]->right = reconstruct(preorderNodes, rightNodes, v, nodes); } nodes[v]->parent = parent; return v; } void printByPostorder(struct Node *nodes[], int n, int rootID) { std::list<int> list; list.push_front(rootID); bool first = true; while (!list.empty()) { int id = list.front(); list.pop_front(); if (id < 0) { continue; } struct Node *node = nodes[id]; if (!node->visited) { node->visited = true; list.push_front(id); list.push_front(node->right); list.push_front(node->left); } else { if (first) { std::cout << id; first = false; } else { std::cout << " " << id; } } } std::cout << std::endl; } int main(void) { int n; std::cin >> n; struct Node *nodes[MAX_N]; for (int i = 0; i <= n; i++) { struct Node *node = (struct Node *)std::malloc(sizeof(struct Node)); node->parent = NONE; node->left = NONE; node->right = NONE; node->visited = false; nodes[i] = node; } std::list<int> *preorderNodes = new std::list<int>(); std::list<int> *inorderNodes = new std::list<int>(); for (int i = 0; i < n; i++) { int v = 0; std::cin >> v; preorderNodes->push_back(v); } for (int i = 0; i < n; i++) { int v = 0; std::cin >> v; inorderNodes->push_back(v); } int rootID = preorderNodes->front(); reconstruct(preorderNodes, inorderNodes, -1, nodes); printByPostorder(nodes, n, rootID); return 0; }
#include <cstdlib> #include <iostream> #include <list> const int MAX_N = 41; const int SINGLE_NODE = 1; const int NONE = -1; struct Node { int parent; int left; int right; bool visited; }; int reconstruct(std::list<int> *preorderNodes, std::list<int> *inorderNodes, int parent, struct Node *nodes[]) { if (inorderNodes->size() == SINGLE_NODE) { int v = inorderNodes->front(); inorderNodes->pop_front(); nodes[v]->parent = parent; preorderNodes->pop_front(); return v; } int v = preorderNodes->front(); preorderNodes->pop_front(); std::list<int> *leftNodes = new std::list<int>(); std::list<int> *rightNodes = new std::list<int>(); while (!inorderNodes->empty()) { int k = inorderNodes->front(); inorderNodes->pop_front(); if (k == v) { break; } else { leftNodes->push_back(k); } } while (!inorderNodes->empty()) { int k = inorderNodes->front(); inorderNodes->pop_front(); // if (k == v) { // break; // } else { rightNodes->push_back(k); // } } // std::cout << v << std::endl; if (!leftNodes->empty()) { // std::cout << "Left: "; // for (std::list<int>::iterator i = leftNodes->begin(); i != // leftNodes->end(); ++i) { // std::cout << (*i) << " "; // } // std::cout << std::endl; nodes[v]->left = reconstruct(preorderNodes, leftNodes, v, nodes); } if (!rightNodes->empty()) { // std::cout << "Right: "; // for (std::list<int>::iterator i = rightNodes->begin(); i != // rightNodes->end(); ++i) { // std::cout << (*i) << " "; // } // std::cout << std::endl; nodes[v]->right = reconstruct(preorderNodes, rightNodes, v, nodes); } nodes[v]->parent = parent; return v; } void printByPostorder(struct Node *nodes[], int n, int rootID) { std::list<int> list; list.push_front(rootID); bool first = true; while (!list.empty()) { int id = list.front(); list.pop_front(); if (id < 0) { continue; } struct Node *node = nodes[id]; if (!node->visited) { node->visited = true; list.push_front(id); list.push_front(node->right); list.push_front(node->left); } else { if (first) { std::cout << id; first = false; } else { std::cout << " " << id; } } } std::cout << std::endl; } int main(void) { int n; std::cin >> n; struct Node *nodes[MAX_N]; for (int i = 0; i <= n; i++) { struct Node *node = (struct Node *)std::malloc(sizeof(struct Node)); node->parent = NONE; node->left = NONE; node->right = NONE; node->visited = false; nodes[i] = node; } std::list<int> *preorderNodes = new std::list<int>(); std::list<int> *inorderNodes = new std::list<int>(); for (int i = 0; i < n; i++) { int v = 0; std::cin >> v; preorderNodes->push_back(v); } for (int i = 0; i < n; i++) { int v = 0; std::cin >> v; inorderNodes->push_back(v); } int rootID = preorderNodes->front(); reconstruct(preorderNodes, inorderNodes, -1, nodes); printByPostorder(nodes, n, rootID); return 0; }
replace
4
5
4
5
0
p02282
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int n, pos; vector<int> pre, in, post; void rec(int l, int r) { if (l >= r) return; int root = pre[pos++]; int m = distance(in.begin(), find(in.begin(), in.end(), root)); rec(l, m); rec(m + l, r); post.push_back(root); } void solve() { pos = 0; rec(0, pre.size()); for (int i = 0; i < n; i++) { if (i) cout << " "; cout << post[i]; } cout << endl; } int main() { int k; cin >> n; for (int i = 0; i < n; i++) { cin >> k; pre.push_back(k); } for (int i = 0; i < n; i++) { cin >> k; in.push_back(k); } solve(); }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int n, pos; vector<int> pre, in, post; void rec(int l, int r) { if (l >= r) return; int root = pre[pos++]; int m = distance(in.begin(), find(in.begin(), in.end(), root)); rec(l, m); rec(m + 1, r); post.push_back(root); } void solve() { pos = 0; rec(0, pre.size()); for (int i = 0; i < n; i++) { if (i) cout << " "; cout << post[i]; } cout << endl; } int main() { int k; cin >> n; for (int i = 0; i < n; i++) { cin >> k; pre.push_back(k); } for (int i = 0; i < n; i++) { cin >> k; in.push_back(k); } solve(); }
replace
15
16
15
16
-11
p02282
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> #include <vector> #define _USE_MATH_DEFINES #include <cmath> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) using namespace std; typedef long long ll; const int MAXN = 25; const int NIL = -1; struct Node { int p, l, r; }; Node T[MAXN]; int P[MAXN], I[MAXN], root; int cnt = 0; void postorder(int u) { if (T[u].l != NIL) postorder(T[u].l); if (T[u].r != NIL) postorder(T[u].r); if (cnt != 0) cout << " "; cout << u; cnt++; } void makeT(int u, int n, int head, int tail) { int p_place, i_place; rep(i, n) { if (P[i] == u) p_place = i; if (I[i] == u) i_place = i; } if (head != i_place) { T[u].l = P[p_place + 1]; T[P[p_place + 1]].p = u; makeT(P[p_place + 1], n, head, i_place); } if (i_place + 1 != tail) { T[u].r = P[p_place + 1 + i_place - head]; T[P[p_place + 1 + i_place - head]].p = u; makeT(P[p_place + 1 + i_place - head], n, i_place + 1, tail); } } int main() { int n; cin >> n; rep1(i, n) T[i].p = T[i].r = T[i].l = NIL; rep(i, n) cin >> P[i]; rep(i, n) cin >> I[i]; makeT(P[0], n, 0, n); rep1(i, n) { if (T[i].p == NIL) root = i; } postorder(root); cout << endl; return (0); }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> #include <vector> #define _USE_MATH_DEFINES #include <cmath> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) using namespace std; typedef long long ll; const int MAXN = 50; const int NIL = -1; struct Node { int p, l, r; }; Node T[MAXN]; int P[MAXN], I[MAXN], root; int cnt = 0; void postorder(int u) { if (T[u].l != NIL) postorder(T[u].l); if (T[u].r != NIL) postorder(T[u].r); if (cnt != 0) cout << " "; cout << u; cnt++; } void makeT(int u, int n, int head, int tail) { int p_place, i_place; rep(i, n) { if (P[i] == u) p_place = i; if (I[i] == u) i_place = i; } if (head != i_place) { T[u].l = P[p_place + 1]; T[P[p_place + 1]].p = u; makeT(P[p_place + 1], n, head, i_place); } if (i_place + 1 != tail) { T[u].r = P[p_place + 1 + i_place - head]; T[P[p_place + 1 + i_place - head]].p = u; makeT(P[p_place + 1 + i_place - head], n, i_place + 1, tail); } } int main() { int n; cin >> n; rep1(i, n) T[i].p = T[i].r = T[i].l = NIL; rep(i, n) cin >> P[i]; rep(i, n) cin >> I[i]; makeT(P[0], n, 0, n); rep1(i, n) { if (T[i].p == NIL) root = i; } postorder(root); cout << endl; return (0); }
replace
16
17
16
17
0
p02282
C++
Runtime Error
#include <algorithm> #include <array> #include <cstdio> #include <iostream> #include <list> #include <numeric> #include <queue> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define _USE_MATH_DEFINES #include <map> #include <math.h> #define SENTINEL 1000000001 #define min(a, b) (a) > (b) ? (b) : (a) #define max(a, b) (a) > (b) ? (a) : (b) using namespace std; enum struct Type { ROOT, INTERNAL, LEAF }; struct OwnNode { int id; int parent; int depth; int height; int sibling; Type type; int left; int right; }; OwnNode tree[100001]; bool isChild[100001]; const string typeStr[] = {"root", "internal node", "leaf"}; int TrackTree(int parentid, int id, int depth) { /*int height = 0; tree[id].parent = parentid; tree[id].depth = depth; for (int i = 0; i != tree[id].children.size(); i++) { height = max(height, TrackTree(id, tree[id].children[i], depth + 1)); } if (tree[id].children.size() == 2) { tree[tree[id].children[0]].sibling = tree[tree[id].children[1]].id; tree[tree[id].children[1]].sibling = tree[tree[id].children[0]].id; } tree[id].height = height; return height + 1;*/ return 0; } void PreOrder(int id) { printf(" %d", id); if (tree[id].left != -1) { PreOrder(tree[id].left); } if (tree[id].right != -1) { PreOrder(tree[id].right); } } void InOrder(int id) { if (tree[id].left != -1) { InOrder(tree[id].left); } printf(" %d", id); if (tree[id].right != -1) { InOrder(tree[id].right); } } void PostOrder(int id, int cnt) { if (tree[id].left != -1) { PostOrder(tree[id].left, 1); } if (tree[id].right != -1) { PostOrder(tree[id].right, 1); } if (cnt == 0) { printf("%d", id); } else { printf("%d ", id); } } int Reconstruct(int a[], int b[], int n) { if (n == 1) { tree[a[0]].left = -1; tree[a[0]].right = -1; return a[0]; } int tempi = -1; for (int i = 0; i < n; i++) { if (b[i] == a[0]) { tempi = i; break; } } int *a1 = new int[tempi]; int *a2 = new int[n - tempi - 1]; int *b1 = new int[tempi]; int *b2 = new int[n - tempi - 1]; for (int i = 0; i < tempi; i++) { a1[i] = a[i + 1]; b1[i] = b[i]; } for (int i = 0; i < (n - tempi - 1); i++) { a2[i] = a[tempi + 1 + i]; b2[i] = b[tempi + 1 + i]; } int left, right; if (tempi != 0) { left = Reconstruct(a1, b1, tempi); isChild[left] = true; // printf("left:%d\n", left); } else { left = -1; } if ((n - tempi - 1) != 0) { right = Reconstruct(a2, b2, n - tempi - 1); isChild[right] = true; // printf("right:%d\n", right); } else { right = -1; } tree[b[tempi]].left = left; tree[b[tempi]].right = right; delete[] a1; delete[] a2; delete[] b1; delete[] b2; return b[tempi]; } int main() { int n; int a[50], b[50]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } /*for (int i = 0; i < n; i++) { int id,left,right; scanf("%d %d %d", &id, &left, &right); tree[id].id = id; tree[id].depth = 0; tree[id].sibling = -1; tree[id].parent = -1; tree[id].left = left; tree[id].right = right; if (left != -1) { isChild[left] = true; } if (right != -1) { isChild[right] = true; } }*/ Reconstruct(a, b, n); int rootID = -1; for (int i = 1; i < n; i++) { if (!isChild[i]) { rootID = i; break; } } /*TrackTree(-1, rootID, 0); for (int i = 0; i < n; i++) { if (tree[i].parent == -1) { tree[i].type = Type::ROOT; } else if (tree[i].children.size() == 0) { tree[i].type = Type::LEAF; } else { tree[i].type = Type::INTERNAL; } } for (int i = 0; i < n; i++) { printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s\n", tree[i].id, tree[i].parent, tree[i].sibling,tree[i].children.size(),tree[i].depth, tree[i].height,typeStr[(int)tree[i].type].c_str()); }*/ /*printf("Preorder\n"); PreOrder(rootID); printf("\nInorder\n"); InOrder(rootID); printf("\nPostorder\n");*/ PostOrder(rootID, 0); printf("\n"); return 0; }
#include <algorithm> #include <array> #include <cstdio> #include <iostream> #include <list> #include <numeric> #include <queue> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define _USE_MATH_DEFINES #include <map> #include <math.h> #define SENTINEL 1000000001 #define min(a, b) (a) > (b) ? (b) : (a) #define max(a, b) (a) > (b) ? (a) : (b) using namespace std; enum struct Type { ROOT, INTERNAL, LEAF }; struct OwnNode { int id; int parent; int depth; int height; int sibling; Type type; int left; int right; }; OwnNode tree[100001]; bool isChild[100001]; const string typeStr[] = {"root", "internal node", "leaf"}; int TrackTree(int parentid, int id, int depth) { /*int height = 0; tree[id].parent = parentid; tree[id].depth = depth; for (int i = 0; i != tree[id].children.size(); i++) { height = max(height, TrackTree(id, tree[id].children[i], depth + 1)); } if (tree[id].children.size() == 2) { tree[tree[id].children[0]].sibling = tree[tree[id].children[1]].id; tree[tree[id].children[1]].sibling = tree[tree[id].children[0]].id; } tree[id].height = height; return height + 1;*/ return 0; } void PreOrder(int id) { printf(" %d", id); if (tree[id].left != -1) { PreOrder(tree[id].left); } if (tree[id].right != -1) { PreOrder(tree[id].right); } } void InOrder(int id) { if (tree[id].left != -1) { InOrder(tree[id].left); } printf(" %d", id); if (tree[id].right != -1) { InOrder(tree[id].right); } } void PostOrder(int id, int cnt) { if (tree[id].left != -1) { PostOrder(tree[id].left, 1); } if (tree[id].right != -1) { PostOrder(tree[id].right, 1); } if (cnt == 0) { printf("%d", id); } else { printf("%d ", id); } } int Reconstruct(int a[], int b[], int n) { if (n == 1) { tree[a[0]].left = -1; tree[a[0]].right = -1; return a[0]; } int tempi = -1; for (int i = 0; i < n; i++) { if (b[i] == a[0]) { tempi = i; break; } } int *a1 = new int[tempi]; int *a2 = new int[n - tempi - 1]; int *b1 = new int[tempi]; int *b2 = new int[n - tempi - 1]; for (int i = 0; i < tempi; i++) { a1[i] = a[i + 1]; b1[i] = b[i]; } for (int i = 0; i < (n - tempi - 1); i++) { a2[i] = a[tempi + 1 + i]; b2[i] = b[tempi + 1 + i]; } int left, right; if (tempi != 0) { left = Reconstruct(a1, b1, tempi); isChild[left] = true; // printf("left:%d\n", left); } else { left = -1; } if ((n - tempi - 1) != 0) { right = Reconstruct(a2, b2, n - tempi - 1); isChild[right] = true; // printf("right:%d\n", right); } else { right = -1; } tree[b[tempi]].left = left; tree[b[tempi]].right = right; delete[] a1; delete[] a2; delete[] b1; delete[] b2; return b[tempi]; } int main() { int n; int a[50], b[50]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } /*for (int i = 0; i < n; i++) { int id,left,right; scanf("%d %d %d", &id, &left, &right); tree[id].id = id; tree[id].depth = 0; tree[id].sibling = -1; tree[id].parent = -1; tree[id].left = left; tree[id].right = right; if (left != -1) { isChild[left] = true; } if (right != -1) { isChild[right] = true; } }*/ Reconstruct(a, b, n); int rootID = -1; for (int i = 1; i <= n; i++) { if (!isChild[i]) { rootID = i; break; } } /*TrackTree(-1, rootID, 0); for (int i = 0; i < n; i++) { if (tree[i].parent == -1) { tree[i].type = Type::ROOT; } else if (tree[i].children.size() == 0) { tree[i].type = Type::LEAF; } else { tree[i].type = Type::INTERNAL; } } for (int i = 0; i < n; i++) { printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s\n", tree[i].id, tree[i].parent, tree[i].sibling,tree[i].children.size(),tree[i].depth, tree[i].height,typeStr[(int)tree[i].type].c_str()); }*/ /*printf("Preorder\n"); PreOrder(rootID); printf("\nInorder\n"); InOrder(rootID); printf("\nPostorder\n");*/ PostOrder(rootID, 0); printf("\n"); return 0; }
replace
186
187
186
187
0
p02283
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct node { int lc; int rc; int key; void init(int a) { lc = rc = -1; key = a; } }; #define bstN 200000 struct bst { int size; node t[bstN]; void init() { size = 0; } void insert(int v) { int i = 0; while (i < size) { int &child = (v < t[i].key ? t[i].lc : t[i].rc); if (child == -1) child = size; i = child; } t[size++].init(v); } void preord(int pos, vector<int> &vec) { if (pos == -1 || pos == size) return; preord(t[pos].lc, vec); vec.push_back(t[pos].key); preord(t[pos].rc, vec); } void inord(int pos, vector<int> &vec) { if (pos == -1 || pos == size) return; vec.push_back(t[pos].key); inord(t[pos].lc, vec); inord(t[pos].rc, vec); } }; bst T; int main() { T.init(); int n, a; char str[100]; scanf("%d", &n); while (n--) { scanf("%s", str); if (str[0] == 'i') { scanf("%d", &a); T.insert(a); } else { vector<int> ans, ans2; T.preord(0, ans); for (int i : ans) cout << ' ' << i; cout << endl; T.inord(0, ans2); for (int i : ans2) cout << ' ' << i; cout << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int lc; int rc; int key; void init(int a) { lc = rc = -1; key = a; } }; #define bstN 600000 struct bst { int size; node t[bstN]; void init() { size = 0; } void insert(int v) { int i = 0; while (i < size) { int &child = (v < t[i].key ? t[i].lc : t[i].rc); if (child == -1) child = size; i = child; } t[size++].init(v); } void preord(int pos, vector<int> &vec) { if (pos == -1 || pos == size) return; preord(t[pos].lc, vec); vec.push_back(t[pos].key); preord(t[pos].rc, vec); } void inord(int pos, vector<int> &vec) { if (pos == -1 || pos == size) return; vec.push_back(t[pos].key); inord(t[pos].lc, vec); inord(t[pos].rc, vec); } }; bst T; int main() { T.init(); int n, a; char str[100]; scanf("%d", &n); while (n--) { scanf("%s", str); if (str[0] == 'i') { scanf("%d", &a); T.insert(a); } else { vector<int> ans, ans2; T.preord(0, ans); for (int i : ans) cout << ' ' << i; cout << endl; T.inord(0, ans2); for (int i : ans2) cout << ' ' << i; cout << endl; } } return 0; }
replace
13
14
13
14
0
p02283
C++
Time Limit Exceeded
#include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; class node { public: int parent, children[2], sib, val; node() { parent = -1; children[0] = -1; children[1] = -1; sib = -1; val = -1; } int deg() { int c = 0; for (int i = 0; i < 2; i++) if (children[i] != -1) c++; return c; } string typ() { if (parent == -1) return "root"; else if (deg() == 0) return "leaf"; else return "internal node"; } }; int n = 0, c = 0; node d[100000]; void pre(int i) { cout << " " << d[i].val; if (d[i].children[0] != -1) pre(d[i].children[0]); if (d[i].children[1] != -1) pre(d[i].children[1]); } void ino(int i) { if (d[i].children[0] != -1) ino(d[i].children[0]); cout << " " << d[i].val; if (d[i].children[1] != -1) ino(d[i].children[1]); } void pos(int i) { if (d[i].children[0] != -1) pos(d[i].children[0]); if (d[i].children[1] != -1) pos(d[i].children[1]); cout << " " << d[i].val; } void ins(int v) { d[c].val = v; if (c == 0) { c++; return; } int h = 0; while (1) { int g; if (d[h].val < v) { g = d[h].children[1]; if (g == -1) { d[h].children[1] = c; break; } else { h = g; } } else { g = d[h].children[0]; if (g == -1) { d[h].children[0] = c; break; } else { h = g; } } } c++; } int main(void) { cin >> n; string s; int t; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> t; ins(t); } else { ino(0); cout << endl; pre(0); cout << endl; } } }
#include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; class node { public: int parent, children[2], sib, val; node() { parent = -1; children[0] = -1; children[1] = -1; sib = -1; val = -1; } int deg() { int c = 0; for (int i = 0; i < 2; i++) if (children[i] != -1) c++; return c; } string typ() { if (parent == -1) return "root"; else if (deg() == 0) return "leaf"; else return "internal node"; } }; int n = 0, c = 0; node d[600000]; void pre(int i) { cout << " " << d[i].val; if (d[i].children[0] != -1) pre(d[i].children[0]); if (d[i].children[1] != -1) pre(d[i].children[1]); } void ino(int i) { if (d[i].children[0] != -1) ino(d[i].children[0]); cout << " " << d[i].val; if (d[i].children[1] != -1) ino(d[i].children[1]); } void pos(int i) { if (d[i].children[0] != -1) pos(d[i].children[0]); if (d[i].children[1] != -1) pos(d[i].children[1]); cout << " " << d[i].val; } void ins(int v) { d[c].val = v; if (c == 0) { c++; return; } int h = 0; while (1) { int g; if (d[h].val < v) { g = d[h].children[1]; if (g == -1) { d[h].children[1] = c; break; } else { h = g; } } else { g = d[h].children[0]; if (g == -1) { d[h].children[0] = c; break; } else { h = g; } } } c++; } int main(void) { cin >> n; string s; int t; for (int i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> t; ins(t); } else { ino(0); cout << endl; pre(0); cout << endl; } } }
replace
35
36
35
36
TLE
p02283
C++
Runtime Error
#include <cstdio> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *left; Node *right; Node() { left = right = NULL; } }; class Tree { Node *root; public: void insert(Node *n) { Node *y = NULL; Node *x = root; while (x != NULL) { y = x; if (n->key < x->key) { x = x->left; } else { x = x->right; } } if (y == NULL) { root = n; } else if (n->key < y->key) { y->left = n; } else { y->right = n; } } void print() { print_inorder(root); printf("\n"); print_preorder(root); printf("\n"); } void print_preorder(Node *n) { if (n == NULL) return; printf(" %d", n->key); print_preorder(n->left); print_preorder(n->right); } void print_inorder(Node *n) { if (n == NULL) return; print_inorder(n->left); printf(" %d", n->key); print_inorder(n->right); } }; int main() { int n; cin >> n; Tree t; for (int i = 0; i < n; i++) { string ope; cin >> ope; if (ope == "insert") { int k; cin >> k; Node *node = new Node(); node->key = k; t.insert(node); } else if (ope == "print") { t.print(); } } return 0; }
#include <cstdio> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *left; Node *right; Node() { left = right = NULL; } }; class Tree { Node *root = NULL; public: void insert(Node *n) { Node *y = NULL; Node *x = root; while (x != NULL) { y = x; if (n->key < x->key) { x = x->left; } else { x = x->right; } } if (y == NULL) { root = n; } else if (n->key < y->key) { y->left = n; } else { y->right = n; } } void print() { print_inorder(root); printf("\n"); print_preorder(root); printf("\n"); } void print_preorder(Node *n) { if (n == NULL) return; printf(" %d", n->key); print_preorder(n->left); print_preorder(n->right); } void print_inorder(Node *n) { if (n == NULL) return; print_inorder(n->left); printf(" %d", n->key); print_inorder(n->right); } }; int main() { int n; cin >> n; Tree t; for (int i = 0; i < n; i++) { string ope; cin >> ope; if (ope == "insert") { int k; cin >> k; Node *node = new Node(); node->key = k; t.insert(node); } else if (ope == "print") { t.print(); } } return 0; }
replace
14
15
14
15
-11
p02283
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct Node { int key; Node *right, *left, *parent; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int n, i, x; string com; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s", &com); if (com == "insert") { scanf("%d", &x); insert(x); } else if (com == "print") { inorder(root); printf("\n"); preorder(root); printf("\n"); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Node { int key; Node *right, *left, *parent; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int n, i, x; string com; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> com; if (com == "insert") { scanf("%d", &x); insert(x); } else if (com == "print") { inorder(root); printf("\n"); preorder(root); printf("\n"); } } return 0; }
replace
62
63
62
63
-11
p02283
C++
Memory Limit Exceeded
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; #define N 4500000 #define NIL -1 int key[N]; int parent[N]; int leftc[N]; int rightc[N]; int root = NIL; void insert(int z) { int x, y; y = NIL; x = root; while (x != NIL) { y = x; if (key[z] < key[x]) x = leftc[x]; else x = rightc[x]; } parent[z] = y; if (y == NIL) root = z; else if (key[z] < key[y]) leftc[y] = z; else rightc[y] = z; } void inorderprint(int now) { if (now != NIL) { inorderprint(leftc[now]); printf(" %d", key[now]); inorderprint(rightc[now]); } } void preorderprint(int now) { if (now != NIL) { printf(" %d", key[now]); preorderprint(leftc[now]); preorderprint(rightc[now]); } } int main() { int n, i, j, data; int z = 0; char str[10]; for (i = 0; i < N; i++) { parent[i] = NIL; key[i] = NIL; rightc[i] = NIL; leftc[i] = NIL; } scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s", str); if (strcmp(str, "insert") == 0) { scanf("%d", &data); key[z] = data; insert(z); z++; } else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; #define N 4000000 #define NIL -1 int key[N]; int parent[N]; int leftc[N]; int rightc[N]; int root = NIL; void insert(int z) { int x, y; y = NIL; x = root; while (x != NIL) { y = x; if (key[z] < key[x]) x = leftc[x]; else x = rightc[x]; } parent[z] = y; if (y == NIL) root = z; else if (key[z] < key[y]) leftc[y] = z; else rightc[y] = z; } void inorderprint(int now) { if (now != NIL) { inorderprint(leftc[now]); printf(" %d", key[now]); inorderprint(rightc[now]); } } void preorderprint(int now) { if (now != NIL) { printf(" %d", key[now]); preorderprint(leftc[now]); preorderprint(rightc[now]); } } int main() { int n, i, j, data; int z = 0; char str[10]; for (i = 0; i < N; i++) { parent[i] = NIL; key[i] = NIL; rightc[i] = NIL; leftc[i] = NIL; } scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s", str); if (strcmp(str, "insert") == 0) { scanf("%d", &data); key[z] = data; insert(z); z++; } else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
replace
6
7
6
7
MLE
p02283
C++
Time Limit Exceeded
#include <iostream> #include <stdlib.h> #include <string> #include <vector> using namespace std; class Node { public: Node *l, *r; int key; }; void insert(Node **root, int key) { Node **cur = root, *node = new Node; node->l = NULL; node->r = NULL; node->key = key; while (NULL != *cur) { if (node->key < (*cur)->key) cur = &((*cur)->l); else cur = &((*cur)->r); } *cur = node; return; } void inorder(Node *cur) { if (cur == NULL) return; inorder(cur->l); cout << " " << cur->key; inorder(cur->r); } void preorder(Node *cur) { if (cur == NULL) return; cout << " " << cur->key; preorder(cur->l); preorder(cur->r); free(cur); } int main() { Node *root = NULL; int n, key; string command; cin >> n; while (n--) { cin >> command; if ("insert" == command) { cin >> key; insert(&root, key); } if ("print" == command) { inorder(root); cout << endl; preorder(root); cout << endl; } } return 0; }
#include <iostream> #include <stdlib.h> #include <string> #include <vector> using namespace std; class Node { public: Node *l, *r; int key; }; void insert(Node **root, int key) { Node **cur = root, *node = new Node; node->l = NULL; node->r = NULL; node->key = key; while (NULL != *cur) { if (node->key < (*cur)->key) cur = &((*cur)->l); else cur = &((*cur)->r); } *cur = node; return; } void inorder(Node *cur) { if (cur == NULL) return; inorder(cur->l); cout << " " << cur->key; inorder(cur->r); } void preorder(Node *cur) { if (cur == NULL) return; cout << " " << cur->key; preorder(cur->l); preorder(cur->r); } int main() { Node *root = NULL; int n, key; string command; cin >> n; while (n--) { cin >> command; if ("insert" == command) { cin >> key; insert(&root, key); } if ("print" == command) { inorder(root); cout << endl; preorder(root); cout << endl; } } return 0; }
delete
41
42
41
41
TLE
p02283
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *right, *left, *parent; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } x->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int n, i, x; string com; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> com; if (com == "insert") { scanf("%d", &x); insert(x); } else if (com == "print") { inorder(root); printf("\n"); preorder(root); printf("\n"); } } return 0; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *right, *left, *parent; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int n, i, x; string com; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> com; if (com == "insert") { scanf("%d", &x); insert(x); } else if (com == "print") { inorder(root); printf("\n"); preorder(root); printf("\n"); } } return 0; }
replace
32
33
32
33
-11
p02283
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *right, *left, *parent; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } void inorder(Node *u) { if (u = NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int n, i, x; string com; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> com; if (com == "insert") { scanf("%d", &x); insert(x); } else if (com == "print") { inorder(root); printf("\n"); preorder(root); printf("\n"); } } return 0; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *right, *left, *parent; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int n, i, x; string com; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> com; if (com == "insert") { scanf("%d", &x); insert(x); } else if (com == "print") { inorder(root); printf("\n"); preorder(root); printf("\n"); } } return 0; }
replace
45
46
45
46
-11
p02283
C++
Runtime Error
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <string> using namespace std; #define N 100000 int key[N]; int rt[N]; int lt[N]; int parent[N]; int np = 0; int root = -1; void insert(int k) { int y = -1; int x = root; while (x != -1) { y = x; if (k < key[x]) { x = lt[x]; } else { x = rt[x]; } } if (y == -1) { root = np; } else { if (k < key[y]) { lt[y] = np; } else { rt[y] = np; } } key[np] = k; parent[np] = y; np++; } void saiki1(int x) { if (lt[x] != -1) saiki1(lt[x]); cout << " " << key[x]; if (rt[x] != -1) saiki1(rt[x]); } void saiki2(int x) { cout << " " << key[x]; if (lt[x] != -1) saiki2(lt[x]); if (rt[x] != -1) saiki2(rt[x]); } void print() { saiki1(root); cout << endl; saiki2(root); cout << endl; } int main() { for (int i = 0; i < N; i++) { key[i] = -1; parent[i] = -1; lt[i] = -1; rt[i] = -1; } int n; cin >> n; for (int i = 0; i < n; i++) { // cout << i << endl; string s; cin >> s; if (s == "print") { print(); } else if (s == "insert") { int k; cin >> k; insert(k); } } return 0; }
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <string> using namespace std; #define N 500000 int key[N]; int rt[N]; int lt[N]; int parent[N]; int np = 0; int root = -1; void insert(int k) { int y = -1; int x = root; while (x != -1) { y = x; if (k < key[x]) { x = lt[x]; } else { x = rt[x]; } } if (y == -1) { root = np; } else { if (k < key[y]) { lt[y] = np; } else { rt[y] = np; } } key[np] = k; parent[np] = y; np++; } void saiki1(int x) { if (lt[x] != -1) saiki1(lt[x]); cout << " " << key[x]; if (rt[x] != -1) saiki1(rt[x]); } void saiki2(int x) { cout << " " << key[x]; if (lt[x] != -1) saiki2(lt[x]); if (rt[x] != -1) saiki2(rt[x]); } void print() { saiki1(root); cout << endl; saiki2(root); cout << endl; } int main() { for (int i = 0; i < N; i++) { key[i] = -1; parent[i] = -1; lt[i] = -1; rt[i] = -1; } int n; cin >> n; for (int i = 0; i < n; i++) { // cout << i << endl; string s; cin >> s; if (s == "print") { print(); } else if (s == "insert") { int k; cin >> k; insert(k); } } return 0; }
replace
7
8
7
8
0
p02283
C++
Runtime Error
/* * ALDS1_8_A.cpp * * Created on: May 2, 2018 * Author: 13743 */ #include <cstdio> #include <cstdlib> struct Node { int key; Node *pa, *lc, *rc; }; Node *root = NULL; void insert(int k) { Node *y = NULL; Node *x = root; Node *z = new Node; while (x != NULL) { y = x; if (k < y->key) x = y->lc; else x = y->rc; } z->pa = y; z->lc = NULL; z->rc = NULL; z->key = k; if (y == NULL) { root = z; } else if (k < y->key) { y->lc = z; } else { y->rc = z; } } void preorder(Node *u) { if (u == NULL) return; printf(" %d", u->key); preorder(u->lc); preorder(u->rc); } void inorder(Node *u) { if (u == NULL) return; inorder(u->lc); printf(" %d", u->key); inorder(u->rc); } void print() { inorder(root); printf("\n"); preorder(root); printf("\n"); } int main() { int m; scanf("%d", &m); char *s; int ke; for (int i = 0; i < m; i++) { scanf("%s", s); if (s[0] == 'i') { scanf("%d", &ke); insert(ke); } else { print(); } } }
/* * ALDS1_8_A.cpp * * Created on: May 2, 2018 * Author: 13743 */ #include <cstdio> #include <cstdlib> struct Node { int key; Node *pa, *lc, *rc; }; Node *root = NULL; void insert(int k) { Node *y = NULL; Node *x = root; Node *z = new Node; while (x != NULL) { y = x; if (k < y->key) x = y->lc; else x = y->rc; } z->pa = y; z->lc = NULL; z->rc = NULL; z->key = k; if (y == NULL) { root = z; } else if (k < y->key) { y->lc = z; } else { y->rc = z; } } void preorder(Node *u) { if (u == NULL) return; printf(" %d", u->key); preorder(u->lc); preorder(u->rc); } void inorder(Node *u) { if (u == NULL) return; inorder(u->lc); printf(" %d", u->key); inorder(u->rc); } void print() { inorder(root); printf("\n"); preorder(root); printf("\n"); } int main() { int m; scanf("%d", &m); char s[100]; int ke; for (int i = 0; i < m; i++) { scanf("%s", s); if (s[0] == 'i') { scanf("%d", &ke); insert(ke); } else { print(); } } }
replace
67
68
67
68
-11
p02284
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <cstdio> #include <iostream> #include <list> #include <numeric> #include <queue> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define _USE_MATH_DEFINES #include <map> #include <math.h> #define SENTINEL 1000000001 #define min(a, b) (a) > (b) ? (b) : (a) #define max(a, b) (a) > (b) ? (a) : (b) using namespace std; struct OwnNode { int key; OwnNode *parent; OwnNode *left; OwnNode *right; OwnNode(int key) : key(key), parent(nullptr), left(nullptr), right(nullptr) {} }; void Insert(OwnNode *&top, OwnNode *&z) { OwnNode *y = nullptr; OwnNode *x = top; while (x != nullptr) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == nullptr) { top = z; } else if (z->key < y->key) { y->left = z; } else { y->right = z; } } bool Find(OwnNode *top, int key) { if (top == nullptr) { return false; } if (top->key == key) { return true; } else { return Find(top->left, key) || Find(top->right, key); } } void PrintPre(OwnNode *top) { printf(" %d", top->key); if (top->left != nullptr) { PrintPre(top->left); } if (top->right != nullptr) { PrintPre(top->right); } } void PrintIn(OwnNode *top) { if (top->left != nullptr) { PrintIn(top->left); } printf(" %d", top->key); if (top->right != nullptr) { PrintIn(top->right); } } void Print(OwnNode *top) { if (top == nullptr) { return; } PrintIn(top); printf("\n"); PrintPre(top); printf("\n"); } int main() { OwnNode *T; OwnNode *z; T = nullptr; int m; cin >> m; for (int i = 0; i < m; i++) { char command[10]; // int key; scanf("%s", command); // string command; // cin >> command; if (command[0] == 'i') { int key; scanf("%d", &key); z = new OwnNode(key); Insert(T, z); } else if (command[0] == 'f') { int key; cin >> key; printf(Find(T, key) ? "yes\n" : "no\n"); } else { Print(T); } } return 0; }
#include <algorithm> #include <array> #include <cstdio> #include <iostream> #include <list> #include <numeric> #include <queue> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define _USE_MATH_DEFINES #include <map> #include <math.h> #define SENTINEL 1000000001 #define min(a, b) (a) > (b) ? (b) : (a) #define max(a, b) (a) > (b) ? (a) : (b) using namespace std; struct OwnNode { int key; OwnNode *parent; OwnNode *left; OwnNode *right; OwnNode(int key) : key(key), parent(nullptr), left(nullptr), right(nullptr) {} }; void Insert(OwnNode *&top, OwnNode *&z) { OwnNode *y = nullptr; OwnNode *x = top; while (x != nullptr) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == nullptr) { top = z; } else if (z->key < y->key) { y->left = z; } else { y->right = z; } } bool Find(OwnNode *top, int key) { if (top == nullptr) { return false; } if (top->key == key) { return true; } else { if (top->key < key) { if (top->right == nullptr) { return false; } else { return Find(top->right, key); } } else { if (top->left == nullptr) { return false; } else { return Find(top->left, key); } } } } void PrintPre(OwnNode *top) { printf(" %d", top->key); if (top->left != nullptr) { PrintPre(top->left); } if (top->right != nullptr) { PrintPre(top->right); } } void PrintIn(OwnNode *top) { if (top->left != nullptr) { PrintIn(top->left); } printf(" %d", top->key); if (top->right != nullptr) { PrintIn(top->right); } } void Print(OwnNode *top) { if (top == nullptr) { return; } PrintIn(top); printf("\n"); PrintPre(top); printf("\n"); } int main() { OwnNode *T; OwnNode *z; T = nullptr; int m; cin >> m; for (int i = 0; i < m; i++) { char command[10]; // int key; scanf("%s", command); // string command; // cin >> command; if (command[0] == 'i') { int key; scanf("%d", &key); z = new OwnNode(key); Insert(T, z); } else if (command[0] == 'f') { int key; cin >> key; printf(Find(T, key) ? "yes\n" : "no\n"); } else { Print(T); } } return 0; }
replace
64
65
64
77
TLE
p02284
C++
Time Limit Exceeded
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *parent, *left, *right; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } bool find(int k, Node *u) { if (u == NIL) return false; if (u->key == k) return true; return find(k, u->left) || find(k, u->right); } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preoder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preoder(u->left); preoder(u->right); } int main() { int n; string s; cin >> n; scanf("%d", &n); for (int i = 0; i < n; i++) { // scanf("%s", s); cin >> s; if (s == "print") { inorder(root); printf("\n"); preoder(root); printf("\n"); } else if (s == "insert") { int x; scanf("%d", &x); insert(x); } else if (s == "find") { int k; scanf("%d", &k); printf(("%s", find(k, root) ? "yes" : "no")); printf("\n"); } } return 0; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *parent, *left, *right; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } bool find(int k, Node *u) { if (u == NIL) return false; if (u->key == k) return true; return (u->key > k) ? find(k, u->left) : find(k, u->right); } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preoder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preoder(u->left); preoder(u->right); } int main() { int n; string s; cin >> n; scanf("%d", &n); for (int i = 0; i < n; i++) { // scanf("%s", s); cin >> s; if (s == "print") { inorder(root); printf("\n"); preoder(root); printf("\n"); } else if (s == "insert") { int x; scanf("%d", &x); insert(x); } else if (s == "find") { int k; scanf("%d", &k); printf(("%s", find(k, root) ? "yes" : "no")); printf("\n"); } } return 0; }
replace
48
49
48
49
TLE
p02284
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <cstdlib> using namespace std; int key[500000]; int left[500000]; int right[500000]; int root = -1; int numOfNodes = 0; void printInOrder(int i) { if (i == -1) { return; } printInOrder(left[i]); printf(" %d", key[i]); printInOrder(right[i]); } void printPreOrder(int i) { if (i == -1) { return; } printf(" %d", key[i]); printPreOrder(left[i]); printPreOrder(right[i]); } void insert(int k) { if (root == -1) { root = 0; key[root] = k; left[root] = -1; right[root] = -1; ++numOfNodes; return; } int y; int x = root; while (x != -1) { y = x; if (k < key[x]) { x = left[x]; } else { x = right[x]; } } key[numOfNodes] = k; left[numOfNodes] = -1; left[numOfNodes] = -1; if (k < key[y]) { left[y] = numOfNodes++; } else { right[y] = numOfNodes++; } } void find(int k) { if (root == -1) { printf("no\n"); return; } int x = root; while (x != -1) { if (k == key[x]) { printf("yes\n"); return; } else if (k < key[x]) { x = left[x]; } else { x = right[x]; } } printf("no\n"); } int main() { int m; scanf("%d", &m); char s[19]; for (int i = 0; i < m; i++) { scanf("%s", s); if (s[0] == 'i') { scanf("%s", s); insert(atoi(s)); } else if (s[0] == 'f') { scanf("%s", s); find(atoi(s)); } else { printInOrder(root); printf("\n"); printPreOrder(root); printf("\n"); } } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <cstdlib> using namespace std; int key[500000]; int left[500000]; int right[500000]; int root = -1; int numOfNodes = 0; void printInOrder(int i) { if (i == -1) { return; } printInOrder(left[i]); printf(" %d", key[i]); printInOrder(right[i]); } void printPreOrder(int i) { if (i == -1) { return; } printf(" %d", key[i]); printPreOrder(left[i]); printPreOrder(right[i]); } void insert(int k) { if (root == -1) { root = 0; key[root] = k; left[root] = -1; right[root] = -1; ++numOfNodes; return; } int y; int x = root; while (x != -1) { y = x; if (k < key[x]) { x = left[x]; } else { x = right[x]; } } key[numOfNodes] = k; left[numOfNodes] = -1; right[numOfNodes] = -1; if (k < key[y]) { left[y] = numOfNodes++; } else { right[y] = numOfNodes++; } } void find(int k) { if (root == -1) { printf("no\n"); return; } int x = root; while (x != -1) { if (k == key[x]) { printf("yes\n"); return; } else if (k < key[x]) { x = left[x]; } else { x = right[x]; } } printf("no\n"); } int main() { int m; scanf("%d", &m); char s[19]; for (int i = 0; i < m; i++) { scanf("%s", s); if (s[0] == 'i') { scanf("%s", s); insert(atoi(s)); } else if (s[0] == 'f') { scanf("%s", s); find(atoi(s)); } else { printInOrder(root); printf("\n"); printPreOrder(root); printf("\n"); } } return 0; }
replace
53
54
53
54
TLE
p02284
C++
Runtime Error
#include <iostream> #include <memory> #include <string> #include <vector> struct node { int key; std::weak_ptr<node> parent; std::shared_ptr<node> left; std::shared_ptr<node> right; node(int k) : key(k), left(nullptr), right(nullptr){}; }; class bst { private: std::shared_ptr<node> root; void r_inorder(std::shared_ptr<node> n); void r_preorder(std::shared_ptr<node> n); public: bst() : root(nullptr){}; void insert(int v); bool find(int k); void print(); }; void bst::insert(int k) { std::shared_ptr<node> y = nullptr; std::shared_ptr<node> x = root; std::shared_ptr<node> z(new node(k)); while (x != nullptr) { y = x; if (z->key < x->key) x = x->left; else x = x->right; } z->parent = y; if (y == nullptr) root = z; else if (z->key < y->key) y->left = z; else y->right = z; } bool bst::find(int k) { std::shared_ptr<node> n = root; while (n != nullptr) { if (n->key == k) return true; if (n->key > k) n = n->left; if (n->key <= k) n = n->right; } return false; } void bst::r_inorder(std::shared_ptr<node> n) { if (n == nullptr) return; r_inorder(n->left); std::cout << " " << n->key; r_inorder(n->right); } void bst::r_preorder(std::shared_ptr<node> n) { if (n == nullptr) return; std::cout << " " << n->key; r_preorder(n->left); r_preorder(n->right); } void bst::print() { r_inorder(root); std::cout << std::endl; r_preorder(root); std::cout << std::endl; } int main() { int n; std::cin >> n; bst tree; for (int i = 0; i < n; ++i) { std::string str; std::cin >> str; int k; switch (str[0]) { case 'i': std::cin >> k; tree.insert(k); break; case 'f': std::cin >> k; std::cout << (tree.find(k) ? "yes" : "no") << std::endl; break; case 'p': tree.print(); break; default: break; } } }
#include <iostream> #include <memory> #include <string> #include <vector> struct node { int key; std::weak_ptr<node> parent; std::shared_ptr<node> left; std::shared_ptr<node> right; node(int k) : key(k), left(nullptr), right(nullptr){}; }; class bst { private: std::shared_ptr<node> root; void r_inorder(std::shared_ptr<node> n); void r_preorder(std::shared_ptr<node> n); public: bst() : root(nullptr){}; void insert(int v); bool find(int k); void print(); }; void bst::insert(int k) { std::shared_ptr<node> y = nullptr; std::shared_ptr<node> x = root; std::shared_ptr<node> z(new node(k)); while (x != nullptr) { y = x; if (z->key < x->key) x = x->left; else x = x->right; } z->parent = y; if (y == nullptr) root = z; else if (z->key < y->key) y->left = z; else y->right = z; } bool bst::find(int k) { std::shared_ptr<node> n = root; while (n != nullptr) { if (n->key == k) return true; if (n->key > k) n = n->left; else n = n->right; } return false; } void bst::r_inorder(std::shared_ptr<node> n) { if (n == nullptr) return; r_inorder(n->left); std::cout << " " << n->key; r_inorder(n->right); } void bst::r_preorder(std::shared_ptr<node> n) { if (n == nullptr) return; std::cout << " " << n->key; r_preorder(n->left); r_preorder(n->right); } void bst::print() { r_inorder(root); std::cout << std::endl; r_preorder(root); std::cout << std::endl; } int main() { int n; std::cin >> n; bst tree; for (int i = 0; i < n; ++i) { std::string str; std::cin >> str; int k; switch (str[0]) { case 'i': std::cin >> k; tree.insert(k); break; case 'f': std::cin >> k; std::cout << (tree.find(k) ? "yes" : "no") << std::endl; break; case 'p': tree.print(); break; default: break; } } }
replace
53
54
53
54
-11
p02284
C++
Time Limit Exceeded
#include <assert.h> #include <climits> #include <iostream> #include <string> struct TNode { int Value; TNode *Parent; TNode *Left; TNode *Right; TNode(); TNode(int AValue, TNode *AParent); }; TNode::TNode() { Value = INT_MAX; Parent = NULL; Left = NULL; Right = NULL; } TNode::TNode(int AValue, TNode *AParent) { Value = AValue; Parent = AParent; Left = NULL; Right = NULL; } void Insert(int Key, TNode *Root); void Delete(TNode *Root) { if (Root == NULL) return; Delete(Root->Left); Delete(Root->Right); delete Root; } void AddLeft(int Key, TNode *Root) { assert(Root != NULL); if (Root->Left == NULL) { TNode *Left = new TNode(Key, Root); Root->Left = Left; } else { Insert(Key, Root->Left); } } void AddRight(int Key, TNode *Root) { assert(Root != NULL); if (Root->Right == NULL) { TNode *Right = new TNode(Key, Root); Root->Right = Right; } else { Insert(Key, Root->Right); } } void Insert(int Key, TNode *Root) { assert(Root != NULL); if (Root->Value == Key) return; if (Root->Value == INT_MAX) { Root->Value = Key; return; } if (Root->Value > Key) { AddLeft(Key, Root); } else { AddRight(Key, Root); } } bool Find(int Key, TNode *Node) { if (Node == NULL) return false; if (Node->Value == Key) return true; return Find(Key, Node->Left) || Find(Key, Node->Right); } void OutputPreOrder(const TNode *Root) { if (Root == NULL) return; std::cout << " " << Root->Value; OutputPreOrder(Root->Left); OutputPreOrder(Root->Right); } void OutputInorder(const TNode *Root) { if (Root == NULL) return; OutputInorder(Root->Left); std::cout << " " << Root->Value; OutputInorder(Root->Right); } void Print(const TNode *Node) { OutputInorder(Node); std::cout << std::endl; OutputPreOrder(Node); std::cout << std::endl; } void Execute(const std::string &Command, TNode *Root) { if (Command == "insert") { int Key = 0; std::cin >> Key; Insert(Key, Root); return; } if (Command == "find") { int Key = 0; std::cin >> Key; if (Find(Key, Root)) { std::cout << "yes" << std::endl; return; } std::cout << "no" << std::endl; } if (Command == "print") { Print(Root); } } int main() { int Count = 0; std::cin >> Count; TNode *Root = new TNode(); for (int i = 0; i < Count; ++i) { std::string Command; std::cin >> Command; Execute(Command, Root); } Delete(Root); return 0; }
#include <assert.h> #include <climits> #include <iostream> #include <string> struct TNode { int Value; TNode *Parent; TNode *Left; TNode *Right; TNode(); TNode(int AValue, TNode *AParent); }; TNode::TNode() { Value = INT_MAX; Parent = NULL; Left = NULL; Right = NULL; } TNode::TNode(int AValue, TNode *AParent) { Value = AValue; Parent = AParent; Left = NULL; Right = NULL; } void Insert(int Key, TNode *Root); void Delete(TNode *Root) { if (Root == NULL) return; Delete(Root->Left); Delete(Root->Right); delete Root; } void AddLeft(int Key, TNode *Root) { assert(Root != NULL); if (Root->Left == NULL) { TNode *Left = new TNode(Key, Root); Root->Left = Left; } else { Insert(Key, Root->Left); } } void AddRight(int Key, TNode *Root) { assert(Root != NULL); if (Root->Right == NULL) { TNode *Right = new TNode(Key, Root); Root->Right = Right; } else { Insert(Key, Root->Right); } } void Insert(int Key, TNode *Root) { assert(Root != NULL); if (Root->Value == Key) return; if (Root->Value == INT_MAX) { Root->Value = Key; return; } if (Root->Value > Key) { AddLeft(Key, Root); } else { AddRight(Key, Root); } } bool Find(int Key, TNode *Node) { if (Node == NULL) return false; if (Node->Value == Key) return true; if (Node->Value > Key) return Find(Key, Node->Left); return Find(Key, Node->Right); } void OutputPreOrder(const TNode *Root) { if (Root == NULL) return; std::cout << " " << Root->Value; OutputPreOrder(Root->Left); OutputPreOrder(Root->Right); } void OutputInorder(const TNode *Root) { if (Root == NULL) return; OutputInorder(Root->Left); std::cout << " " << Root->Value; OutputInorder(Root->Right); } void Print(const TNode *Node) { OutputInorder(Node); std::cout << std::endl; OutputPreOrder(Node); std::cout << std::endl; } void Execute(const std::string &Command, TNode *Root) { if (Command == "insert") { int Key = 0; std::cin >> Key; Insert(Key, Root); return; } if (Command == "find") { int Key = 0; std::cin >> Key; if (Find(Key, Root)) { std::cout << "yes" << std::endl; return; } std::cout << "no" << std::endl; } if (Command == "print") { Print(Root); } } int main() { int Count = 0; std::cin >> Count; TNode *Root = new TNode(); for (int i = 0; i < Count; ++i) { std::string Command; std::cin >> Command; Execute(Command, Root); } Delete(Root); return 0; }
replace
77
78
77
80
TLE
p02284
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1 << 29; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; struct node { int key; node *left; node *right; node(int k) : key(k), left(nullptr), right(nullptr) {} }; void insert(node *to, node *v) { if (v->key < to->key) { if (to->left != nullptr) { insert(to->left, v); } else { to->left = v; } } else { if (to->right != nullptr) { insert(to->right, v); } else { to->right = v; } } } bool find(node *to, node *v) { if (to->key == v->key) { return true; } bool ret = false; if (to->left != nullptr) { ret |= find(to->left, v); } if (to->right != nullptr) { ret |= find(to->right, v); } return ret; } void print_first(node *target) { cout << " " << target->key; if (target->left != nullptr) print_first(target->left); if (target->right != nullptr) print_first(target->right); } void print_mid(node *target) { if (target->left != nullptr) print_mid(target->left); cout << " " << target->key; if (target->right != nullptr) print_mid(target->right); } void deletetree(node *target) { if (target->left != nullptr) deletetree(target->left); if (target->right != nullptr) deletetree(target->right); delete target; } int main() { int N; int num; string inst; cin >> N; cin >> inst; cin >> num; N--; node *root = new node(num); for (int i = 0; i < N; ++i) { cin >> inst; if (inst == "insert") { cin >> num; node *v = new node(num); insert(root, v); } else if (inst == "print") { print_mid(root); cout << endl; print_first(root); cout << endl; } else if (inst == "find") { cin >> num; node *v = new node(num); if (find(root, v)) { cout << "yes" << endl; } else { cout << "no" << endl; } } } deletetree(root); }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1 << 29; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; struct node { int key; node *left; node *right; node(int k) : key(k), left(nullptr), right(nullptr) {} }; void insert(node *to, node *v) { if (v->key < to->key) { if (to->left != nullptr) { insert(to->left, v); } else { to->left = v; } } else { if (to->right != nullptr) { insert(to->right, v); } else { to->right = v; } } } bool find(node *to, node *v) { if (to->key == v->key) { return true; } bool ret = false; if (v->key < to->key) { if (to->left != nullptr) { ret |= find(to->left, v); } } else { if (to->right != nullptr) { ret |= find(to->right, v); } } return ret; } void print_first(node *target) { cout << " " << target->key; if (target->left != nullptr) print_first(target->left); if (target->right != nullptr) print_first(target->right); } void print_mid(node *target) { if (target->left != nullptr) print_mid(target->left); cout << " " << target->key; if (target->right != nullptr) print_mid(target->right); } void deletetree(node *target) { if (target->left != nullptr) deletetree(target->left); if (target->right != nullptr) deletetree(target->right); delete target; } int main() { int N; int num; string inst; cin >> N; cin >> inst; cin >> num; N--; node *root = new node(num); for (int i = 0; i < N; ++i) { cin >> inst; if (inst == "insert") { cin >> num; node *v = new node(num); insert(root, v); } else if (inst == "print") { print_mid(root); cout << endl; print_first(root); cout << endl; } else if (inst == "find") { cin >> num; node *v = new node(num); if (find(root, v)) { cout << "yes" << endl; } else { cout << "no" << endl; } } } deletetree(root); }
replace
66
71
66
74
TLE
p02284
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; vector<vector<int>> T; vector<long> v; void in(long k) { if (k == -1) return; else { in(T[k][0]); cout << " " << v[k]; in(T[k][1]); return; } } void pre(long k) { if (k == -1) return; else { cout << " " << v[k]; pre(T[k][0]); pre(T[k][1]); return; } } int main() { string s; long n = 0, t = 0; cin >> n; T.assign(n, {-1, -1}); v.assign(n, -2000000001); for (long i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> v[t]; long y = -1, x = 0; while (x != -1) { int m = 1; y = x; if (v[t] < v[x]) m = 0; x = T[x][m]; } int s = 1; if (i > 0 && y != -1) { if (v[t] < v[y]) s = 0; T[y][s] = t; } t++; } else if (s == "print") { in(0); cout << endl; pre(0); cout << endl; } else if (s == "find") { long w = 0; cin >> w; std::vector<long>::iterator itr = find(v.begin(), v.end(), w); if (itr != v.end()) cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; vector<vector<int>> T; vector<long> v; void in(long k) { if (k == -1) return; else { in(T[k][0]); cout << " " << v[k]; in(T[k][1]); return; } } void pre(long k) { if (k == -1) return; else { cout << " " << v[k]; pre(T[k][0]); pre(T[k][1]); return; } } int main() { string s; long n = 0, t = 0; cin >> n; T.assign(n, {-1, -1}); v.assign(n, -2000000001); for (long i = 0; i < n; i++) { cin >> s; if (s == "insert") { cin >> v[t]; long y = -1, x = 0; while (x != -1) { int m = 1; y = x; if (v[t] < v[x]) m = 0; x = T[x][m]; } int s = 1; if (i > 0 && y != -1) { if (v[t] < v[y]) s = 0; T[y][s] = t; } t++; } else if (s == "print") { in(0); cout << endl; pre(0); cout << endl; } else if (s == "find") { long w = 0; cin >> w; long x = 0; while (x != -1) { int m = 1; if (w == v[x]) break; else if (w < v[x]) m = 0; x = T[x][m]; } if (x != -1) cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
replace
72
74
72
85
TLE
p02284
C++
Runtime Error
#include <cstdio> using namespace std; struct Node { Node *parent; Node *left; Node *right; int key; }; Node *NIL = NULL; void preorder(Node *v) { if (v == NIL) return; printf(" %d", v->key); preorder(v->left); preorder(v->right); } void inorder(Node *v) { if (v == NIL) return; inorder(v->left); printf(" %d", v->key); inorder(v->right); } void postorder(Node *v) { if (v == NIL) return; postorder(v->left); postorder(v->right); printf(" %d", v->key); } void insert(Node *&T, Node *z) { Node *y = NIL; Node *x = T; while (x != NIL) { y = x; if (z->key < x->key) x = x->left; else x = x->right; } z->parent = y; if (y == NIL) T = z; else if (z->key < y->key) y->left = z; else y->right = z; } bool find(Node *T, int key) { Node *x = T; if (x == NIL) return false; if (x->key == key) return true; else if (key < x->key) return find(x->left, key); else return find(x->right, key); } int main() { int n; scanf("%d", &n); Node *T = NIL; for (int i = 0; i < n; ++i) { char cmd[20]; int v; scanf("%s", cmd); if (cmd[0] == 'i') { scanf("%d", &v); Node *p = new Node; p->key = v; insert(T, p); } else if (cmd[0] == 'f') { scanf("%d", &v); printf("%s\n", find(T, v) ? "yes" : "no"); } else if (cmd[0] == 'p') { inorder(T); printf("\n"); preorder(T); printf("\n"); } } return 0; }
#include <cstdio> using namespace std; struct Node { Node *parent; Node *left; Node *right; int key; }; Node *NIL = NULL; void preorder(Node *v) { if (v == NIL) return; printf(" %d", v->key); preorder(v->left); preorder(v->right); } void inorder(Node *v) { if (v == NIL) return; inorder(v->left); printf(" %d", v->key); inorder(v->right); } void postorder(Node *v) { if (v == NIL) return; postorder(v->left); postorder(v->right); printf(" %d", v->key); } void insert(Node *&T, Node *z) { Node *y = NIL; Node *x = T; while (x != NIL) { y = x; if (z->key < x->key) x = x->left; else x = x->right; } z->parent = y; if (y == NIL) T = z; else if (z->key < y->key) y->left = z; else y->right = z; } bool find(Node *T, int key) { Node *x = T; if (x == NIL) return false; if (x->key == key) return true; else if (key < x->key) return find(x->left, key); else return find(x->right, key); } int main() { int n; scanf("%d", &n); Node *T = NIL; for (int i = 0; i < n; ++i) { char cmd[20]; int v; scanf("%s", cmd); if (cmd[0] == 'i') { scanf("%d", &v); Node *p = new Node; p->key = v; p->left = NIL; p->right = NIL; insert(T, p); } else if (cmd[0] == 'f') { scanf("%d", &v); printf("%s\n", find(T, v) ? "yes" : "no"); } else if (cmd[0] == 'p') { inorder(T); printf("\n"); preorder(T); printf("\n"); } } return 0; }
insert
81
81
81
83
0
p02284
C++
Time Limit Exceeded
#include "iostream" #include "vector" using namespace std; class branch { public: int key; branch *p; branch *left, *right; branch() {} branch(int key, branch *p) { this->key = key; this->p = p; this->left = NULL; this->right = NULL; } }; branch *tree; void insert(int z) { branch *buf; branch *y = NULL; branch *x = tree; while (x != NULL) { y = x; if (z < x->key) { x = x->left; } else { x = x->right; } } buf = new branch(z, y); if (y == NULL) tree = buf; else if (buf->key < y->key) y->left = buf; else y->right = buf; } int search(int key, branch *p) { if (p == NULL) return 0; if (p->key == key) return 1; if (search(key, p->left) == 1 || search(key, p->right) == 1) return 1; return 0; } int preParse(branch *p) { if (p == NULL) { return 0; } cout << " " << p->key; preParse(p->left); preParse(p->right); } int find(int key) { branch *p = tree; return search(key, p); } int inParse(branch *p) { if (p == NULL) { return 0; } inParse(p->left); cout << " " << p->key; inParse(p->right); } int postParse(branch *p) { if (p == NULL) { return 0; } postParse(p->left); postParse(p->right); cout << " " << p->key; } int main() { tree = NULL; int m; char cmd[20]; cin >> m; for (int i = 0; i < m; i++) { cin >> cmd; if (cmd[0] == 'i') { int n; cin >> n; insert(n); } else if (cmd[0] == 'p') { inParse(tree); cout << endl; preParse(tree); cout << endl; } else if (cmd[0] == 'f') { int n; cin >> n; if (find(n)) cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
#include "iostream" #include "vector" using namespace std; class branch { public: int key; branch *p; branch *left, *right; branch() {} branch(int key, branch *p) { this->key = key; this->p = p; this->left = NULL; this->right = NULL; } }; branch *tree; void insert(int z) { branch *buf; branch *y = NULL; branch *x = tree; while (x != NULL) { y = x; if (z < x->key) { x = x->left; } else { x = x->right; } } buf = new branch(z, y); if (y == NULL) tree = buf; else if (buf->key < y->key) y->left = buf; else y->right = buf; } int search(int key, branch *p) { if (p == NULL) return 0; if (p->key == key) return 1; if (key < p->key) { return search(key, p->left); } else { return search(key, p->right); } return 0; } int preParse(branch *p) { if (p == NULL) { return 0; } cout << " " << p->key; preParse(p->left); preParse(p->right); } int find(int key) { branch *p = tree; return search(key, p); } int inParse(branch *p) { if (p == NULL) { return 0; } inParse(p->left); cout << " " << p->key; inParse(p->right); } int postParse(branch *p) { if (p == NULL) { return 0; } postParse(p->left); postParse(p->right); cout << " " << p->key; } int main() { tree = NULL; int m; char cmd[20]; cin >> m; for (int i = 0; i < m; i++) { cin >> cmd; if (cmd[0] == 'i') { int n; cin >> n; insert(n); } else if (cmd[0] == 'p') { inParse(tree); cout << endl; preParse(tree); cout << endl; } else if (cmd[0] == 'f') { int n; cin >> n; if (find(n)) cout << "yes" << endl; else cout << "no" << endl; } } return 0; }
replace
42
44
42
47
TLE
p02284
C++
Time Limit Exceeded
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct BINNODE { int key; BINNODE *parent; BINNODE *left; BINNODE *right; BINNODE(int k) { key = k; parent = nullptr; left = nullptr; right = nullptr; } }; void preorderwalk(BINNODE *root) { if (root == nullptr) return; printf(" %d", root->key); preorderwalk(root->left); preorderwalk(root->right); return; } void inorderwalk(BINNODE *root) { if (root == nullptr) return; inorderwalk(root->left); printf(" %d", root->key); inorderwalk(root->right); return; } bool findnode(int v, BINNODE *root) { if (root == nullptr) return false; if (root->key == v) return true; return findnode(v, root->left) || findnode(v, root->right); } int main() { int m, v; int i; string cmd; BINNODE *root = nullptr; BINNODE *x; BINNODE *y; BINNODE *z; cin >> m; for (i = 0; i < m; ++i) { cin >> cmd; if (cmd == "find") { cin >> v; printf(findnode(v, root) ? "yes\n" : "no\n"); } else if (cmd == "insert") { cin >> v; z = new BINNODE(v); y = nullptr; x = root; while (x != nullptr) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == nullptr) { root = z; } else if (z->key < y->key) { y->left = z; } else { y->right = z; } } else { inorderwalk(root); printf("\n"); preorderwalk(root); printf("\n"); } } return 0; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct BINNODE { int key; BINNODE *parent; BINNODE *left; BINNODE *right; BINNODE(int k) { key = k; parent = nullptr; left = nullptr; right = nullptr; } }; void preorderwalk(BINNODE *root) { if (root == nullptr) return; printf(" %d", root->key); preorderwalk(root->left); preorderwalk(root->right); return; } void inorderwalk(BINNODE *root) { if (root == nullptr) return; inorderwalk(root->left); printf(" %d", root->key); inorderwalk(root->right); return; } bool findnode(int v, BINNODE *root) { if (root == nullptr) return false; if (root->key == v) return true; return (v < root->key) ? findnode(v, root->left) : findnode(v, root->right); } int main() { int m, v; int i; string cmd; BINNODE *root = nullptr; BINNODE *x; BINNODE *y; BINNODE *z; cin >> m; for (i = 0; i < m; ++i) { cin >> cmd; if (cmd == "find") { cin >> v; printf(findnode(v, root) ? "yes\n" : "no\n"); } else if (cmd == "insert") { cin >> v; z = new BINNODE(v); y = nullptr; x = root; while (x != nullptr) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == nullptr) { root = z; } else if (z->key < y->key) { y->left = z; } else { y->right = z; } } else { inorderwalk(root); printf("\n"); preorderwalk(root); printf("\n"); } } return 0; }
replace
43
44
43
44
TLE
p02284
C++
Runtime Error
#include <iostream> #include <string> using namespace std; struct node { int key; node *p, *l, *r; }; int n; node *N; void tree_insert(node *z) { node *y = NULL; node *x = N; while (x != NULL) { y = x; if (x->key > z->key) x = x->l; else if (x->key < z->key) x = x->r; } z->p = y; if (y == NULL) N = z; else if (y->key < z->key) y->r = z; else y->l = z; } void tree_find(int num) { node *x = N; while (x != NULL) { if (x->key == num) { cout << "yes" << endl; return; } if (x->key > num) x = x->l; if (x->key < num) x = x->r; } cout << "no" << endl; } void in(node *x) { if (x->l != NULL) in(x->l); cout << " " << x->key; if (x->r != NULL) in(x->r); } void pre(node *x) { cout << " " << x->key; if (x->l != NULL) pre(x->l); if (x->r != NULL) pre(x->r); } void print() { node *x = N; in(x); cout << endl; pre(x); cout << endl; } int main() { N = NULL; cin >> n; int i; for (i = 0; i < n; i++) { string s; cin >> s; if (s == "insert") { int num; cin >> num; node *z = new node(); z->key = num; z->l = z->r = z->p = NULL; tree_insert(z); } if (s == "find") { int num; cin >> num; tree_find(num); } if (s == "print") { print(); } } return 0; }
#include <iostream> #include <string> using namespace std; struct node { int key; node *p, *l, *r; }; int n; node *N; void tree_insert(node *z) { node *y = NULL; node *x = N; while (x != NULL) { y = x; if (x->key > z->key) x = x->l; else if (x->key < z->key) x = x->r; } z->p = y; if (y == NULL) N = z; else if (y->key < z->key) y->r = z; else y->l = z; } void tree_find(int num) { node *x = N; while (x != NULL) { if (x->key == num) { cout << "yes" << endl; return; } if (x->key > num) x = x->l; else x = x->r; } cout << "no" << endl; } void in(node *x) { if (x->l != NULL) in(x->l); cout << " " << x->key; if (x->r != NULL) in(x->r); } void pre(node *x) { cout << " " << x->key; if (x->l != NULL) pre(x->l); if (x->r != NULL) pre(x->r); } void print() { node *x = N; in(x); cout << endl; pre(x); cout << endl; } int main() { N = NULL; cin >> n; int i; for (i = 0; i < n; i++) { string s; cin >> s; if (s == "insert") { int num; cin >> num; node *z = new node(); z->key = num; z->l = z->r = z->p = NULL; tree_insert(z); } if (s == "find") { int num; cin >> num; tree_find(num); } if (s == "print") { print(); } } return 0; }
replace
40
41
40
41
-11
p02285
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct Node { int key; Node *right, *left, *parent; }; Node *NIL; struct BST { Node *root; BST() { root = NIL; } void insert(int k) { Node *y = NIL; Node *x = root; Node *z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } Node *find(int k) { Node *u = root; while (u != NIL) { if (k == u->key) { return u; } else if (k < u->key) { u = u->left; } else { u = u->right; } } return NIL; } void deletes(int k) { Node *z = find(k); if (z == NIL) return; if (z->left == NIL && z->right == NIL) { Node *p = z->parent; if (k < p->key) { p->left = NIL; } else { p->right = NIL; } free(z); } else if (z->left != NIL && z->right != NIL) { Node *d = z->right; while (d->left != NIL) d = d->left; z->key = d->key; (d->parent)->left = NIL; free(d); } else if (z->left != NIL) { Node *p = z->parent; Node *c = z->left; if (p->key > c->key) { p->left = c; } else { p->right = c; } c->parent = p; free(z); } else { Node *p = z->parent; Node *c = z->right; if (p->key > c->key) { p->left = c; } else { p->right = c; } c->parent = p; free(z); } } }; void inorder(Node *u) { if (u == NIL) return; inorder(u->left); cout << " " << u->key; inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; cout << " " << u->key; preorder(u->left); preorder(u->right); } int main() { int m; cin >> m; BST bst; while (m--) { string c; cin >> c; int n; if (c == "insert") { cin >> n; bst.insert(n); } else if (c == "find") { cin >> n; if (bst.find(n) != NIL) cout << "yes" << endl; else cout << "no" << endl; } else if (c == "delete") { cin >> n; bst.deletes(n); } else { inorder(bst.root); cout << endl; preorder(bst.root); cout << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Node { int key; Node *right, *left, *parent; }; Node *NIL; struct BST { Node *root; BST() { root = NIL; } void insert(int k) { Node *y = NIL; Node *x = root; Node *z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } Node *find(int k) { Node *u = root; while (u != NIL) { if (k == u->key) { return u; } else if (k < u->key) { u = u->left; } else { u = u->right; } } return NIL; } void deletes(int k) { Node *z = find(k); if (z == NIL) return; if (z->left == NIL && z->right == NIL) { Node *p = z->parent; if (k < p->key) { p->left = NIL; } else { p->right = NIL; } free(z); } else if (z->left != NIL && z->right != NIL) { Node *d = z->right; while (d->left != NIL) d = d->left; z->key = d->key; if (z->right == d) (d->parent)->right = NIL; else if (d->right == NIL) (d->parent)->left = NIL; else (d->parent)->left = d->right; free(d); } else if (z->left != NIL) { Node *p = z->parent; Node *c = z->left; if (p->key > c->key) { p->left = c; } else { p->right = c; } c->parent = p; free(z); } else { Node *p = z->parent; Node *c = z->right; if (p->key > c->key) { p->left = c; } else { p->right = c; } c->parent = p; free(z); } } }; void inorder(Node *u) { if (u == NIL) return; inorder(u->left); cout << " " << u->key; inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; cout << " " << u->key; preorder(u->left); preorder(u->right); } int main() { int m; cin >> m; BST bst; while (m--) { string c; cin >> c; int n; if (c == "insert") { cin >> n; bst.insert(n); } else if (c == "find") { cin >> n; if (bst.find(n) != NIL) cout << "yes" << endl; else cout << "no" << endl; } else if (c == "delete") { cin >> n; bst.deletes(n); } else { inorder(bst.root); cout << endl; preorder(bst.root); cout << endl; } } return 0; }
replace
78
79
78
84
0
p02285
C++
Runtime Error
#include <iostream> #include <vector> #define eol '\n'; using namespace std; class Node { public: int k; Node *p; Node *l; Node *r; Node() : k(-1), p(nullptr), l(nullptr), r(nullptr){}; }; Node *root; void insert(int k) { Node *y = nullptr; Node *x = root; while (x != nullptr) { y = x; if (k < x->k) { x = x->l; } else { x = x->r; } } Node *z = new Node(); z->k = k; z->p = y; if (y == nullptr) { root = z; } else if (k < y->k) { y->l = z; } else { y->r = z; } } bool find(int k) { Node *x = root; while (x != nullptr) { if (x->k == k) { return true; } else if (k < x->k) { x = x->l; } else { x = x->r; } } return false; } void remove(int k) { Node *z = nullptr; Node *x = root; while (x != nullptr) { if (x->k == k) { z = x; break; } else if (k < x->k) { x = x->l; } else { x = x->r; } } if (z->l == nullptr && z->r == nullptr) { // has not child if (z->p->l == z) { z->p->l = nullptr; } else { z->p->r = nullptr; } } else if (z->l == nullptr) { // has one child if (z->p->l == z) { z->p->l = z->r; z->r->p = z->p; } else { z->p->r = z->r; z->r->p = z->p; } } else if (z->r == nullptr) { if (z->p->l == z) { z->p->l = z->l; z->l->p = z->p; } else { z->p->r = z->l; z->l->p = z->p; } } else { Node *w = z->r; while (w->l != nullptr) { w = w->l; } // まずwを取り除く remove(w->k); // 次にzの親の子をwに更新する if (z->p->l == z) { z->p->l = w; } else { z->p->r = w; } // wの親をzの親に w->p = z->p; // wの子をzの子に w->l = z->l; w->r = z->r; // zの子の親をwに(ただし、w自身の場合は更新せず) z->l->p = w; z->r->p = w; } } void inorder(Node *x) { if (x == nullptr) return; inorder(x->l); cout << " " << x->k; inorder(x->r); if (x->k == root->k) cout << eol; } void preorder(Node *x) { if (x == nullptr) return; cout << " " << x->k; preorder(x->l); preorder(x->r); if (x->k == root->k) cout << eol; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int m, k; string cmd; cin >> m; for (int i = 0; i < m; i++) { cin >> cmd; if (cmd == "insert") { cin >> k; insert(k); } else if (cmd == "find") { cin >> k; if (find(k)) { cout << "yes" << eol; } else { cout << "no" << eol; } } else if (cmd == "print") { inorder(root); preorder(root); } else if (cmd == "delete") { cin >> k; remove(k); } } return 0; }
#include <iostream> #include <vector> #define eol '\n'; using namespace std; class Node { public: int k; Node *p; Node *l; Node *r; Node() : k(-1), p(nullptr), l(nullptr), r(nullptr){}; }; Node *root; void insert(int k) { Node *y = nullptr; Node *x = root; while (x != nullptr) { y = x; if (k < x->k) { x = x->l; } else { x = x->r; } } Node *z = new Node(); z->k = k; z->p = y; if (y == nullptr) { root = z; } else if (k < y->k) { y->l = z; } else { y->r = z; } } bool find(int k) { Node *x = root; while (x != nullptr) { if (x->k == k) { return true; } else if (k < x->k) { x = x->l; } else { x = x->r; } } return false; } void remove(int k) { Node *z = nullptr; Node *x = root; while (x != nullptr) { if (x->k == k) { z = x; break; } else if (k < x->k) { x = x->l; } else { x = x->r; } } if (z->l == nullptr && z->r == nullptr) { // has not child if (z->p->l == z) { z->p->l = nullptr; } else { z->p->r = nullptr; } } else if (z->l == nullptr) { // has one child if (z->p->l == z) { z->p->l = z->r; z->r->p = z->p; } else { z->p->r = z->r; z->r->p = z->p; } } else if (z->r == nullptr) { if (z->p->l == z) { z->p->l = z->l; z->l->p = z->p; } else { z->p->r = z->l; z->l->p = z->p; } } else { Node *w = z->r; while (w->l != nullptr) { w = w->l; } // まずwを取り除く remove(w->k); // 次にzの親の子をwに更新する if (z->p->l == z) { z->p->l = w; } else { z->p->r = w; } // wの親をzの親に w->p = z->p; // wの子をzの子に w->l = z->l; w->r = z->r; // zの子の親をwに(ただし、w自身の場合は更新せず) z->l->p = w; if (z->r != nullptr) z->r->p = w; } } void inorder(Node *x) { if (x == nullptr) return; inorder(x->l); cout << " " << x->k; inorder(x->r); if (x->k == root->k) cout << eol; } void preorder(Node *x) { if (x == nullptr) return; cout << " " << x->k; preorder(x->l); preorder(x->r); if (x->k == root->k) cout << eol; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int m, k; string cmd; cin >> m; for (int i = 0; i < m; i++) { cin >> cmd; if (cmd == "insert") { cin >> k; insert(k); } else if (cmd == "find") { cin >> k; if (find(k)) { cout << "yes" << eol; } else { cout << "no" << eol; } } else if (cmd == "print") { inorder(root); preorder(root); } else if (cmd == "delete") { cin >> k; remove(k); } } return 0; }
replace
115
116
115
117
0
p02285
C++
Runtime Error
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string> using namespace std; struct Node { int key; Node *parent, *left, *right; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } Node *treeMinimum(Node *x) { while (x->left != NIL) x = x->left; return x; } Node *find(Node *u, int k) { while (u != NIL && k != u->key) { if (k < u->key) u = u->left; else u = u->right; } return u; } Node *treeSuccessor(Node *x) { if (x->right != NIL) return treeMinimum(x->right); Node *y = x->parent; while (y != NIL && x == y->right) { x = y; y = y->parent; } return y; } void treeDelete(Node *z) { Node *y; Node *x; if (z->left == NIL || z->right) y = z; else y = treeSuccessor(z); if (y->left != NIL) { x = y->left; } else { x = y->right; } if (x != NIL) { x->parent = y->parent; } if (y->parent == NIL) { root = x; } else { if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } } if (y != z) { z->key = y->key; } free(y); } int main() { int n, i, x; string com; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> com; if (com == "insert") { scanf("%d", &x); insert(x); } else if (com == "delete") { scanf("%d", &x); treeDelete(find(root, x)); } else if (com == "find") { scanf("%d", &x); Node *t = find(root, x); if (t != NIL) printf("yes\n"); else printf("no\n"); } else if (com == "print") { inorder(root); printf("\n"); preorder(root); printf("\n"); } } return 0; }
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string> using namespace std; struct Node { int key; Node *parent, *left, *right; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } Node *treeMinimum(Node *x) { while (x->left != NIL) x = x->left; return x; } Node *find(Node *u, int k) { while (u != NIL && k != u->key) { if (k < u->key) u = u->left; else u = u->right; } return u; } Node *treeSuccessor(Node *x) { if (x->right != NIL) return treeMinimum(x->right); Node *y = x->parent; while (y != NIL && x == y->right) { x = y; y = y->parent; } return y; } void treeDelete(Node *z) { Node *y; Node *x; if (z->left == NIL || z->right == NIL) y = z; else y = treeSuccessor(z); if (y->left != NIL) { x = y->left; } else { x = y->right; } if (x != NIL) { x->parent = y->parent; } if (y->parent == NIL) { root = x; } else { if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } } if (y != z) { z->key = y->key; } free(y); } int main() { int n, i, x; string com; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> com; if (com == "insert") { scanf("%d", &x); insert(x); } else if (com == "delete") { scanf("%d", &x); treeDelete(find(root, x)); } else if (com == "find") { scanf("%d", &x); Node *t = find(root, x); if (t != NIL) printf("yes\n"); else printf("no\n"); } else if (com == "print") { inorder(root); printf("\n"); preorder(root); printf("\n"); } } return 0; }
replace
91
92
91
92
0
p02285
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; struct Node { int key; Node *parent, *left, *right; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = new Node; z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) y->left = z; else y->right = z; } } Node *find(Node *u, int k) { while (u != NIL && k != u->key) { if (k < u->key) u = u->left; else u = u->right; } return u; } Node *treeMinimum(Node *x) { while (x->left != NIL) x = x->left; return x; } Node *treeSuccessor(Node *x) { if (x->right != NIL) return treeMinimum(x->right); Node *y = x->parent; while (y != NIL && x == y->right) { x = y; y = y->parent; } return y; } void treeDelete(Node *z) { Node *y; Node *x; if (z->left == NIL || z->right) y = z; else y = treeSuccessor(z); if (y->left != NIL) { x = y->left; } else { x = y->right; } if (x != NIL) x->parent = y->parent; if (y->parent == NIL) { root = x; } else { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } if (y != z) z->key = y->key; delete y; } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int n, x; string com; cin >> n; for (int i = 0; i < n; i++) { cin >> com; if (com == "insert") { cin >> x; insert(x); } else if (com == "print") { inorder(root); cout << endl; preorder(root); cout << endl; } else if (com == "find") { cin >> x; if (find(root, x) != NIL) cout << "yes" << endl; else cout << "no" << endl; } else if (com == "delete") { cin >> x; treeDelete(find(root, x)); } } return 0; }
#include <cstdio> #include <iostream> using namespace std; struct Node { int key; Node *parent, *left, *right; }; Node *root, *NIL; void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = new Node; z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) y->left = z; else y->right = z; } } Node *find(Node *u, int k) { while (u != NIL && k != u->key) { if (k < u->key) u = u->left; else u = u->right; } return u; } Node *treeMinimum(Node *x) { while (x->left != NIL) x = x->left; return x; } Node *treeSuccessor(Node *x) { if (x->right != NIL) return treeMinimum(x->right); Node *y = x->parent; while (y != NIL && x == y->right) { x = y; y = y->parent; } return y; } void treeDelete(Node *z) { Node *y; Node *x; if (z->left == NIL || z->right == NIL) y = z; else y = treeSuccessor(z); if (y->left != NIL) { x = y->left; } else { x = y->right; } if (x != NIL) x->parent = y->parent; if (y->parent == NIL) { root = x; } else { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } if (y != z) z->key = y->key; delete y; } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int n, x; string com; cin >> n; for (int i = 0; i < n; i++) { cin >> com; if (com == "insert") { cin >> x; insert(x); } else if (com == "print") { inorder(root); cout << endl; preorder(root); cout << endl; } else if (com == "find") { cin >> x; if (find(root, x) != NIL) cout << "yes" << endl; else cout << "no" << endl; } else if (com == "delete") { cin >> x; treeDelete(find(root, x)); } } return 0; }
replace
72
73
72
73
0
p02285
C++
Runtime Error
// Ref : https://book.mynavi.jp/ec/products/detail/id=35408 #include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *parent, *left, *right; }; // These pointer adress is NULL until first store (root == NIL). // NIL adress is permanently NULL for defining empty node. Node *root, *NIL; void myInsert(int k) { Node *y = NIL; // parent Node *x = root; // comparison Node *z; // insertion z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } void inOrderPrint(Node *n) { if (n == NIL) return; inOrderPrint(n->left); cout << " " << n->key; inOrderPrint(n->right); } void preOrderPrint(Node *n) { if (n == NIL) return; cout << " " << n->key; preOrderPrint(n->left); preOrderPrint(n->right); } Node *myFind(Node *n, int k) { while (n != NIL && n->key != k) { if (k > n->key) n = n->right; else n = n->left; } return n; } void myDelete(Node *n) { Node *deletion, *children, *parent; if (n->left == NIL || n->right == NIL) { // deleted node has 0 or 1 children node. deletion = n; } else { // deleted node has 2 children node; deletion = n->right; while (deletion->left != NIL) { deletion = deletion->left; } } if (deletion->left != NIL) children = deletion->left; else children = deletion->right; parent = deletion->parent; if (parent == NIL) root = n; else if (parent->left == deletion) parent->left = children; else parent->right = children; if (children != NIL) children->parent = parent; if (deletion != n) n->key = deletion->key; free(n); } int main() { int n, x; char com[7]; cin >> n; for (int i = 0; i < n; i++) { cin >> com; if (com[0] == 'i') { cin >> x; myInsert(x); } else if (com[0] == 'p') { inOrderPrint(root); cout << endl; preOrderPrint(root); cout << endl; } else if (com[0] == 'f') { cin >> x; if (myFind(root, x) == NIL) cout << "no"; else cout << "yes"; cout << endl; } else if (com[0] == 'd') { cin >> x; myDelete(myFind(root, x)); } } return 0; }
// Ref : https://book.mynavi.jp/ec/products/detail/id=35408 #include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *parent, *left, *right; }; // These pointer adress is NULL until first store (root == NIL). // NIL adress is permanently NULL for defining empty node. Node *root, *NIL; void myInsert(int k) { Node *y = NIL; // parent Node *x = root; // comparison Node *z; // insertion z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } void inOrderPrint(Node *n) { if (n == NIL) return; inOrderPrint(n->left); cout << " " << n->key; inOrderPrint(n->right); } void preOrderPrint(Node *n) { if (n == NIL) return; cout << " " << n->key; preOrderPrint(n->left); preOrderPrint(n->right); } Node *myFind(Node *n, int k) { while (n != NIL && n->key != k) { if (k > n->key) n = n->right; else n = n->left; } return n; } void myDelete(Node *n) { Node *deletion, *children, *parent; if (n->left == NIL || n->right == NIL) { // deleted node has 0 or 1 children node. deletion = n; } else { // deleted node has 2 children node; deletion = n->right; while (deletion->left != NIL) { deletion = deletion->left; } } if (deletion->left != NIL) children = deletion->left; else children = deletion->right; parent = deletion->parent; if (parent == NIL) root = n; else if (parent->left == deletion) parent->left = children; else parent->right = children; if (children != NIL) children->parent = parent; if (deletion != n) n->key = deletion->key; free(deletion); } int main() { int n, x; char com[7]; cin >> n; for (int i = 0; i < n; i++) { cin >> com; if (com[0] == 'i') { cin >> x; myInsert(x); } else if (com[0] == 'p') { inOrderPrint(root); cout << endl; preOrderPrint(root); cout << endl; } else if (com[0] == 'f') { cin >> x; if (myFind(root, x) == NIL) cout << "no"; else cout << "yes"; cout << endl; } else if (com[0] == 'd') { cin >> x; myDelete(myFind(root, x)); } } return 0; }
replace
106
107
106
107
0
p02285
C++
Runtime Error
#include <iostream> using namespace std; struct node { int key; node *left = NULL; node *right = NULL; node *parent = NULL; node(int k, node *l = NULL, node *r = NULL, node *p = NULL) : key(k), left(l), right(r), parent(p) {} }; node *Tree; void insert(int z) { node *y = NULL; node *x = Tree; while (x != NULL) { y = x; if (z < x->key) x = x->left; else x = x->right; } if (y == NULL) Tree = new node(z); else if (z < y->key) y->left = new node(z, NULL, NULL, y); else y->right = new node(z, NULL, NULL, y); } void midprint(node *root) { if (root == NULL) return; midprint(root->left); cout << " " << root->key; midprint(root->right); } void preprint(node *root) { if (root == NULL) return; cout << " " << root->key; preprint(root->left); preprint(root->right); } node *find(int k) { node *x = Tree; while (x != NULL && x->key != k) { if (k < x->key) x = x->left; else x = x->right; } return x; } node *TreeSuccessor(node *x) { x = x->right; node *y = x; while (x != NULL && x->left == NULL) x = x->right; while (x != NULL && x->left != NULL) x = x->left; return x; } void del(int k) { node *z = find(k); if (z == NULL) return; node *y = NULL; if (z->left == NULL || z->right == NULL) y = z; else y = TreeSuccessor(z); node *x; if (y->left != NULL) x = y->left; else x = y->right; if (x != NULL) x->parent = y->parent; if (y->parent == NULL) Tree = x; else { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } if (y != z) z->key = y->key; delete y; } void print(node *root) { midprint(root); cout << endl; preprint(root); cout << endl; } int main() { int n; cin >> n; while (n--) { string s; cin >> s; if (s[0] == 'i') { int k; cin >> k; insert(k); } else if (s[0] == 'p') print(Tree); else if (s[0] == 'f') { int k; cin >> k; node *ans = find(k); if (ans == NULL) cout << "no" << endl; else cout << "yes" << endl; } else if (s[0] == 'd') { int k; cin >> k; del(k); } } return 0; }
#include <iostream> using namespace std; struct node { int key; node *left = NULL; node *right = NULL; node *parent = NULL; node(int k, node *l = NULL, node *r = NULL, node *p = NULL) : key(k), left(l), right(r), parent(p) {} }; node *Tree; void insert(int z) { node *y = NULL; node *x = Tree; while (x != NULL) { y = x; if (z < x->key) x = x->left; else x = x->right; } if (y == NULL) Tree = new node(z); else if (z < y->key) y->left = new node(z, NULL, NULL, y); else y->right = new node(z, NULL, NULL, y); } void midprint(node *root) { if (root == NULL) return; midprint(root->left); cout << " " << root->key; midprint(root->right); } void preprint(node *root) { if (root == NULL) return; cout << " " << root->key; preprint(root->left); preprint(root->right); } node *find(int k) { node *x = Tree; while (x != NULL && x->key != k) { if (k < x->key) x = x->left; else x = x->right; } return x; } node *TreeSuccessor(node *x) { x = x->right; while (x != NULL && x->left != NULL) x = x->left; return x; } void del(int k) { node *z = find(k); if (z == NULL) return; node *y = NULL; if (z->left == NULL || z->right == NULL) y = z; else y = TreeSuccessor(z); node *x; if (y->left != NULL) x = y->left; else x = y->right; if (x != NULL) x->parent = y->parent; if (y->parent == NULL) Tree = x; else { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } if (y != z) z->key = y->key; delete y; } void print(node *root) { midprint(root); cout << endl; preprint(root); cout << endl; } int main() { int n; cin >> n; while (n--) { string s; cin >> s; if (s[0] == 'i') { int k; cin >> k; insert(k); } else if (s[0] == 'p') print(Tree); else if (s[0] == 'f') { int k; cin >> k; node *ans = find(k); if (ans == NULL) cout << "no" << endl; else cout << "yes" << endl; } else if (s[0] == 'd') { int k; cin >> k; del(k); } } return 0; }
delete
60
63
60
60
0
p02285
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; // --------------------- // repetition #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; // --------------------- #define INF 922337203685477580 typedef long long ll; #define NIL -INF typedef struct Node_t { ll key; Node_t *parent; Node_t *left; Node_t *right; } Node; Node *root = NULL; void insert(ll key) { Node *y = NULL; Node *x = root; Node *z = (Node *)malloc(sizeof(Node)); z->key = key; z->parent = NULL; z->left = NULL; z->right = NULL; while (x != NULL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NULL) { root = z; } else if (z->key < y->key) { y->left = z; } else { y->right = z; } } int find(Node *r, ll key) { if (r == NULL) return 0; if (r->key == key) return 1; if (key < r->key) { return find(r->left, key); } else { return find(r->right, key); } } void delNode(Node *z) { // 1. if (z->left == NULL && z->right == NULL) { if (z->parent->key < z->key) { z->parent->right = NULL; } else { z->parent->left = NULL; } free(z); } else if (z->left == NULL) { // 2-1 if (z->parent->key < z->key) { z->parent->right = z->right; } else { z->parent->left = z->right; } z->right->parent = z->parent; free(z); } else if (z->right == NULL) { // 2-2 if (z->parent->key < z->key) { z->parent->right = z->left; } else { z->parent->left = z->left; } z->left->parent = z->parent; free(z); } else { // 3 z->key = z->right->key; delNode(z->right); } } void del(Node *z, ll key) { if (z == NULL) return; if (z->key == key) { delNode(z); } if (key < z->key) { del(z->left, key); } else { del(z->right, key); } } void inorder(Node *n) { if (n == NULL) return; inorder(n->left); cout << " " << n->key; inorder(n->right); } void preorder(Node *n) { if (n == NULL) return; cout << " " << n->key; preorder(n->left); preorder(n->right); } int main() { int n; cin >> n; // REP(i, 500500) { // Node n = {NIL, NIL, NIL, NIL}; // T[i] = n; // } REP(i, n) { string cmd; cin >> cmd; if (cmd == "insert") { ll key; cin >> key; insert(key); } else if (cmd == "find") { ll key; cin >> key; if (find(root, key)) { cout << "yes" << endl; } else { cout << "no" << endl; } } else if (cmd == "delete") { ll key; cin >> key; del(root, key); } else { inorder(root); cout << endl; preorder(root); cout << endl; } } return 0; }
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; // --------------------- // repetition #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; // --------------------- #define INF 922337203685477580 typedef long long ll; #define NIL -INF typedef struct Node_t { ll key; Node_t *parent; Node_t *left; Node_t *right; } Node; Node *root = NULL; void insert(ll key) { Node *y = NULL; Node *x = root; Node *z = (Node *)malloc(sizeof(Node)); z->key = key; z->parent = NULL; z->left = NULL; z->right = NULL; while (x != NULL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NULL) { root = z; } else if (z->key < y->key) { y->left = z; } else { y->right = z; } } int find(Node *r, ll key) { if (r == NULL) return 0; if (r->key == key) return 1; if (key < r->key) { return find(r->left, key); } else { return find(r->right, key); } } void delNode(Node *z) { // 1. if (z->left == NULL && z->right == NULL) { if (z->parent->key < z->key) { z->parent->right = NULL; } else { z->parent->left = NULL; } free(z); } else if (z->left == NULL) { // 2-1 if (z->parent->key < z->key) { z->parent->right = z->right; } else { z->parent->left = z->right; } z->right->parent = z->parent; free(z); } else if (z->right == NULL) { // 2-2 if (z->parent->key < z->key) { z->parent->right = z->left; } else { z->parent->left = z->left; } z->left->parent = z->parent; free(z); } else { // 3 Node *y = z->right; while (y->left != NULL) { y = y->left; } ll k = y->key; delNode(y); z->key = k; } } void del(Node *z, ll key) { if (z == NULL) return; if (z->key == key) { delNode(z); } if (key < z->key) { del(z->left, key); } else { del(z->right, key); } } void inorder(Node *n) { if (n == NULL) return; inorder(n->left); cout << " " << n->key; inorder(n->right); } void preorder(Node *n) { if (n == NULL) return; cout << " " << n->key; preorder(n->left); preorder(n->right); } int main() { int n; cin >> n; // REP(i, 500500) { // Node n = {NIL, NIL, NIL, NIL}; // T[i] = n; // } REP(i, n) { string cmd; cin >> cmd; if (cmd == "insert") { ll key; cin >> key; insert(key); } else if (cmd == "find") { ll key; cin >> key; if (find(root, key)) { cout << "yes" << endl; } else { cout << "no" << endl; } } else if (cmd == "delete") { ll key; cin >> key; del(root, key); } else { inorder(root); cout << endl; preorder(root); cout << endl; } } return 0; }
replace
109
111
109
117
0
p02285
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; // NIL is NULL #define NIL NULL // Node structure struct Node { int key; struct Node *parent; struct Node *leftc; struct Node *rightc; }; // define Nodepointer typedef struct Node *Nodepointer; // root Nodepointer root; // function insert void insert(int k) { Nodepointer x, y, z; y = NIL; x = root; // make memory z = new struct Node(); z->key = k; z->leftc = NIL; z->rightc = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->leftc; else x = x->rightc; } z->parent = y; if (y == NIL) root = z; else if (z->key < y->key) y->leftc = z; else y->rightc = z; } // function search Nodepointer find(Nodepointer now, int data) { if (now == NIL || data == now->key) return now; if (data < now->key) return find(now->leftc, data); else return find(now->rightc, data); } // function treeminimum Nodepointer treeminimum(Nodepointer x) { while (x->leftc != NIL) { x = x->leftc; } return x; } // function treesuccerssor Nodepointer treesuccessor(Nodepointer x) { Nodepointer y; if (x->rightc != NIL) return treeminimum(x->rightc); y = x->parent; while (y != NIL && x == x->rightc) { x = y; y = y->parent; } return y; } // function treedelete void treedelete(Nodepointer z) { Nodepointer x, y; if (z->leftc == NIL || z->rightc == NIL) y = z; else y = treesuccessor(z); if (y->leftc != NIL) x = y->leftc; else x = y->rightc; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->leftc) y->parent->leftc = x; else y->parent->rightc = x; if (y != z) z->key = y->key; } // print void inorderprint(Nodepointer now) { if (now != NIL) { inorderprint(now->leftc); printf(" %d", now->key); inorderprint(now->rightc); } } void preorderprint(Nodepointer now) { if (now != NIL) { printf(" %d", now->key); preorderprint(now->leftc); preorderprint(now->rightc); } } int main() { int n, i, j, data; Nodepointer del; char str[10]; // load n scanf("%d", &n); // load commnad and key for (i = 0; i < n; i++) { scanf("%s", str); // insert if (strcmp(str, "insert") == 0) { scanf("%d", &data); insert(data); } // find else if (strcmp(str, "find") == 0) { scanf("%d", &data); if (find(root, data) != NIL) printf("yes\n"); else printf("no\n"); } // delete else if (strcmp(str, "delete") == 0) { scanf("%d", &data); del = find(root, data); treedelete(del); // free memory delete del; } // print else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; // NIL is NULL #define NIL NULL // Node structure struct Node { int key; struct Node *parent; struct Node *leftc; struct Node *rightc; }; // define Nodepointer typedef struct Node *Nodepointer; // root Nodepointer root; // function insert void insert(int k) { Nodepointer x, y, z; y = NIL; x = root; // make memory z = new struct Node(); z->key = k; z->leftc = NIL; z->rightc = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->leftc; else x = x->rightc; } z->parent = y; if (y == NIL) root = z; else if (z->key < y->key) y->leftc = z; else y->rightc = z; } // function search Nodepointer find(Nodepointer now, int data) { if (now == NIL || data == now->key) return now; if (data < now->key) return find(now->leftc, data); else return find(now->rightc, data); } // function treeminimum Nodepointer treeminimum(Nodepointer x) { while (x->leftc != NIL) { x = x->leftc; } return x; } // function treesuccerssor Nodepointer treesuccessor(Nodepointer x) { Nodepointer y; if (x->rightc != NIL) return treeminimum(x->rightc); y = x->parent; while (y != NIL && x == x->rightc) { x = y; y = y->parent; } return y; } // function treedelete void treedelete(Nodepointer z) { Nodepointer x, y; if (z->leftc == NIL || z->rightc == NIL) y = z; else y = treesuccessor(z); if (y->leftc != NIL) x = y->leftc; else x = y->rightc; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->leftc) y->parent->leftc = x; else y->parent->rightc = x; if (y != z) z->key = y->key; } // print void inorderprint(Nodepointer now) { if (now != NIL) { inorderprint(now->leftc); printf(" %d", now->key); inorderprint(now->rightc); } } void preorderprint(Nodepointer now) { if (now != NIL) { printf(" %d", now->key); preorderprint(now->leftc); preorderprint(now->rightc); } } int main() { int n, i, j, data; Nodepointer del; char str[10]; // load n scanf("%d", &n); // load commnad and key for (i = 0; i < n; i++) { scanf("%s", str); // insert if (strcmp(str, "insert") == 0) { scanf("%d", &data); insert(data); } // find else if (strcmp(str, "find") == 0) { scanf("%d", &data); if (find(root, data) != NIL) printf("yes\n"); else printf("no\n"); } // delete else if (strcmp(str, "delete") == 0) { scanf("%d", &data); del = find(root, data); treedelete(del); } // print else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
delete
151
153
151
151
0
p02285
C++
Runtime Error
#include <iostream> #include <stdio.h> #include <string> struct Node { int key; Node *parent, *left, *right; }; Node *root, *NIL; int n; Node *getSuccessor(Node *u) { Node *x = u->right; while (x != NIL) { x = x->left; } return x; } void insert(int val) { Node *x = root; Node *y = NIL; Node *z; z = new Node(); z->key = val; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (x->key > val) { x = x->left; } else if (x->key <= val) { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else if (y->key > z->key) { y->left = z; } else if (y->key <= z->key) { y->right = z; } } void delete_Node(Node *u) { Node *y; Node *x; if (u->left != NIL && u->right != NIL) { y = getSuccessor(u); } else { y = u; } if (y->left != NIL) { x = y->left; } else { x = y->right; } if (x != NIL) { x->parent = y->parent; } if (y->parent == NIL) { root = x; } else if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } if (y != u) { u->key = y->key; } delete y; } Node *Find(Node *u, int val) { while (u != NIL) { if (u->key == val) return u; if (u->key > val) { u = u->left; } else if (u->key <= val) { u = u->right; } } return u; } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } void print() { inorder(root); printf("\n"); preorder(root); printf("\n"); } int main() { int val; char moji[10]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", moji); if (moji[0] == 'i') { scanf("%d", &val); insert(val); } else if (moji[0] == 'p') { print(); } else if (moji[0] == 'f') { scanf("%d", &val); Node *u = Find(root, val); if (u == NIL) printf("no\n"); else printf("yes\n"); } else if (moji[0] == 'd') { scanf("%d", &val); delete_Node(Find(root, val)); } } return 0; }
#include <iostream> #include <stdio.h> #include <string> struct Node { int key; Node *parent, *left, *right; }; Node *root, *NIL; int n; Node *getSuccessor(Node *u) { Node *x = u->right; while (x->left != NIL) { x = x->left; } return x; } void insert(int val) { Node *x = root; Node *y = NIL; Node *z; z = new Node(); z->key = val; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (x->key > val) { x = x->left; } else if (x->key <= val) { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else if (y->key > z->key) { y->left = z; } else if (y->key <= z->key) { y->right = z; } } void delete_Node(Node *u) { Node *y; Node *x; if (u->left != NIL && u->right != NIL) { y = getSuccessor(u); } else { y = u; } if (y->left != NIL) { x = y->left; } else { x = y->right; } if (x != NIL) { x->parent = y->parent; } if (y->parent == NIL) { root = x; } else if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } if (y != u) { u->key = y->key; } delete y; } Node *Find(Node *u, int val) { while (u != NIL) { if (u->key == val) return u; if (u->key > val) { u = u->left; } else if (u->key <= val) { u = u->right; } } return u; } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } void print() { inorder(root); printf("\n"); preorder(root); printf("\n"); } int main() { int val; char moji[10]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", moji); if (moji[0] == 'i') { scanf("%d", &val); insert(val); } else if (moji[0] == 'p') { print(); } else if (moji[0] == 'f') { scanf("%d", &val); Node *u = Find(root, val); if (u == NIL) printf("no\n"); else printf("yes\n"); } else if (moji[0] == 'd') { scanf("%d", &val); delete_Node(Find(root, val)); } } return 0; }
replace
14
15
14
15
0
p02285
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *right, *left, *parent; }; Node *root, *NIL; // 宣言時ポインタにはNULL(0x0)が入っているため, // 木の根が設定されていなければroot == NILとなる. void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } Node *find(Node *u, int k) { while (u != NIL && k != u->key) { if (k < u->key) u = u->left; else u = u->right; } return u; } void deleteNode(int k) { Node *y; Node *z = find(root, k); if (z->left == NIL && z->right == NIL) { if (z->parent->left == z) z->parent->left = NIL; else z->parent->right = NIL; free(z); } else if (z->left != NIL && z->right == NIL) { if (z->parent->left == z) z->parent->left = z->left; else z->parent->right = z->left; z->left->parent = z->parent; free(z); } else if (z->left == NIL && z->right != NIL) { if (z->parent->left == z) z->parent->left = z->right; else z->parent->right = z->right; z->right->parent = z->parent; free(z); } else { y = z->right; while (y->left != NIL) y = y->left; z->key = y->key; if (y->parent->left == y) { y->parent->left = y->right; } else { y->parent->right = y->right; } y->right->parent = y->parent; free(y); } } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int n, i, x; string com; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> com; if (com == "insert") { scanf("%d", &x); insert(x); } else if (com == "print") { inorder(root); printf("\n"); preorder(root); printf("\n"); } else if (com == "find") { scanf("%d", &x); Node *t = find(root, x); if (t != NIL) printf("yes\n"); else printf("no\n"); } else { scanf("%d", &x); deleteNode(x); } } return 0; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; struct Node { int key; Node *right, *left, *parent; }; Node *root, *NIL; // 宣言時ポインタにはNULL(0x0)が入っているため, // 木の根が設定されていなければroot == NILとなる. void insert(int k) { Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } Node *find(Node *u, int k) { while (u != NIL && k != u->key) { if (k < u->key) u = u->left; else u = u->right; } return u; } void deleteNode(int k) { Node *y; Node *z = find(root, k); if (z->left == NIL && z->right == NIL) { if (z->parent->left == z) z->parent->left = NIL; else z->parent->right = NIL; free(z); } else if (z->left != NIL && z->right == NIL) { if (z->parent->left == z) z->parent->left = z->left; else z->parent->right = z->left; z->left->parent = z->parent; free(z); } else if (z->left == NIL && z->right != NIL) { if (z->parent->left == z) z->parent->left = z->right; else z->parent->right = z->right; z->right->parent = z->parent; free(z); } else { y = z->right; while (y->left != NIL) y = y->left; z->key = y->key; if (y->parent->left == y) y->parent->left = NIL; else y->parent->right = NIL; free(y); } } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int n, i, x; string com; scanf("%d", &n); for (i = 0; i < n; i++) { cin >> com; if (com == "insert") { scanf("%d", &x); insert(x); } else if (com == "print") { inorder(root); printf("\n"); preorder(root); printf("\n"); } else if (com == "find") { scanf("%d", &x); Node *t = find(root, x); if (t != NIL) printf("yes\n"); else printf("no\n"); } else { scanf("%d", &x); deleteNode(x); } } return 0; }
replace
83
89
83
87
0
p02285
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <list> #include <queue> #include <stack> #include <string.h> #include <string> #include <vector> #define _USE_MATH_DEFINES #include <math.h> using namespace std; struct node { int key; node *parent, *left, *right; }; node *root, *NIL; void insert(int k) { node *y = NIL; node *x = root; node *z; z = (node *)malloc(sizeof(node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->left; else x = x->right; } z->parent = y; if (y == NIL) { root = z; } else if (z->key < y->key) { y->left = z; } else { y->right = z; } } node *find(int k) { node *x = root; node *y = NIL; while (x != NIL && k != x->key) { if (k < x->key) x = x->left; else if (k > x->key) x = x->right; } return x; } node *mini(node *u) { while (u != NIL) { u = u->left; } return u; } node *Successor(node *u) { if (u->right != NIL) return mini(u->right); node *y = u->parent; while (y != NIL && u == y->right) { u = y; y = y->parent; } return y; } void Delete(node *z) { node *y; node *x; if (z->left == NIL || z->right == NIL) y = z; else y = Successor(z); if (y->left != NIL) { x = y->left; } else { x = y->right; } if (x != NIL) { x->parent = y->parent; } if (y->parent == NIL) { root = x; } else { if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } } if (y != z) { z->key = y->key; } free(y); } void Preorder(node *t) { if (t == NIL) return; printf(" %d", t->key); Preorder(t->left); Preorder(t->right); } void Inorder(node *t) { if (t == NIL) return; Inorder(t->left); printf(" %d", t->key); Inorder(t->right); } void Postorder(node *t) { if (t == NIL) return; Postorder(t->left); Postorder(t->right); printf(" %d", t->key); } int main() { int x, n; string st; cin >> n; for (int i = 0; i < n; i++) { cin >> st; if (st[0] == 'i') { cin >> x; insert(x); } else if (st[0] == 'p') { Inorder(root); cout << endl; Preorder(root); cout << endl; } else if (st[0] == 'f') { cin >> x; node *t = find(x); if (t == NIL) cout << "no"; else cout << "yes"; cout << endl; } else { cin >> x; Delete(find(x)); } } return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <list> #include <queue> #include <stack> #include <string.h> #include <string> #include <vector> #define _USE_MATH_DEFINES #include <math.h> using namespace std; struct node { int key; node *parent, *left, *right; }; node *root, *NIL; void insert(int k) { node *y = NIL; node *x = root; node *z; z = (node *)malloc(sizeof(node)); z->key = k; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->left; else x = x->right; } z->parent = y; if (y == NIL) { root = z; } else if (z->key < y->key) { y->left = z; } else { y->right = z; } } node *find(int k) { node *x = root; node *y = NIL; while (x != NIL && k != x->key) { if (k < x->key) x = x->left; else if (k > x->key) x = x->right; } return x; } node *mini(node *u) { while (u->left != NIL) { u = u->left; } return u; } node *Successor(node *u) { if (u->right != NIL) return mini(u->right); node *y = u->parent; while (y != NIL && u == y->right) { u = y; y = y->parent; } return y; } void Delete(node *z) { node *y; node *x; if (z->left == NIL || z->right == NIL) y = z; else y = Successor(z); if (y->left != NIL) { x = y->left; } else { x = y->right; } if (x != NIL) { x->parent = y->parent; } if (y->parent == NIL) { root = x; } else { if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } } if (y != z) { z->key = y->key; } free(y); } void Preorder(node *t) { if (t == NIL) return; printf(" %d", t->key); Preorder(t->left); Preorder(t->right); } void Inorder(node *t) { if (t == NIL) return; Inorder(t->left); printf(" %d", t->key); Inorder(t->right); } void Postorder(node *t) { if (t == NIL) return; Postorder(t->left); Postorder(t->right); printf(" %d", t->key); } int main() { int x, n; string st; cin >> n; for (int i = 0; i < n; i++) { cin >> st; if (st[0] == 'i') { cin >> x; insert(x); } else if (st[0] == 'p') { Inorder(root); cout << endl; Preorder(root); cout << endl; } else if (st[0] == 'f') { cin >> x; node *t = find(x); if (t == NIL) cout << "no"; else cout << "yes"; cout << endl; } else { cin >> x; Delete(find(x)); } } return 0; }
replace
62
63
62
63
0
p02285
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <list> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) using namespace std; class node { public: int key; node *left, *right, *parent; node() : key(0), left(NULL), right(NULL), parent(NULL){}; node(int n) : key(n), left(NULL), right(NULL), parent(NULL){}; }; class tree { public: node *root; tree() : root(NULL){}; tree(node *n) : root(n){}; void insert(int n) { node N(n); this->insert(N); }; void insert(node n) { node *y = NULL; node *x = root; while (x != NULL) { y = x; if (n.key < x->key) x = x->left; else x = x->right; } n.parent = y; if (y == NULL) root = new node(n); else if (n.key < y->key) y->left = new node(n); else y->right = new node(n); }; bool find(int n) { if (root->key == n) return true; if (root->key > n) { tree t1(root->left); if (t1.root != NULL) return t1.find(n); else return false; } if (root->key < n) { tree t1(root->right); if (t1.root != NULL) return t1.find(n); else return false; } }; void Delete(int n) { if (root->key > n) { tree t1(root->left); if (t1.root != NULL) t1.Delete(n); return; } if (root->key < n) { tree t1(root->right); if (t1.root != NULL) t1.Delete(n); return; } if (root->left == NULL && root->right == NULL) { if (root->parent->key > n) root->parent->left = NULL; else root->parent->right = NULL; } else if (root->left != NULL && root->right != NULL) { node *y = root->right; while (y->left != NULL) y = y->left; root->left->parent = y; root->right->parent = y; if (root->parent->key > n) root->parent->left = y; else root->parent->right = y; if (y->parent->key > y->key) y->parent->left = y->right; else y->parent->right = y->right; y->parent = root->parent; y->left = root->left; if (y != root->right) y->right = root->right; else y->right = y->right->right; } else { if (root->left != NULL) { if (root->parent->key > n) root->parent->left = root->left; else root->parent->right = root->left; root->left->parent = root->parent; } else { if (root->parent->key > n) root->parent->left = root->right; else root->parent->right = root->right; root->right->parent = root->parent; } } } void Pre() { tree t1(root->left), t2(root->right); cout << " " << root->key; if (t1.root != NULL) t1.Pre(); if (t2.root != NULL) t2.Pre(); }; void Ino() { tree t1(root->left), t2(root->right); if (t1.root != NULL) t1.Ino(); cout << " " << root->key; if (t2.root != NULL) t2.Ino(); }; void Pos() { tree t1(root->left), t2(root->right); if (t1.root != NULL) t1.Pos(); if (t2.root != NULL) t2.Pos(); cout << " " << root->key; }; }; int main() { int n, nd; string str; tree T; cin >> n; REP(i, n) { cin >> str; if (str == "insert") { cin >> nd; T.insert(nd); } else if (str == "find") { cin >> nd; cout << (T.find(nd) ? "yes" : "no") << endl; } else if (str == "delete") { cin >> nd; T.Delete(nd); } else { T.Ino(); cout << endl; T.Pre(); cout << endl; } } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <list> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) using namespace std; class node { public: int key; node *left, *right, *parent; node() : key(0), left(NULL), right(NULL), parent(NULL){}; node(int n) : key(n), left(NULL), right(NULL), parent(NULL){}; }; class tree { public: node *root; tree() : root(NULL){}; tree(node *n) : root(n){}; void insert(int n) { node N(n); this->insert(N); }; void insert(node n) { node *y = NULL; node *x = root; while (x != NULL) { y = x; if (n.key < x->key) x = x->left; else x = x->right; } n.parent = y; if (y == NULL) root = new node(n); else if (n.key < y->key) y->left = new node(n); else y->right = new node(n); }; bool find(int n) { if (root->key == n) return true; if (root->key > n) { tree t1(root->left); if (t1.root != NULL) return t1.find(n); else return false; } if (root->key < n) { tree t1(root->right); if (t1.root != NULL) return t1.find(n); else return false; } }; void Delete(int n) { if (root->key > n) { tree t1(root->left); if (t1.root != NULL) t1.Delete(n); return; } if (root->key < n) { tree t1(root->right); if (t1.root != NULL) t1.Delete(n); return; } if (root->left == NULL && root->right == NULL) { if (root->parent->key > n) root->parent->left = NULL; else root->parent->right = NULL; } else if (root->left != NULL && root->right != NULL) { node *y = root->right; while (y->left != NULL) y = y->left; root->left->parent = y; root->right->parent = y; if (root->parent->key > n) root->parent->left = y; else root->parent->right = y; if (y->parent->key > y->key) y->parent->left = y->right; else y->parent->right = y->right; y->parent = root->parent; y->left = root->left; if (y != root->right) y->right = root->right; else y->right = root->right->right; } else { if (root->left != NULL) { if (root->parent->key > n) root->parent->left = root->left; else root->parent->right = root->left; root->left->parent = root->parent; } else { if (root->parent->key > n) root->parent->left = root->right; else root->parent->right = root->right; root->right->parent = root->parent; } } } void Pre() { tree t1(root->left), t2(root->right); cout << " " << root->key; if (t1.root != NULL) t1.Pre(); if (t2.root != NULL) t2.Pre(); }; void Ino() { tree t1(root->left), t2(root->right); if (t1.root != NULL) t1.Ino(); cout << " " << root->key; if (t2.root != NULL) t2.Ino(); }; void Pos() { tree t1(root->left), t2(root->right); if (t1.root != NULL) t1.Pos(); if (t2.root != NULL) t2.Pos(); cout << " " << root->key; }; }; int main() { int n, nd; string str; tree T; cin >> n; REP(i, n) { cin >> str; if (str == "insert") { cin >> nd; T.insert(nd); } else if (str == "find") { cin >> nd; cout << (T.find(nd) ? "yes" : "no") << endl; } else if (str == "delete") { cin >> nd; T.Delete(nd); } else { T.Ino(); cout << endl; T.Pre(); cout << endl; } } return 0; }
replace
104
105
104
105
0
p02285
C++
Runtime Error
// 二分探索木の削除 #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <stack> #include <vector> using namespace std; #define MAX_N 30 // const int NIL = -1; typedef struct node { // 節点の値 int key; // 節点の親 struct node *parent; // 節点の左の子 struct node *left; // 節点の右の子 struct node *right; } Node; // グローバル変数の定義 Node *p_Tree; void preorderTreeWalk(Node *Tree); void inorderTreeWalk(Node *Tree); void postorderTreeWalk(Node *Tree); void insert(int num); bool deleteTree(int num); bool find(Node *Tree, int num); int main(void) { int n, num; char str[10]; cin >> n; for (int i = 0; i < n; i++) { // 命令を取得 scanf("%s", str); if (str[0] == 'i') { // --- 挿入の命令の時 --- scanf("%d", &num); insert(num); } else if (str[0] == 'f') { // --- 検索の命令の時 --- scanf("%d", &num); if (find(p_Tree, num)) { printf("yes\n"); } else { printf("no\n"); } } else if (str[0] == 'd') { // --- 削除の命令の時 --- scanf("%d", &num); deleteTree(num); } else { // --- 表示の命令 --- inorderTreeWalk(p_Tree); printf("\n"); preorderTreeWalk(p_Tree); printf("\n"); } } return 0; } // ----------▼▼▼▼▼ 2分探索木の操作関数 ▼▼▼▼▼---------- // 二分探索木の検索関数 bool find(Node *Tree, int num) { bool ret = false; Node *tree = Tree; while (tree != NULL) { if (num < tree->key) { // 左の子を探索 tree = tree->left; } else if (num > tree->key) { // 右の子を探索 tree = tree->right; } else { // 値が見つかったので, 処理終了 ret = true; break; } } return ret; } // 二分探索木の挿入関数 void insert(int num) { Node *tree, *parent; // 追加する要素の親 parent = NULL; // 木の根 ( 先頭アドレス ) tree = p_Tree; Node *newNode = new Node; newNode->key = num; newNode->parent = NULL; newNode->left = NULL; newNode->right = NULL; while (tree != NULL) { // 親の候補の取得 parent = tree; if (newNode->key < tree->key) { // 左の子へ移動 tree = tree->left; } else { // 右の子へ移動 tree = tree->right; } } // 追加する要素の親を設定 newNode->parent = parent; if (parent == NULL) { // 木が空の時 newNode->parent = NULL; // 木が空のなので, 実体を入れる p_Tree = newNode; } else if (newNode->key < parent->key) { // 左の子にする parent->left = newNode; } else { // 右の子にする parent->right = newNode; } return; } // 2分探索木の削除関数 bool deleteTree(int num) { bool ret = true; Node *tree; // 木の根 ( 先頭アドレス ) tree = p_Tree; while (tree != NULL && tree->key != num) { if (num < tree->key) { // 左の子へ移動 tree = tree->left; } else if (num > tree->key) { // 右の子へ移動 tree = tree->right; } } if (tree == NULL) { // 削除要素が無かった時 ret = false; } else { // ----- 削除要素があった時 ----- if (tree->left == NULL && tree->right == NULL) { // 削除要素が子を持たない時 if (tree->key < tree->parent->key) { // 削除要素が自分の親の左の子の時 tree->parent->left = NULL; } else { // 削除要素が自分の親の右の子の時 tree->parent->right = NULL; } // 直接要素を削除する delete tree; } else if (tree->left != NULL && tree->right != NULL) { // 削除要素が子を2つ持つ時 // ---▼▼▼ 削除する要素の右の子から値が最小の要素を検索する ▼▼▼--- Node *minElement = tree->right; while (minElement->left != NULL) { minElement = minElement->left; } // ---▲▲▲ 削除する要素の右の子から値が最小の要素を検索する ▲▲▲--- // ----- 置き換える要素に, データを設定する ----- if (minElement->right != NULL) { // 置き換える要素の右の子を置き換えておく // ( 2分探索木の性質上, 左の子は存在しない ) minElement->parent->left = minElement->right; minElement->right->parent = minElement->parent; } if (minElement != tree->right) { // 右側に要素が, 1つ以上ある時 minElement->right = tree->right; } // 左側の要素の置き換え minElement->left = tree->left; tree->left->parent = minElement; // 親の置き換え // minElement->parent->left = NULL; minElement->parent = tree->parent; // 全体の置き換え if (tree->key < tree->parent->key) { // 削除要素自身が左の子の時 tree->parent->left = minElement; } else { // 削除要素自身が右の子の時 tree->parent->right = minElement; } delete tree; } else { // 削除要素が子を1つ持つ時 Node *child; if (tree->left != NULL) { // 削除要素の左側に子が存在 tree->left->parent = tree->parent; child = tree->left; } else { // 削除要素の右側に子が存在 tree->right->parent = tree->parent; child = tree->right; } if (tree->key < tree->parent->key) { // 削除要素自身が左の子の時 tree->parent->left = child; } else { // 削除要素自身が右の子の時 tree->parent->right = child; } // 要素を削除する delete tree; } } return ret; } // ----------▲▲▲▲▲ 2分探索木の操作関数 ▲▲▲▲▲---------- // ----------▼▼▼▼▼ 巡回関連の関数 ▼▼▼▼▼---------- // 先行順巡回 // 根節点、左部分木、右部分木の順で節点を巡回 void preorderTreeWalk(Node *Tree) { if (Tree == NULL) { return; } printf(" %d", Tree->key); preorderTreeWalk(Tree->left); preorderTreeWalk(Tree->right); return; } int pre_right = 0; // 中間順巡回 // 左部分木、根節点、右部分木の順で節点を巡回 void inorderTreeWalk(Node *Tree) { if (Tree == NULL) { return; } inorderTreeWalk(Tree->left); printf(" %d", Tree->key); inorderTreeWalk(Tree->right); return; } // 後行順巡回 // 左部分木、右部分木、根節点の順で節点を巡回 void postorderTreeWalk(Node *Tree) { if (Tree == NULL) { return; } postorderTreeWalk(Tree->left); postorderTreeWalk(Tree->right); printf(" %d", Tree->key); return; } // ----------▲▲▲▲▲ 巡回関連の関数 ▲▲▲▲▲----------
// 二分探索木の削除 #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <stack> #include <vector> using namespace std; #define MAX_N 30 // const int NIL = -1; typedef struct node { // 節点の値 int key; // 節点の親 struct node *parent; // 節点の左の子 struct node *left; // 節点の右の子 struct node *right; } Node; // グローバル変数の定義 Node *p_Tree; void preorderTreeWalk(Node *Tree); void inorderTreeWalk(Node *Tree); void postorderTreeWalk(Node *Tree); void insert(int num); bool deleteTree(int num); bool find(Node *Tree, int num); int main(void) { int n, num; char str[10]; cin >> n; for (int i = 0; i < n; i++) { // 命令を取得 scanf("%s", str); if (str[0] == 'i') { // --- 挿入の命令の時 --- scanf("%d", &num); insert(num); } else if (str[0] == 'f') { // --- 検索の命令の時 --- scanf("%d", &num); if (find(p_Tree, num)) { printf("yes\n"); } else { printf("no\n"); } } else if (str[0] == 'd') { // --- 削除の命令の時 --- scanf("%d", &num); deleteTree(num); } else { // --- 表示の命令 --- inorderTreeWalk(p_Tree); printf("\n"); preorderTreeWalk(p_Tree); printf("\n"); } } return 0; } // ----------▼▼▼▼▼ 2分探索木の操作関数 ▼▼▼▼▼---------- // 二分探索木の検索関数 bool find(Node *Tree, int num) { bool ret = false; Node *tree = Tree; while (tree != NULL) { if (num < tree->key) { // 左の子を探索 tree = tree->left; } else if (num > tree->key) { // 右の子を探索 tree = tree->right; } else { // 値が見つかったので, 処理終了 ret = true; break; } } return ret; } // 二分探索木の挿入関数 void insert(int num) { Node *tree, *parent; // 追加する要素の親 parent = NULL; // 木の根 ( 先頭アドレス ) tree = p_Tree; Node *newNode = new Node; newNode->key = num; newNode->parent = NULL; newNode->left = NULL; newNode->right = NULL; while (tree != NULL) { // 親の候補の取得 parent = tree; if (newNode->key < tree->key) { // 左の子へ移動 tree = tree->left; } else { // 右の子へ移動 tree = tree->right; } } // 追加する要素の親を設定 newNode->parent = parent; if (parent == NULL) { // 木が空の時 newNode->parent = NULL; // 木が空のなので, 実体を入れる p_Tree = newNode; } else if (newNode->key < parent->key) { // 左の子にする parent->left = newNode; } else { // 右の子にする parent->right = newNode; } return; } // 2分探索木の削除関数 bool deleteTree(int num) { bool ret = true; Node *tree; // 木の根 ( 先頭アドレス ) tree = p_Tree; while (tree != NULL && tree->key != num) { if (num < tree->key) { // 左の子へ移動 tree = tree->left; } else if (num > tree->key) { // 右の子へ移動 tree = tree->right; } } if (tree == NULL) { // 削除要素が無かった時 ret = false; } else { // ----- 削除要素があった時 ----- if (tree->left == NULL && tree->right == NULL) { // 削除要素が子を持たない時 if (tree->key < tree->parent->key) { // 削除要素が自分の親の左の子の時 tree->parent->left = NULL; } else { // 削除要素が自分の親の右の子の時 tree->parent->right = NULL; } // 直接要素を削除する delete tree; } else if (tree->left != NULL && tree->right != NULL) { // 削除要素が子を2つ持つ時 // ---▼▼▼ 削除する要素の右の子から値が最小の要素を検索する ▼▼▼--- Node *minElement = tree->right; while (minElement->left != NULL) { minElement = minElement->left; } // ---▲▲▲ 削除する要素の右の子から値が最小の要素を検索する ▲▲▲--- // ----- 置き換える要素に, データを設定する ----- if (minElement->right != NULL) { // 置き換える要素の右の子を置き換えておく // ( 2分探索木の性質上, 左の子は存在しない ) minElement->parent->left = minElement->right; minElement->right->parent = minElement->parent; } if (minElement != tree->right) { // 右側に要素が, 1つ以上ある時 minElement->right = tree->right; } // 左側の要素の置き換え minElement->left = tree->left; tree->left->parent = minElement; // 親の置き換え minElement->parent->left = NULL; minElement->parent = tree->parent; // 全体の置き換え if (tree->key < tree->parent->key) { // 削除要素自身が左の子の時 tree->parent->left = minElement; } else { // 削除要素自身が右の子の時 tree->parent->right = minElement; } delete tree; } else { // 削除要素が子を1つ持つ時 Node *child; if (tree->left != NULL) { // 削除要素の左側に子が存在 tree->left->parent = tree->parent; child = tree->left; } else { // 削除要素の右側に子が存在 tree->right->parent = tree->parent; child = tree->right; } if (tree->key < tree->parent->key) { // 削除要素自身が左の子の時 tree->parent->left = child; } else { // 削除要素自身が右の子の時 tree->parent->right = child; } // 要素を削除する delete tree; } } return ret; } // ----------▲▲▲▲▲ 2分探索木の操作関数 ▲▲▲▲▲---------- // ----------▼▼▼▼▼ 巡回関連の関数 ▼▼▼▼▼---------- // 先行順巡回 // 根節点、左部分木、右部分木の順で節点を巡回 void preorderTreeWalk(Node *Tree) { if (Tree == NULL) { return; } printf(" %d", Tree->key); preorderTreeWalk(Tree->left); preorderTreeWalk(Tree->right); return; } int pre_right = 0; // 中間順巡回 // 左部分木、根節点、右部分木の順で節点を巡回 void inorderTreeWalk(Node *Tree) { if (Tree == NULL) { return; } inorderTreeWalk(Tree->left); printf(" %d", Tree->key); inorderTreeWalk(Tree->right); return; } // 後行順巡回 // 左部分木、右部分木、根節点の順で節点を巡回 void postorderTreeWalk(Node *Tree) { if (Tree == NULL) { return; } postorderTreeWalk(Tree->left); postorderTreeWalk(Tree->right); printf(" %d", Tree->key); return; } // ----------▲▲▲▲▲ 巡回関連の関数 ▲▲▲▲▲----------
replace
224
225
224
225
0
p02285
C++
Runtime Error
#include <iostream> #include <string> using namespace std; struct Node { int val; Node *parent; Node *left; Node *right; }; Node *nil; class Tree { Node *root; void preorder(Node *p) { if (p == nil) return; cout << " " << p->val; preorder(p->left); preorder(p->right); } void inorder(Node *p) { if (p == nil) return; inorder(p->left); cout << " " << p->val; inorder(p->right); } Node *next_node(Node *z) { if (z->left == nil) return z; return next_node(z->left); } void delete_node(Node *z) { if (z->left == nil && z->right == nil) { if (z == root) { root = nil; return; } if (z->parent->left == z) { z->parent->left = nil; } else { z->parent->right = nil; } return; } if (z->left != nil && z->right != nil) { Node *next = next_node(z->right); z->val = next->val; delete_node(next); } Node *z_c; if (z->left == nil) { z_c = z->right; } if (z->right == nil) { z_c = z->left; } if (z == root) { root = z_c; return; } if (z->parent->left == z) { z->parent->left = z_c; } else { z->parent->right = z_c; } z_c->parent = z->parent; } Node *find_node(int k) { Node *p = root; while (p != nil) { if (k == p->val) return p; if (k < p->val) { p = p->left; } else { p = p->right; } } return nil; } public: Tree() { root = nil; } void insert(int k) { Node *n = new Node; n->val = k; n->left = nil; n->right = nil; if (root == nil) { root = n; n->parent = nil; return; } Node *pos = root; Node *pos_p; while (pos != nil) { pos_p = pos; if (k < pos->val) { pos = pos->left; continue; } pos = pos->right; } n->parent = pos_p; if (k < pos_p->val) { pos_p->left = n; } else { pos_p->right = n; } } void print() { inorder(root); cout << endl; preorder(root); cout << endl; } bool find(int k) { Node *z = find_node(k); if (z == nil) { return false; } else { return true; } } void del(int k) { Node *z = find_node(k); delete_node(z); } }; int main() { int n; cin >> n; Tree T; for (int i = 0; i < n; i++) { string str; cin >> str; if (str == "print") { T.print(); continue; } int k; cin >> k; if (str == "insert") { T.insert(k); continue; } if (str == "find") { if (T.find(k)) { cout << "yes" << endl; } else { cout << "no" << endl; } continue; } if (str == "delete") { T.del(k); } } return 0; }
#include <iostream> #include <string> using namespace std; struct Node { int val; Node *parent; Node *left; Node *right; }; Node *nil; class Tree { Node *root; void preorder(Node *p) { if (p == nil) return; cout << " " << p->val; preorder(p->left); preorder(p->right); } void inorder(Node *p) { if (p == nil) return; inorder(p->left); cout << " " << p->val; inorder(p->right); } Node *next_node(Node *z) { if (z->left == nil) return z; return next_node(z->left); } void delete_node(Node *z) { if (z->left == nil && z->right == nil) { if (z == root) { root = nil; return; } if (z->parent->left == z) { z->parent->left = nil; } else { z->parent->right = nil; } return; } if (z->left != nil && z->right != nil) { Node *next = next_node(z->right); z->val = next->val; delete_node(next); return; } Node *z_c; if (z->left == nil) { z_c = z->right; } if (z->right == nil) { z_c = z->left; } if (z == root) { root = z_c; return; } if (z->parent->left == z) { z->parent->left = z_c; } else { z->parent->right = z_c; } z_c->parent = z->parent; } Node *find_node(int k) { Node *p = root; while (p != nil) { if (k == p->val) return p; if (k < p->val) { p = p->left; } else { p = p->right; } } return nil; } public: Tree() { root = nil; } void insert(int k) { Node *n = new Node; n->val = k; n->left = nil; n->right = nil; if (root == nil) { root = n; n->parent = nil; return; } Node *pos = root; Node *pos_p; while (pos != nil) { pos_p = pos; if (k < pos->val) { pos = pos->left; continue; } pos = pos->right; } n->parent = pos_p; if (k < pos_p->val) { pos_p->left = n; } else { pos_p->right = n; } } void print() { inorder(root); cout << endl; preorder(root); cout << endl; } bool find(int k) { Node *z = find_node(k); if (z == nil) { return false; } else { return true; } } void del(int k) { Node *z = find_node(k); delete_node(z); } }; int main() { int n; cin >> n; Tree T; for (int i = 0; i < n; i++) { string str; cin >> str; if (str == "print") { T.print(); continue; } int k; cin >> k; if (str == "insert") { T.insert(k); continue; } if (str == "find") { if (T.find(k)) { cout << "yes" << endl; } else { cout << "no" << endl; } continue; } if (str == "delete") { T.del(k); } } return 0; }
insert
50
50
50
51
0
p02285
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; // NIL is NULL #define NIL NULL // Node structure struct Node { int key; struct Node *parent; struct Node *leftc; struct Node *rightc; }; // define Nodepointer typedef struct Node *Nodepointer; // root Nodepointer root; // function insert void insert(int k) { Nodepointer x, y, z; y = NIL; x = root; // make memory z = new struct Node(); z->key = k; z->leftc = NIL; z->rightc = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->leftc; else x = x->rightc; } z->parent = y; if (y == NIL) root = z; else if (z->key < y->key) y->leftc = z; else y->rightc = z; } // function search Nodepointer find(Nodepointer now, int data) { if (now == NIL || data == now->key) return now; if (data < now->key) return find(now->leftc, data); else return find(now->rightc, data); } // function treeminimum Nodepointer treeminimum(Nodepointer x) { while (x->leftc != NIL) { x = x->leftc; } return x; } // function treesuccerssor Nodepointer treesuccessor(Nodepointer x) { Nodepointer y; if (x->rightc != NIL) return treeminimum(x->rightc); y = x->parent; while (y != NIL && x == x->rightc) { x = y; y = y->parent; } return y; } // function treedelete void treedelete(Nodepointer z) { Nodepointer x, y; if (z->leftc == NIL || z->rightc == NIL) y = z; else y = treesuccessor(z); if (y->leftc != NIL) x = y->leftc; else x = y->rightc; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->leftc) y->parent->leftc = x; else y->parent->rightc = x; if (y != z) z->key = y->key; // free memory delete z; } // print void inorderprint(Nodepointer now) { if (now != NIL) { inorderprint(now->leftc); printf(" %d", now->key); inorderprint(now->rightc); } } void preorderprint(Nodepointer now) { if (now != NIL) { printf(" %d", now->key); preorderprint(now->leftc); preorderprint(now->rightc); } } int main() { int n, i, j, data; Nodepointer del; char str[10]; // load n scanf("%d", &n); // load commnad and key for (i = 0; i < n; i++) { scanf("%s", str); // insert if (strcmp(str, "insert") == 0) { scanf("%d", &data); insert(data); } // find else if (strcmp(str, "find") == 0) { scanf("%d", &data); if (find(root, data) != NIL) printf("yes\n"); else printf("no\n"); } // delete else if (strcmp(str, "delete") == 0) { scanf("%d", &data); del = find(root, data); treedelete(del); } // print else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; // NIL is NULL #define NIL NULL // Node structure struct Node { int key; struct Node *parent; struct Node *leftc; struct Node *rightc; }; // define Nodepointer typedef struct Node *Nodepointer; // root Nodepointer root; // function insert void insert(int k) { Nodepointer x, y, z; y = NIL; x = root; // make memory z = new struct Node(); z->key = k; z->leftc = NIL; z->rightc = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->leftc; else x = x->rightc; } z->parent = y; if (y == NIL) root = z; else if (z->key < y->key) y->leftc = z; else y->rightc = z; } // function search Nodepointer find(Nodepointer now, int data) { if (now == NIL || data == now->key) return now; if (data < now->key) return find(now->leftc, data); else return find(now->rightc, data); } // function treeminimum Nodepointer treeminimum(Nodepointer x) { while (x->leftc != NIL) { x = x->leftc; } return x; } // function treesuccerssor Nodepointer treesuccessor(Nodepointer x) { Nodepointer y; if (x->rightc != NIL) return treeminimum(x->rightc); y = x->parent; while (y != NIL && x == x->rightc) { x = y; y = y->parent; } return y; } // function treedelete void treedelete(Nodepointer z) { Nodepointer x, y; if (z->leftc == NIL || z->rightc == NIL) y = z; else y = treesuccessor(z); if (y->leftc != NIL) x = y->leftc; else x = y->rightc; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->leftc) y->parent->leftc = x; else y->parent->rightc = x; if (y != z) z->key = y->key; } // print void inorderprint(Nodepointer now) { if (now != NIL) { inorderprint(now->leftc); printf(" %d", now->key); inorderprint(now->rightc); } } void preorderprint(Nodepointer now) { if (now != NIL) { printf(" %d", now->key); preorderprint(now->leftc); preorderprint(now->rightc); } } int main() { int n, i, j, data; Nodepointer del; char str[10]; // load n scanf("%d", &n); // load commnad and key for (i = 0; i < n; i++) { scanf("%s", str); // insert if (strcmp(str, "insert") == 0) { scanf("%d", &data); insert(data); } // find else if (strcmp(str, "find") == 0) { scanf("%d", &data); if (find(root, data) != NIL) printf("yes\n"); else printf("no\n"); } // delete else if (strcmp(str, "delete") == 0) { scanf("%d", &data); del = find(root, data); treedelete(del); } // print else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
delete
105
107
105
105
0
p02285
C++
Time Limit Exceeded
#include <stdio.h> struct binary_tree { int n; int l; int r; }; binary_tree bt[100]; int now = 0, empty[100]; void push(int n, int t) { if (now == 0) { bt[0].n = n; now++; return; } if (bt[t].n < n) { if (bt[t].r == 0) { bt[t].r = empty[now]; bt[bt[t].r].n = n; now++; return; } else push(n, bt[t].r); } if (bt[t].n > n) { if (bt[t].l == 0) { bt[t].l = empty[now]; bt[bt[t].l].n = n; now++; return; } else push(n, bt[t].l); } } void dfs(int n) { printf(" %d", bt[n].n); if (bt[n].l != 0) { dfs(bt[n].l); } if (bt[n].r != 0) { dfs(bt[n].r); } } void print_sorted(int n) { if (bt[n].l != 0) { print_sorted(bt[n].l); } printf(" %d", bt[n].n); if (bt[n].r != 0) { print_sorted(bt[n].r); } } bool find(int n, int t) { if (bt[t].n == n) return true; if (bt[t].n < n) { if (bt[t].r != 0) return find(n, bt[t].r); return false; } if (bt[t].l != 0) return find(n, bt[t].l); return false; } void delete_item(int n, int t, int p) { if (bt[t].n == n) { if (bt[t].r != 0 && bt[t].l != 0) { int big = bt[t].r, bp = t; while (bt[big].l != 0) { bp = big; big = bt[big].l; } bt[t].n = bt[big].n; if (bp != t) bt[bp].l = bt[big].r; else bt[bp].r = bt[big].r; now--; empty[now] = big; bt[big].n = bt[big].l = bt[big].r = 0; return; } if (bt[t].r == 0) { if (p == -1) { now--; empty[now] = bt[t].l; bt[t] = bt[bt[t].l]; bt[empty[now]].n = bt[empty[now]].l = bt[empty[now]].r = 0; return; } if (bt[p].n > n) bt[p].l = bt[t].l; else bt[p].r = bt[t].l; now--; empty[now] = t; bt[empty[now]].n = bt[empty[now]].l = bt[empty[now]].r = 0; return; } if (bt[t].l == 0) { if (p == -1) { now--; empty[now] = bt[t].r; bt[t] = bt[bt[t].r]; bt[empty[now]].n = bt[empty[now]].l = bt[empty[now]].r = 0; return; } if (bt[p].n > n) bt[p].l = bt[t].r; else bt[p].r = bt[t].r; now--; empty[now] = t; bt[empty[now]].n = bt[empty[now]].l = bt[empty[now]].r = 0; return; } } else if (bt[t].n > n) { delete_item(n, bt[t].l, t); return; } else delete_item(n, bt[t].r, t); } int main() { int n, temp; char a[1000]; scanf("%d", &n); for (int i = 0; i < n; i++) { empty[i] = i; bt[i].n = bt[i].l = bt[i].r = 0; scanf("%s", a); if (a[0] == 'i') { scanf("%d", &temp); push(temp, 0); } if (a[0] == 'p') { print_sorted(0); printf("\n"); dfs(0); printf("\n"); } if (a[0] == 'f') { scanf("%d", &temp); if (find(temp, 0)) printf("yes\n"); else printf("no\n"); } if (a[0] == 'd') { scanf("%d", &temp); delete_item(temp, 0, -1); } } }
#include <stdio.h> struct binary_tree { int n; int l; int r; }; binary_tree bt[1000000]; int now = 0, empty[1000000]; void push(int n, int t) { if (now == 0) { bt[0].n = n; now++; return; } if (bt[t].n < n) { if (bt[t].r == 0) { bt[t].r = empty[now]; bt[bt[t].r].n = n; now++; return; } else push(n, bt[t].r); } if (bt[t].n > n) { if (bt[t].l == 0) { bt[t].l = empty[now]; bt[bt[t].l].n = n; now++; return; } else push(n, bt[t].l); } } void dfs(int n) { printf(" %d", bt[n].n); if (bt[n].l != 0) { dfs(bt[n].l); } if (bt[n].r != 0) { dfs(bt[n].r); } } void print_sorted(int n) { if (bt[n].l != 0) { print_sorted(bt[n].l); } printf(" %d", bt[n].n); if (bt[n].r != 0) { print_sorted(bt[n].r); } } bool find(int n, int t) { if (bt[t].n == n) return true; if (bt[t].n < n) { if (bt[t].r != 0) return find(n, bt[t].r); return false; } if (bt[t].l != 0) return find(n, bt[t].l); return false; } void delete_item(int n, int t, int p) { if (bt[t].n == n) { if (bt[t].r != 0 && bt[t].l != 0) { int big = bt[t].r, bp = t; while (bt[big].l != 0) { bp = big; big = bt[big].l; } bt[t].n = bt[big].n; if (bp != t) bt[bp].l = bt[big].r; else bt[bp].r = bt[big].r; now--; empty[now] = big; bt[big].n = bt[big].l = bt[big].r = 0; return; } if (bt[t].r == 0) { if (p == -1) { now--; empty[now] = bt[t].l; bt[t] = bt[bt[t].l]; bt[empty[now]].n = bt[empty[now]].l = bt[empty[now]].r = 0; return; } if (bt[p].n > n) bt[p].l = bt[t].l; else bt[p].r = bt[t].l; now--; empty[now] = t; bt[empty[now]].n = bt[empty[now]].l = bt[empty[now]].r = 0; return; } if (bt[t].l == 0) { if (p == -1) { now--; empty[now] = bt[t].r; bt[t] = bt[bt[t].r]; bt[empty[now]].n = bt[empty[now]].l = bt[empty[now]].r = 0; return; } if (bt[p].n > n) bt[p].l = bt[t].r; else bt[p].r = bt[t].r; now--; empty[now] = t; bt[empty[now]].n = bt[empty[now]].l = bt[empty[now]].r = 0; return; } } else if (bt[t].n > n) { delete_item(n, bt[t].l, t); return; } else delete_item(n, bt[t].r, t); } int main() { int n, temp; char a[1000]; scanf("%d", &n); for (int i = 0; i < n; i++) { empty[i] = i; bt[i].n = bt[i].l = bt[i].r = 0; scanf("%s", a); if (a[0] == 'i') { scanf("%d", &temp); push(temp, 0); } if (a[0] == 'p') { print_sorted(0); printf("\n"); dfs(0); printf("\n"); } if (a[0] == 'f') { scanf("%d", &temp); if (find(temp, 0)) printf("yes\n"); else printf("no\n"); } if (a[0] == 'd') { scanf("%d", &temp); delete_item(temp, 0, -1); } } }
replace
6
8
6
8
TLE
p02285
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; // NIL is NULL #define NIL NULL // Node structure struct Node { int key; struct Node *parent; struct Node *leftc; struct Node *rightc; }; // define Nodepointer typedef struct Node *Nodepointer; // root Nodepointer root; // function insert void insert(int k) { Nodepointer x, y, z; y = NIL; x = root; // make memory z = new struct Node(); z->key = k; z->leftc = NIL; z->rightc = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->leftc; else x = x->rightc; } z->parent = y; if (y == NIL) root = z; else if (z->key < y->key) y->leftc = z; else y->rightc = z; } // function search Nodepointer find(Nodepointer now, int data) { if (now == NIL || data == now->key) return now; if (data < now->key) return find(now->leftc, data); else return find(now->rightc, data); } // function treeminimum Nodepointer treeminimum(Nodepointer x) { while (x->leftc != NIL) { x = x->leftc; } return x; } // function treesuccerssor Nodepointer treesuccessor(Nodepointer x) { Nodepointer y; if (x->rightc != NIL) return treeminimum(x->rightc); y = x->parent; while (y != NIL && x == x->rightc) { x = y; y = y->parent; } return y; } // function treedelete void treedelete(Nodepointer z) { Nodepointer x, y; if (z->leftc == NIL || z->rightc == NIL) y = z; else y = treesuccessor(z); if (y->leftc != NIL) x = y->leftc; else x = y->rightc; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->leftc) y->parent->leftc = x; else y->parent->rightc = x; delete y; if (y != z) z->key = y->key; } // print void inorderprint(Nodepointer now) { if (now != NIL) { inorderprint(now->leftc); printf(" %d", now->key); inorderprint(now->rightc); } } void preorderprint(Nodepointer now) { if (now != NIL) { printf(" %d", now->key); preorderprint(now->leftc); preorderprint(now->rightc); } } int main() { int n, i, j, data; Nodepointer del; char str[10]; // load n scanf("%d", &n); // load commnad and key for (i = 0; i < n; i++) { scanf("%s", str); // insert if (strcmp(str, "insert") == 0) { scanf("%d", &data); insert(data); } // find else if (strcmp(str, "find") == 0) { scanf("%d", &data); if (find(root, data) != NIL) printf("yes\n"); else printf("no\n"); } // delete else if (strcmp(str, "delete") == 0) { scanf("%d", &data); del = find(root, data); treedelete(del); } // print else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; // NIL is NULL #define NIL NULL // Node structure struct Node { int key; struct Node *parent; struct Node *leftc; struct Node *rightc; }; // define Nodepointer typedef struct Node *Nodepointer; // root Nodepointer root; // function insert void insert(int k) { Nodepointer x, y, z; y = NIL; x = root; // make memory z = new struct Node(); z->key = k; z->leftc = NIL; z->rightc = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->leftc; else x = x->rightc; } z->parent = y; if (y == NIL) root = z; else if (z->key < y->key) y->leftc = z; else y->rightc = z; } // function search Nodepointer find(Nodepointer now, int data) { if (now == NIL || data == now->key) return now; if (data < now->key) return find(now->leftc, data); else return find(now->rightc, data); } // function treeminimum Nodepointer treeminimum(Nodepointer x) { while (x->leftc != NIL) { x = x->leftc; } return x; } // function treesuccerssor Nodepointer treesuccessor(Nodepointer x) { Nodepointer y; if (x->rightc != NIL) return treeminimum(x->rightc); y = x->parent; while (y != NIL && x == x->rightc) { x = y; y = y->parent; } return y; } // function treedelete void treedelete(Nodepointer z) { Nodepointer x, y; if (z->leftc == NIL || z->rightc == NIL) y = z; else y = treesuccessor(z); if (y->leftc != NIL) x = y->leftc; else x = y->rightc; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->leftc) y->parent->leftc = x; else y->parent->rightc = x; if (y != z) z->key = y->key; } // print void inorderprint(Nodepointer now) { if (now != NIL) { inorderprint(now->leftc); printf(" %d", now->key); inorderprint(now->rightc); } } void preorderprint(Nodepointer now) { if (now != NIL) { printf(" %d", now->key); preorderprint(now->leftc); preorderprint(now->rightc); } } int main() { int n, i, j, data; Nodepointer del; char str[10]; // load n scanf("%d", &n); // load commnad and key for (i = 0; i < n; i++) { scanf("%s", str); // insert if (strcmp(str, "insert") == 0) { scanf("%d", &data); insert(data); } // find else if (strcmp(str, "find") == 0) { scanf("%d", &data); if (find(root, data) != NIL) printf("yes\n"); else printf("no\n"); } // delete else if (strcmp(str, "delete") == 0) { scanf("%d", &data); del = find(root, data); treedelete(del); } // print else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
delete
102
103
102
102
0
p02285
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; // NIL is NULL #define NIL NULL // Node structure struct Node { int key; struct Node *parent; struct Node *leftc; struct Node *rightc; }; // define Nodepointer typedef struct Node *Nodepointer; // root Nodepointer root; // function insert void insert(int k) { Nodepointer x, y, z; y = NIL; x = root; // make memory z = new struct Node(); z->key = k; z->leftc = NIL; z->rightc = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->leftc; else x = x->rightc; } z->parent = y; if (y == NIL) root = z; else if (z->key < y->key) y->leftc = z; else y->rightc = z; } // function search Nodepointer find(Nodepointer now, int data) { if (now == NIL || data == now->key) return now; if (data < now->key) return find(now->leftc, data); else return find(now->rightc, data); } // function treeminimum Nodepointer treeminimum(Nodepointer x) { while (x->leftc != NIL) { x = x->leftc; } return x; } // function treesuccerssor Nodepointer treesuccessor(Nodepointer x) { Nodepointer y; if (x->rightc != NIL) return treeminimum(x->rightc); y = x->parent; while (y != NIL && x == x->rightc) { x = y; y = y->parent; } return y; } // function treedelete void treedelete(Nodepointer z) { Nodepointer x, y; if (z->leftc == NIL || z->rightc == NIL) y = z; else y = treesuccessor(z); if (y->leftc != NIL) x = y->leftc; else x = y->rightc; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->leftc) y->parent->leftc = x; else y->parent->rightc = x; delete z; if (y != z) z->key = y->key; } // print void inorderprint(Nodepointer now) { if (now != NIL) { inorderprint(now->leftc); printf(" %d", now->key); inorderprint(now->rightc); } } void preorderprint(Nodepointer now) { if (now != NIL) { printf(" %d", now->key); preorderprint(now->leftc); preorderprint(now->rightc); } } int main() { int n, i, j, data; Nodepointer del; char str[10]; // load n scanf("%d", &n); // load commnad and key for (i = 0; i < n; i++) { scanf("%s", str); // insert if (strcmp(str, "insert") == 0) { scanf("%d", &data); insert(data); } // find else if (strcmp(str, "find") == 0) { scanf("%d", &data); if (find(root, data) != NIL) printf("yes\n"); else printf("no\n"); } // delete else if (strcmp(str, "delete") == 0) { scanf("%d", &data); del = find(root, data); treedelete(del); } // print else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; // NIL is NULL #define NIL NULL // Node structure struct Node { int key; struct Node *parent; struct Node *leftc; struct Node *rightc; }; // define Nodepointer typedef struct Node *Nodepointer; // root Nodepointer root; // function insert void insert(int k) { Nodepointer x, y, z; y = NIL; x = root; // make memory z = new struct Node(); z->key = k; z->leftc = NIL; z->rightc = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->leftc; else x = x->rightc; } z->parent = y; if (y == NIL) root = z; else if (z->key < y->key) y->leftc = z; else y->rightc = z; } // function search Nodepointer find(Nodepointer now, int data) { if (now == NIL || data == now->key) return now; if (data < now->key) return find(now->leftc, data); else return find(now->rightc, data); } // function treeminimum Nodepointer treeminimum(Nodepointer x) { while (x->leftc != NIL) { x = x->leftc; } return x; } // function treesuccerssor Nodepointer treesuccessor(Nodepointer x) { Nodepointer y; if (x->rightc != NIL) return treeminimum(x->rightc); y = x->parent; while (y != NIL && x == x->rightc) { x = y; y = y->parent; } return y; } // function treedelete void treedelete(Nodepointer z) { Nodepointer x, y; if (z->leftc == NIL || z->rightc == NIL) y = z; else y = treesuccessor(z); if (y->leftc != NIL) x = y->leftc; else x = y->rightc; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->leftc) y->parent->leftc = x; else y->parent->rightc = x; if (y != z) z->key = y->key; } // print void inorderprint(Nodepointer now) { if (now != NIL) { inorderprint(now->leftc); printf(" %d", now->key); inorderprint(now->rightc); } } void preorderprint(Nodepointer now) { if (now != NIL) { printf(" %d", now->key); preorderprint(now->leftc); preorderprint(now->rightc); } } int main() { int n, i, j, data; Nodepointer del; char str[10]; // load n scanf("%d", &n); // load commnad and key for (i = 0; i < n; i++) { scanf("%s", str); // insert if (strcmp(str, "insert") == 0) { scanf("%d", &data); insert(data); } // find else if (strcmp(str, "find") == 0) { scanf("%d", &data); if (find(root, data) != NIL) printf("yes\n"); else printf("no\n"); } // delete else if (strcmp(str, "delete") == 0) { scanf("%d", &data); del = find(root, data); treedelete(del); } // print else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
delete
102
103
102
102
0
p02285
C++
Runtime Error
#include <assert.h> #include <iostream> #include <string> using namespace std; struct Node { int iKey_; Node *pParent_; Node *pLeftChild_; Node *pRightChild_; }; class Tree { public: Tree(int iNum) : iMaxNodeNum_(iNum), iCurNodeNum_(0) {} void setNodeValue() { for (int iCurNode_i = 0; iCurNode_i < iMaxNodeNum_; iCurNode_i++) { string sCommand; cin >> sCommand; if (sCommand[0] == 'i') { int iKey; cin >> iKey; insertNode(iKey); } else if (sCommand[0] == 'f') { int iKey; cin >> iKey; Node *findNode = isNode(iKey); if (findNode) cout << "yes" << endl; else cout << "no" << endl; } else if (sCommand[0] == 'd') { int iKey; cin >> iKey; deleteNode(iKey); } else { print(); } } } private: // sCommand = insert void insertNode(int iKey) { Node *pNewNode = new Node; //???????????? pNewNode->iKey_ = iKey; pNewNode->pParent_ = NULL; pNewNode->pLeftChild_ = NULL; pNewNode->pRightChild_ = NULL; iCurNodeNum_++; if (iCurNodeNum_ == 1) { //?????????????????????????????? pRoot_ = pNewNode; return; } searchNewNodeLocation(pNewNode); } void searchNewNodeLocation(Node *pNewNode) { Node *pCurNode = pRoot_; Node *pCurParentNode = NULL; while (pCurNode != NULL) { pCurParentNode = pCurNode; if (pNewNode->iKey_ < pCurNode->iKey_) pCurNode = pCurNode->pLeftChild_; else pCurNode = pCurNode->pRightChild_; } pNewNode->pParent_ = pCurParentNode; if (pNewNode->iKey_ < pCurParentNode->iKey_) pCurParentNode->pLeftChild_ = pNewNode; else pCurParentNode->pRightChild_ = pNewNode; } // sCommand = find Node *isNode(int iKey) { Node *curNode = pRoot_; while (curNode != NULL) { if (curNode->iKey_ == iKey) return curNode; if (iKey < curNode->iKey_) { curNode = curNode->pLeftChild_; } else { curNode = curNode->pRightChild_; } } return NULL; } // sCommand = delete void deleteNode(int iKey) { Node *wantToDelete_node = isNode(iKey); if (wantToDelete_node == NULL) return; Node *actuallyDelete_node; Node *actuallyDeleteNode_child; if (wantToDelete_node->pLeftChild_ == NULL || wantToDelete_node->pRightChild_ == NULL) actuallyDelete_node = wantToDelete_node; else actuallyDelete_node = conputeNextNodeInOrder(wantToDelete_node); if (actuallyDelete_node->pLeftChild_ != NULL) actuallyDeleteNode_child = actuallyDelete_node->pLeftChild_; else actuallyDeleteNode_child = actuallyDelete_node->pRightChild_; if (actuallyDeleteNode_child != NULL) actuallyDeleteNode_child->pParent_ = actuallyDelete_node->pParent_; if (actuallyDelete_node->pParent_ == NULL) pRoot_ = actuallyDeleteNode_child; else { Node *parent = actuallyDelete_node->pParent_; if (actuallyDelete_node == parent->pLeftChild_) parent->pLeftChild_ = actuallyDeleteNode_child; else parent->pRightChild_ = actuallyDeleteNode_child; } if (actuallyDelete_node != wantToDelete_node) wantToDelete_node->iKey_ = actuallyDeleteNode_child->iKey_; delete actuallyDelete_node; } Node *conputeNextNodeInOrder(Node *wantNode) { assert(wantNode->pRightChild_ != NULL); Node *child_node = wantNode->pRightChild_; while (child_node->pLeftChild_ != NULL) { child_node = child_node->pLeftChild_; } return child_node; } // sCommand = print void print() { printWidthInorder(pRoot_); cout << endl; printWidthPreorder(pRoot_); cout << endl; } void printWidthPreorder(Node *pN) { if (pN == NULL) return; cout << " " << pN->iKey_; printWidthPreorder(pN->pLeftChild_); printWidthPreorder(pN->pRightChild_); } void printWidthInorder(Node *pN) { if (pN == NULL) return; printWidthInorder(pN->pLeftChild_); cout << " " << pN->iKey_; printWidthInorder(pN->pRightChild_); } Node *pRoot_; int iCurNodeNum_; const int iMaxNodeNum_; }; int main() { int iNodeNum; cin >> iNodeNum; //????????? Tree trNodeTree(iNodeNum); //???????????§??\???????????? trNodeTree.setNodeValue(); return 0; }
#include <assert.h> #include <iostream> #include <string> using namespace std; struct Node { int iKey_; Node *pParent_; Node *pLeftChild_; Node *pRightChild_; }; class Tree { public: Tree(int iNum) : iMaxNodeNum_(iNum), iCurNodeNum_(0) {} void setNodeValue() { for (int iCurNode_i = 0; iCurNode_i < iMaxNodeNum_; iCurNode_i++) { string sCommand; cin >> sCommand; if (sCommand[0] == 'i') { int iKey; cin >> iKey; insertNode(iKey); } else if (sCommand[0] == 'f') { int iKey; cin >> iKey; Node *findNode = isNode(iKey); if (findNode) cout << "yes" << endl; else cout << "no" << endl; } else if (sCommand[0] == 'd') { int iKey; cin >> iKey; deleteNode(iKey); } else { print(); } } } private: // sCommand = insert void insertNode(int iKey) { Node *pNewNode = new Node; //???????????? pNewNode->iKey_ = iKey; pNewNode->pParent_ = NULL; pNewNode->pLeftChild_ = NULL; pNewNode->pRightChild_ = NULL; iCurNodeNum_++; if (iCurNodeNum_ == 1) { //?????????????????????????????? pRoot_ = pNewNode; return; } searchNewNodeLocation(pNewNode); } void searchNewNodeLocation(Node *pNewNode) { Node *pCurNode = pRoot_; Node *pCurParentNode = NULL; while (pCurNode != NULL) { pCurParentNode = pCurNode; if (pNewNode->iKey_ < pCurNode->iKey_) pCurNode = pCurNode->pLeftChild_; else pCurNode = pCurNode->pRightChild_; } pNewNode->pParent_ = pCurParentNode; if (pNewNode->iKey_ < pCurParentNode->iKey_) pCurParentNode->pLeftChild_ = pNewNode; else pCurParentNode->pRightChild_ = pNewNode; } // sCommand = find Node *isNode(int iKey) { Node *curNode = pRoot_; while (curNode != NULL) { if (curNode->iKey_ == iKey) return curNode; if (iKey < curNode->iKey_) { curNode = curNode->pLeftChild_; } else { curNode = curNode->pRightChild_; } } return NULL; } // sCommand = delete void deleteNode(int iKey) { Node *wantToDelete_node = isNode(iKey); if (wantToDelete_node == NULL) return; Node *actuallyDelete_node; Node *actuallyDeleteNode_child; if (wantToDelete_node->pLeftChild_ == NULL || wantToDelete_node->pRightChild_ == NULL) actuallyDelete_node = wantToDelete_node; else actuallyDelete_node = conputeNextNodeInOrder(wantToDelete_node); if (actuallyDelete_node->pLeftChild_ != NULL) actuallyDeleteNode_child = actuallyDelete_node->pLeftChild_; else actuallyDeleteNode_child = actuallyDelete_node->pRightChild_; if (actuallyDeleteNode_child != NULL) actuallyDeleteNode_child->pParent_ = actuallyDelete_node->pParent_; if (actuallyDelete_node->pParent_ == NULL) pRoot_ = actuallyDeleteNode_child; else { Node *parent = actuallyDelete_node->pParent_; if (actuallyDelete_node == parent->pLeftChild_) parent->pLeftChild_ = actuallyDeleteNode_child; else parent->pRightChild_ = actuallyDeleteNode_child; } if (actuallyDelete_node != wantToDelete_node) wantToDelete_node->iKey_ = actuallyDelete_node->iKey_; delete actuallyDelete_node; } Node *conputeNextNodeInOrder(Node *wantNode) { assert(wantNode->pRightChild_ != NULL); Node *child_node = wantNode->pRightChild_; while (child_node->pLeftChild_ != NULL) { child_node = child_node->pLeftChild_; } return child_node; } // sCommand = print void print() { printWidthInorder(pRoot_); cout << endl; printWidthPreorder(pRoot_); cout << endl; } void printWidthPreorder(Node *pN) { if (pN == NULL) return; cout << " " << pN->iKey_; printWidthPreorder(pN->pLeftChild_); printWidthPreorder(pN->pRightChild_); } void printWidthInorder(Node *pN) { if (pN == NULL) return; printWidthInorder(pN->pLeftChild_); cout << " " << pN->iKey_; printWidthInorder(pN->pRightChild_); } Node *pRoot_; int iCurNodeNum_; const int iMaxNodeNum_; }; int main() { int iNodeNum; cin >> iNodeNum; //????????? Tree trNodeTree(iNodeNum); //???????????§??\???????????? trNodeTree.setNodeValue(); return 0; }
replace
125
126
125
126
0
p02285
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; // NIL is NULL #define NIL NULL // Node structure struct Node { int key; struct Node *parent; struct Node *leftc; struct Node *rightc; }; // define Nodepointer typedef struct Node *Nodepointer; // root Nodepointer root; // function insert void insert(int k) { Nodepointer x, y, z; y = NIL; x = root; // make memory z = new struct Node(); z->key = k; z->leftc = NIL; z->rightc = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->leftc; else x = x->rightc; } z->parent = y; if (y == NIL) root = z; else if (z->key < y->key) y->leftc = z; else y->rightc = z; } // function search Nodepointer find(Nodepointer now, int data) { if (now == NIL || data == now->key) return now; if (data < now->key) return find(now->leftc, data); else return find(now->rightc, data); } // function treeminimum Nodepointer treeminimum(Nodepointer x) { while (x->leftc != NIL) { x = x->leftc; } return x; } // function treesuccerssor Nodepointer treesuccessor(Nodepointer x) { Nodepointer y; if (x->rightc != NIL) return treeminimum(x->rightc); y = x->parent; while (y != NIL && x == x->rightc) { x = y; y = y->parent; } return y; } // function treedelete void treedelete(Nodepointer z) { Nodepointer x, y; if (z->leftc == NIL || z->rightc == NIL) y = z; else y = treesuccessor(z); if (y->leftc != NIL) x = y->leftc; else x = y->rightc; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->leftc) y->parent->leftc = x; else y->parent->rightc = x; if (y != z) z->key = y->key; free(z); } // print void inorderprint(Nodepointer now) { if (now != NIL) { inorderprint(now->leftc); printf(" %d", now->key); inorderprint(now->rightc); } } void preorderprint(Nodepointer now) { if (now != NIL) { printf(" %d", now->key); preorderprint(now->leftc); preorderprint(now->rightc); } } int main() { int n, i, j, data; Nodepointer del; char str[10]; // load n scanf("%d", &n); // load commnad and key for (i = 0; i < n; i++) { scanf("%s", str); // insert if (strcmp(str, "insert") == 0) { scanf("%d", &data); insert(data); } // find else if (strcmp(str, "find") == 0) { scanf("%d", &data); if (find(root, data) != NIL) printf("yes\n"); else printf("no\n"); } // delete else if (strcmp(str, "delete") == 0) { scanf("%d", &data); del = find(root, data); treedelete(del); } // print else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; // NIL is NULL #define NIL NULL // Node structure struct Node { int key; struct Node *parent; struct Node *leftc; struct Node *rightc; }; // define Nodepointer typedef struct Node *Nodepointer; // root Nodepointer root; // function insert void insert(int k) { Nodepointer x, y, z; y = NIL; x = root; // make memory z = new struct Node(); z->key = k; z->leftc = NIL; z->rightc = NIL; while (x != NIL) { y = x; if (z->key < x->key) x = x->leftc; else x = x->rightc; } z->parent = y; if (y == NIL) root = z; else if (z->key < y->key) y->leftc = z; else y->rightc = z; } // function search Nodepointer find(Nodepointer now, int data) { if (now == NIL || data == now->key) return now; if (data < now->key) return find(now->leftc, data); else return find(now->rightc, data); } // function treeminimum Nodepointer treeminimum(Nodepointer x) { while (x->leftc != NIL) { x = x->leftc; } return x; } // function treesuccerssor Nodepointer treesuccessor(Nodepointer x) { Nodepointer y; if (x->rightc != NIL) return treeminimum(x->rightc); y = x->parent; while (y != NIL && x == x->rightc) { x = y; y = y->parent; } return y; } // function treedelete void treedelete(Nodepointer z) { Nodepointer x, y; if (z->leftc == NIL || z->rightc == NIL) y = z; else y = treesuccessor(z); if (y->leftc != NIL) x = y->leftc; else x = y->rightc; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->leftc) y->parent->leftc = x; else y->parent->rightc = x; if (y != z) z->key = y->key; } // print void inorderprint(Nodepointer now) { if (now != NIL) { inorderprint(now->leftc); printf(" %d", now->key); inorderprint(now->rightc); } } void preorderprint(Nodepointer now) { if (now != NIL) { printf(" %d", now->key); preorderprint(now->leftc); preorderprint(now->rightc); } } int main() { int n, i, j, data; Nodepointer del; char str[10]; // load n scanf("%d", &n); // load commnad and key for (i = 0; i < n; i++) { scanf("%s", str); // insert if (strcmp(str, "insert") == 0) { scanf("%d", &data); insert(data); } // find else if (strcmp(str, "find") == 0) { scanf("%d", &data); if (find(root, data) != NIL) printf("yes\n"); else printf("no\n"); } // delete else if (strcmp(str, "delete") == 0) { scanf("%d", &data); del = find(root, data); treedelete(del); } // print else if (strcmp(str, "print") == 0) { inorderprint(root); cout << endl; preorderprint(root); cout << endl; } } return 0; }
delete
104
105
104
104
0
p02286
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; /* struct Node{ Node *right, *left; int key, priority; }; Node* node(int k, int p){ Node *newNode = new Node();//(Node *)malloc(sizeof(Node)); newNode->right = NULL; newNode->left = NULL; newNode->key = k; newNode->priority = p; return newNode; } */ class Node { public: Node *right, *left; int key, priority; Node(int k, int p) { right = NULL; left = NULL; key = k; priority = p; } }; Node *rightRotate(Node *t) { Node *s = t->left; t->left = s->right; s->right = t; return s; // the new root of subtree } Node *leftRotate(Node *t) { Node *s = t->right; t->right = s->left; s->left = t; return s; // the new root of subtree } Node *insertT(Node *t, int k, int p) { // when you reach the leaf // if(t == NULL) return node(k, p); //create a new Node if (t == NULL) return new Node(k, p); // create a new Node // ignore duplicated keys if (k == t->key) return t; if (k < t->key) { // move to the left child // update the pointer to the left child t->left = insertT(t->left, k, p); // if the left child has higher priority if (t->priority < t->left->priority) t = rightRotate(t); } else { // move to the right child // update the pointer t the right child t->right = insertT(t->right, k, p); // if the right child has higher priority if (t->priority < t->right->priority) t = leftRotate(t); } return t; } Node *find(Node *t, int k) { while (t != NULL && k != t->key) { // leafじゃない && みつかっていない if (k < t->key) t = t->left; else t = t->right; } return t; } Node *deleteNode(Node *t, int k) { if (t == NULL) return NULL; // if t is the targer node if (k == t->key) { // if t is a leaf if (t->left == NULL && t->right == NULL) return NULL; // if t has only the right child else if (t->left == NULL) t = leftRotate(t); // if t has only the left child else if (t->right == NULL) t = rightRotate(t); // if t has both the left and right child else { // pull up the child with higher priority if (t->left->priority > t->right->priority) t = rightRotate(t); else t = leftRotate(t); } return deleteNode(t, k); } // search the targer recursively if (k < t->key) t->left = deleteNode(t->left, k); else t->right = deleteNode(t->right, k); return t; } void inorder(Node *t) { if (t == NULL) return; inorder(t->left); printf(" %d", t->key); inorder(t->right); } void preorder(Node *t) { if (t == NULL) return; printf(" %d", t->key); preorder(t->left); preorder(t->right); } void print(Node *t) { inorder(t); printf("\n"); preorder(t); printf("\n"); } int main() { int n, k, p; string com; Node *t; // root cin >> n; for (int i = 0; i < n; ++i) { cin >> com; if (com == "insert") { cin >> k >> p; t = insertT(t, k, p); } else if (com == "delete") { cin >> k; t = deleteNode(t, k); } else if (com == "print") { print(t); } else if (com == "find") { cin >> k; Node *n = find(t, k); if (n != NULL) cout << "yes" << endl; else cout << "no" << endl; } } }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; /* struct Node{ Node *right, *left; int key, priority; }; Node* node(int k, int p){ Node *newNode = new Node();//(Node *)malloc(sizeof(Node)); newNode->right = NULL; newNode->left = NULL; newNode->key = k; newNode->priority = p; return newNode; } */ class Node { public: Node *right, *left; int key, priority; Node(int k, int p) { right = NULL; left = NULL; key = k; priority = p; } }; Node *rightRotate(Node *t) { Node *s = t->left; t->left = s->right; s->right = t; return s; // the new root of subtree } Node *leftRotate(Node *t) { Node *s = t->right; t->right = s->left; s->left = t; return s; // the new root of subtree } Node *insertT(Node *t, int k, int p) { // when you reach the leaf // if(t == NULL) return node(k, p); //create a new Node if (t == NULL) return new Node(k, p); // create a new Node // ignore duplicated keys if (k == t->key) return t; if (k < t->key) { // move to the left child // update the pointer to the left child t->left = insertT(t->left, k, p); // if the left child has higher priority if (t->priority < t->left->priority) t = rightRotate(t); } else { // move to the right child // update the pointer t the right child t->right = insertT(t->right, k, p); // if the right child has higher priority if (t->priority < t->right->priority) t = leftRotate(t); } return t; } Node *find(Node *t, int k) { while (t != NULL && k != t->key) { // leafじゃない && みつかっていない if (k < t->key) t = t->left; else t = t->right; } return t; } Node *deleteNode(Node *t, int k) { if (t == NULL) return NULL; // if t is the targer node if (k == t->key) { // if t is a leaf if (t->left == NULL && t->right == NULL) return NULL; // if t has only the right child else if (t->left == NULL) t = leftRotate(t); // if t has only the left child else if (t->right == NULL) t = rightRotate(t); // if t has both the left and right child else { // pull up the child with higher priority if (t->left->priority > t->right->priority) t = rightRotate(t); else t = leftRotate(t); } return deleteNode(t, k); } // search the targer recursively if (k < t->key) t->left = deleteNode(t->left, k); else t->right = deleteNode(t->right, k); return t; } void inorder(Node *t) { if (t == NULL) return; inorder(t->left); printf(" %d", t->key); inorder(t->right); } void preorder(Node *t) { if (t == NULL) return; printf(" %d", t->key); preorder(t->left); preorder(t->right); } void print(Node *t) { inorder(t); printf("\n"); preorder(t); printf("\n"); } int main() { int n, k, p; string com; Node *t = NULL; // root cin >> n; for (int i = 0; i < n; ++i) { cin >> com; if (com == "insert") { cin >> k >> p; t = insertT(t, k, p); } else if (com == "delete") { cin >> k; t = deleteNode(t, k); } else if (com == "print") { print(t); } else if (com == "find") { cin >> k; Node *n = find(t, k); if (n != NULL) cout << "yes" << endl; else cout << "no" << endl; } } }
replace
145
146
145
146
-11
p02287
C++
Runtime Error
#include <iostream> using namespace std; typedef long long signed int ll; ll a[251]; int h; void rk(int n) { if (a[n * 2 + 1]) cout << "right key = " << a[n * 2 + 1] << ", "; } void lk(int n) { if (a[n * 2]) cout << "left key = " << a[n * 2] << ", "; } void pk(int n) { if (a[n / 2]) cout << "parent key = " << a[n / 2] << ", "; } void print(int n) { cout << "node " << n << ": key = " << a[n] << ", "; pk(n); lk(n); rk(n); cout << endl; } int main() { cin >> h; for (int i = 1; i <= h; i++) cin >> a[i]; for (int i = 1; i <= h; i++) print(i); return 0; }
#include <iostream> using namespace std; typedef long long signed int ll; ll a[510]; int h; void rk(int n) { if (a[n * 2 + 1]) cout << "right key = " << a[n * 2 + 1] << ", "; } void lk(int n) { if (a[n * 2]) cout << "left key = " << a[n * 2] << ", "; } void pk(int n) { if (a[n / 2]) cout << "parent key = " << a[n / 2] << ", "; } void print(int n) { cout << "node " << n << ": key = " << a[n] << ", "; pk(n); lk(n); rk(n); cout << endl; } int main() { cin >> h; for (int i = 1; i <= h; i++) cin >> a[i]; for (int i = 1; i <= h; i++) print(i); return 0; }
replace
6
7
6
7
0
p02287
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct Node { int key; int id, parent, left, right; }; Node A[251]; void heap(Node A[], int H) { for (int i = 1; i <= H; i++) { if (i == 1) { A[i].left = A[2 * i].key; A[i].right = A[2 * i + 1].key; } else { A[i].parent = A[i / 2].key; A[i].left = A[2 * i].key; A[i].right = A[2 * i + 1].key; } } } int main() { int H = 0; cin >> H; for (int i = 1; i <= H; i++) { cin >> A[i].key; A[i].id = i; } heap(A, H); for (int i = 1; i <= H; i++) { cout << "node " << A[i].id << ": key = " << A[i].key << ", "; if (i == 1) { cout << "left key = " << A[i].left << ", right key = " << A[i].right << ", " << endl; } else { if (A[i].left == 0 && A[i].right != 0) { cout << "parent key = " << A[i].parent << ", right key = " << A[i].right << ", " << endl; } else if (A[i].left != 0 && A[i].right == 0) { cout << "parent key = " << A[i].parent << ", left key = " << A[i].left << ", " << endl; } else if (A[i].left == 0 && A[i].right == 0) { cout << "parent key = " << A[i].parent << ", " << endl; } else { cout << "parent key = " << A[i].parent << ", left key = " << A[i].left << ", right key = " << A[i].right << ", " << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Node { int key; int id, parent, left, right; }; Node A[100000]; void heap(Node A[], int H) { for (int i = 1; i <= H; i++) { if (i == 1) { A[i].left = A[2 * i].key; A[i].right = A[2 * i + 1].key; } else { A[i].parent = A[i / 2].key; A[i].left = A[2 * i].key; A[i].right = A[2 * i + 1].key; } } } int main() { int H = 0; cin >> H; for (int i = 1; i <= H; i++) { cin >> A[i].key; A[i].id = i; } heap(A, H); for (int i = 1; i <= H; i++) { cout << "node " << A[i].id << ": key = " << A[i].key << ", "; if (i == 1) { cout << "left key = " << A[i].left << ", right key = " << A[i].right << ", " << endl; } else { if (A[i].left == 0 && A[i].right != 0) { cout << "parent key = " << A[i].parent << ", right key = " << A[i].right << ", " << endl; } else if (A[i].left != 0 && A[i].right == 0) { cout << "parent key = " << A[i].parent << ", left key = " << A[i].left << ", " << endl; } else if (A[i].left == 0 && A[i].right == 0) { cout << "parent key = " << A[i].parent << ", " << endl; } else { cout << "parent key = " << A[i].parent << ", left key = " << A[i].left << ", right key = " << A[i].right << ", " << endl; } } } return 0; }
replace
7
8
7
8
0
p02288
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <stdio.h> #include <string> #include <vector> #define MAX 1000000000000000000LL long long inf = 1000000007; using namespace std; int H, A[200001]; void maxheapify(int i) { int l, r, largest; l = 2 * i; r = 2 * i + 1; if (l <= H && A[l] > A[i]) largest = l; else largest = i; if (r <= H && A[r] > A[largest]) largest = r; if (largest != i) { swap(A[i], A[largest]); maxheapify(largest); } } int main() { cin >> H; for (int i = 1; i <= H; i++) { cin >> A[i]; } for (int i = H / 2; i >= 1; i--) { maxheapify(i); } for (int i = 1; i <= H; i++) { cout << " " << A[i]; } cout << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <stdio.h> #include <string> #include <vector> #define MAX 1000000000000000000LL long long inf = 1000000007; using namespace std; int H, A[2000001]; void maxheapify(int i) { int l, r, largest; l = 2 * i; r = 2 * i + 1; if (l <= H && A[l] > A[i]) largest = l; else largest = i; if (r <= H && A[r] > A[largest]) largest = r; if (largest != i) { swap(A[i], A[largest]); maxheapify(largest); } } int main() { cin >> H; for (int i = 1; i <= H; i++) { cin >> A[i]; } for (int i = H / 2; i >= 1; i--) { maxheapify(i); } for (int i = 1; i <= H; i++) { cout << " " << A[i]; } cout << endl; return 0; }
replace
10
11
10
11
0
p02288
C++
Runtime Error
#define scanf_s scanf #include <algorithm> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; #define MAX 500 // 000 // #define MAX_ 1000 static int H; void maxHeapify(long long int A[], int i) { int l = i * 2, r = i * 2 + 1, largest; long long int x; if (l <= H && A[l] > A[i]) largest = l; else largest = i; if (r <= H && A[r] > A[largest]) largest = r; if (largest != i) { x = A[i]; A[i] = A[largest]; A[largest] = x; maxHeapify(A, largest); } } void buildMaxHeap(long long int A[]) { for (int i = H / 2; i >= 1; --i) { maxHeapify(A, i); } } int main(void) { long long int Heaps[MAX + 1]; scanf_s("%d", &H); for (int i = 1; i <= H; ++i) { scanf_s("%lld", &Heaps[i]); } buildMaxHeap(Heaps); for (int i = 1; i <= H; ++i) { printf(" %lld", Heaps[i]); } printf("\n"); }
#define scanf_s scanf #include <algorithm> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; #define MAX 500000 // #define MAX_ 1000 static int H; void maxHeapify(long long int A[], int i) { int l = i * 2, r = i * 2 + 1, largest; long long int x; if (l <= H && A[l] > A[i]) largest = l; else largest = i; if (r <= H && A[r] > A[largest]) largest = r; if (largest != i) { x = A[i]; A[i] = A[largest]; A[largest] = x; maxHeapify(A, largest); } } void buildMaxHeap(long long int A[]) { for (int i = H / 2; i >= 1; --i) { maxHeapify(A, i); } } int main(void) { long long int Heaps[MAX + 1]; scanf_s("%d", &H); for (int i = 1; i <= H; ++i) { scanf_s("%lld", &Heaps[i]); } buildMaxHeap(Heaps); for (int i = 1; i <= H; ++i) { printf(" %lld", Heaps[i]); } printf("\n"); }
replace
13
14
13
14
0
p02288
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define MAX_V 250 int heap[MAX_V]{0}; int left(int i) { return 2 * i + 1; } int right(int i) { return 2 * i + 2; } int parent(int i) { return (i - 1) / 2; } bool has_left(int size, int i) { return size > left(i); } bool has_right(int size, int i) { return size > right(i); } void max_heapify(int n, int i) { int l, r, largest; l = left(i); r = right(i); if (has_left(n, i) && heap[l] > heap[i]) largest = l; else largest = i; if (has_right(n, i) && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); max_heapify(n, largest); } } void build_max_heap(int size) { for (int i = (size - 1) / 2; i >= 0; i--) max_heapify(size, i); } int main() { int n; scanf("%d", &n); REP(i, n) scanf("%d", &heap[i]); build_max_heap(n); REP(i, n) printf(" %d", heap[i]); puts(""); }
#include <cstdio> #include <iostream> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define MAX_V 500000 int heap[MAX_V]{0}; int left(int i) { return 2 * i + 1; } int right(int i) { return 2 * i + 2; } int parent(int i) { return (i - 1) / 2; } bool has_left(int size, int i) { return size > left(i); } bool has_right(int size, int i) { return size > right(i); } void max_heapify(int n, int i) { int l, r, largest; l = left(i); r = right(i); if (has_left(n, i) && heap[l] > heap[i]) largest = l; else largest = i; if (has_right(n, i) && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); max_heapify(n, largest); } } void build_max_heap(int size) { for (int i = (size - 1) / 2; i >= 0; i--) max_heapify(size, i); } int main() { int n; scanf("%d", &n); REP(i, n) scanf("%d", &heap[i]); build_max_heap(n); REP(i, n) printf(" %d", heap[i]); puts(""); }
replace
4
5
4
5
0
p02288
C++
Runtime Error
#include <iostream> #include <stdio.h> #define MAX 1000000 int n, H[MAX]; void maxHeap(int i) { int left = i * 2; int right = i * 2 + 1; int max = left; if (right <= n) max = H[left] >= H[right] ? left : right; max = H[i] >= H[max] ? i : max; if (max != i) { std::swap(H[i], H[max]); maxHeap(max); } } int main() { int key; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &key); H[i] = key; } for (int i = n / 2; i >= 1; i--) { maxHeap(i); } for (int i = 1; i <= n; i++) { printf(" %d", H[i]); } printf("\n"); return 0; }
#include <iostream> #include <stdio.h> #define MAX 1000000 int n, H[MAX]; void maxHeap(int i) { int left = i * 2; int right = i * 2 + 1; if (left > n) return; int max = left; if (right <= n) max = H[left] >= H[right] ? left : right; max = H[i] >= H[max] ? i : max; if (max != i) { std::swap(H[i], H[max]); maxHeap(max); } } int main() { int key; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &key); H[i] = key; } for (int i = n / 2; i >= 1; i--) { maxHeap(i); } for (int i = 1; i <= n; i++) { printf(" %d", H[i]); } printf("\n"); return 0; }
insert
10
10
10
13
0
p02288
C++
Runtime Error
#include <iostream> using namespace std; const int MAX = 100000; int H, i, A[MAX + 1]; int parent(int i) { return i / 2; } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void maxHeapify(int *A, int i) { int l = left(i); int r = right(i); int largest; if (l <= H && A[l] > A[i]) { largest = l; } else { largest = i; } if (r <= H && A[r] > A[largest]) { largest = r; } if (largest != i) { int tmp = A[i]; A[i] = A[largest]; A[largest] = tmp; maxHeapify(A, largest); } } int main() { ios::sync_with_stdio(false); cin >> H; for (i = 1; i <= H; i++) cin >> A[i]; for (i = H / 2; i >= 1; i--) { maxHeapify(A, i); } for (i = 1; i <= H; i++) cout << " " << A[i]; cout << endl; }
#include <iostream> using namespace std; const int MAX = 500000; int H, i, A[MAX + 1]; int parent(int i) { return i / 2; } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void maxHeapify(int *A, int i) { int l = left(i); int r = right(i); int largest; if (l <= H && A[l] > A[i]) { largest = l; } else { largest = i; } if (r <= H && A[r] > A[largest]) { largest = r; } if (largest != i) { int tmp = A[i]; A[i] = A[largest]; A[largest] = tmp; maxHeapify(A, largest); } } int main() { ios::sync_with_stdio(false); cin >> H; for (i = 1; i <= H; i++) cin >> A[i]; for (i = H / 2; i >= 1; i--) { maxHeapify(A, i); } for (i = 1; i <= H; i++) cout << " " << A[i]; cout << endl; }
replace
3
4
3
4
0
p02288
C++
Runtime Error
#include <algorithm> #include <cassert> #include <iostream> #include <vector> int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } void maxHeapify(std::vector<int> &A, int i) { int H = A.size() + 1; int l = left(i); int r = right(i); int largest; if (l <= H && A[l] > A[i]) { largest = l; } else { largest = i; } if (r <= H && A[r] > A[largest]) { largest = r; } if (largest != i) { std::swap(A[i], A[largest]); maxHeapify(A, largest); } } void buildMaxHeap(std::vector<int> &A) { int H = A.size() + 1; for (std::size_t i = H / 2; i; --i) { maxHeapify(A, i); } } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int H; std::cin >> H; std::vector<int> A(H + 1); for (std::size_t i = 1; i <= H; ++i) { int Value; std::cin >> Value; A[i] = Value; } buildMaxHeap(A); for (std::size_t i = 1; i <= H; ++i) { std::cout << " " << A[i]; } std::cout << std::endl; return 0; }
#include <algorithm> #include <cassert> #include <iostream> #include <vector> int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } void maxHeapify(std::vector<int> &A, int i) { int H = A.size(); int l = left(i); int r = right(i); int largest; if (l <= H && A[l] > A[i]) { largest = l; } else { largest = i; } if (r <= H && A[r] > A[largest]) { largest = r; } if (largest != i) { std::swap(A[i], A[largest]); maxHeapify(A, largest); } } void buildMaxHeap(std::vector<int> &A) { int H = A.size() + 1; for (std::size_t i = H / 2; i; --i) { maxHeapify(A, i); } } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int H; std::cin >> H; std::vector<int> A(H + 1); for (std::size_t i = 1; i <= H; ++i) { int Value; std::cin >> Value; A[i] = Value; } buildMaxHeap(A); for (std::size_t i = 1; i <= H; ++i) { std::cout << " " << A[i]; } std::cout << std::endl; return 0; }
replace
8
9
8
9
0
p02288
C++
Runtime Error
#include <iostream> #include <stdio.h> #define abs(a) (a < 0 ? -(a) : a) #define square(x) ((x) * (x)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) #define swap(a, b) ((a != b) ? (a += b, b = a - b, a -= b) : 0) #define This #define True true #define False false #define null Null using namespace std; int N; void maxHeapify(int num[], int i) { int left = 2 * i; int right = 2 * i + 1; int largest; if (left <= N && num[left] > num[i]) { largest = left; } else { largest = i; } if (right <= N && num[right] > num[largest]) { largest = right; } if (largest != i) { swap(num[i], num[largest]); maxHeapify(num, largest); } } void buildMaxHeap(int num[]) { for (int i = N / 2; i >= 1; i--) { maxHeapify(num, i); } for (int i = 1; i <= N; i++) { cout << " " << num[i]; } cout << endl; } int main() { int num[1000]; cin >> N; for (int i = 1; i <= N; i++) { cin >> num[i]; } buildMaxHeap(num); }
#include <iostream> #include <stdio.h> #define abs(a) (a < 0 ? -(a) : a) #define square(x) ((x) * (x)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) #define swap(a, b) ((a != b) ? (a += b, b = a - b, a -= b) : 0) #define This #define True true #define False false #define null Null using namespace std; int N; void maxHeapify(int num[], int i) { int left = 2 * i; int right = 2 * i + 1; int largest; if (left <= N && num[left] > num[i]) { largest = left; } else { largest = i; } if (right <= N && num[right] > num[largest]) { largest = right; } if (largest != i) { swap(num[i], num[largest]); maxHeapify(num, largest); } } void buildMaxHeap(int num[]) { for (int i = N / 2; i >= 1; i--) { maxHeapify(num, i); } for (int i = 1; i <= N; i++) { cout << " " << num[i]; } cout << endl; } int main() { int num[1000000]; cin >> N; for (int i = 1; i <= N; i++) { cin >> num[i]; } buildMaxHeap(num); }
replace
45
46
45
46
0
p02288
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; void f(const int, const int, int *); int main() { int N; int data[251]; cin >> N; for (int i = 1; i <= N; i++) { cin >> data[i]; } for (int i = N / 2; i > 0; i--) { f(N, i, data); } for (int i = 1; i <= N; i++) { cout << " " << data[i]; } cout << endl; } void f(const int N, const int d, int *data) { int l = d * 2; int r = d * 2 + 1; int largest = d; if (l <= N && data[d] < data[l]) { largest = l; } if (r <= N && data[largest] < data[r]) { largest = r; } if (largest != d) { swap(data[d], data[largest]); f(N, largest, data); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; void f(const int, const int, int *); int main() { int N; int data[500001]; cin >> N; for (int i = 1; i <= N; i++) { cin >> data[i]; } for (int i = N / 2; i > 0; i--) { f(N, i, data); } for (int i = 1; i <= N; i++) { cout << " " << data[i]; } cout << endl; } void f(const int N, const int d, int *data) { int l = d * 2; int r = d * 2 + 1; int largest = d; if (l <= N && data[d] < data[l]) { largest = l; } if (r <= N && data[largest] < data[r]) { largest = r; } if (largest != d) { swap(data[d], data[largest]); f(N, largest, data); } }
replace
20
21
20
21
0
p02288
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; int D[10000]; int H[10000]; void printResult(int i, int n) { printf("node %d: key = %d, ", i, H[i]); if (i != 1) printf("parent key = %d, ", H[i / 2]); if (2 * i <= n) printf("left key = %d, ", H[2 * i]); if (2 * i + 1 <= n) printf("right key = %d, ", H[2 * i + 1]); printf("\n"); } void maxHeapify(int i, int n) { int l = 2 * i; int r = 2 * i + 1; int largest = i; if (l <= n and H[l] >= H[i]) largest = l; if (r <= n and H[r] >= H[largest]) largest = r; if (largest != i) { swap(H[i], H[largest]); maxHeapify(largest, n); } } void buildMaxHeap(int n) { for (int i = n / 2; i > 0; i--) maxHeapify(i, n); } int main() { int n, i, x; string com; cin >> n; for (i = 1; i <= n; i++) { cin >> H[i]; } buildMaxHeap(n); for (i = 1; i <= n; i++) { printf(" %d", H[i]); } printf("\n"); return 0; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; int H[500001]; void printResult(int i, int n) { printf("node %d: key = %d, ", i, H[i]); if (i != 1) printf("parent key = %d, ", H[i / 2]); if (2 * i <= n) printf("left key = %d, ", H[2 * i]); if (2 * i + 1 <= n) printf("right key = %d, ", H[2 * i + 1]); printf("\n"); } void maxHeapify(int i, int n) { int l = 2 * i; int r = 2 * i + 1; int largest = i; if (l <= n and H[l] >= H[i]) largest = l; if (r <= n and H[r] >= H[largest]) largest = r; if (largest != i) { swap(H[i], H[largest]); maxHeapify(largest, n); } } void buildMaxHeap(int n) { for (int i = n / 2; i > 0; i--) maxHeapify(i, n); } int main() { int n, i, x; string com; cin >> n; for (i = 1; i <= n; i++) { cin >> H[i]; } buildMaxHeap(n); for (i = 1; i <= n; i++) { printf(" %d", H[i]); } printf("\n"); return 0; }
replace
6
8
6
7
0
p02288
C++
Time Limit Exceeded
#include <cstdio> #include <iostream> #define MAX 2000000 using namespace std; int H, a[MAX + 1]; void max(int i) { int l, r, large; l = 2 * i; r = 2 * i + 1; if (l <= H && a[l] > a[i]) large = 1; else large = i; if (r <= H && a[r] > a[large]) large = r; if (large != i) { swap(a[i], a[large]); max(large); } } int main() { cin >> H; for (int i = 1; i <= H; i++) cin >> a[i]; for (int i = H / 2; i >= 1; i--) max(i); for (int i = 1; i <= H; i++) cout << " " << a[i]; cout << endl; return 0; }
#include <cstdio> #include <iostream> #define MAX 2000000 using namespace std; int H, a[MAX + 1]; void max(int i) { int l, r, large; l = 2 * i; r = 2 * i + 1; if (l <= H && a[l] > a[i]) large = l; else large = i; if (r <= H && a[r] > a[large]) large = r; if (large != i) { swap(a[i], a[large]); max(large); } } int main() { cin >> H; for (int i = 1; i <= H; i++) cin >> a[i]; for (int i = H / 2; i >= 1; i--) max(i); for (int i = 1; i <= H; i++) cout << " " << a[i]; cout << endl; return 0; }
replace
10
11
10
11
TLE
p02288
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int A[260], H; void maxHeapify(int i) { int l = i * 2; int r = i * 2 + 1; int largest; if (l <= H && A[l] > A[i]) largest = l; else largest = i; if (r <= H && A[r] > A[largest]) largest = r; if (largest != i) { swap(A[i], A[largest]); maxHeapify(largest); } } void buildHeap() { for (int i = H / 2; i >= 1; i--) maxHeapify(i); } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> A[i]; H = n; buildHeap(); for (int i = 1; i <= n; i++) cout << " " << A[i]; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int A[500010], H; void maxHeapify(int i) { int l = i * 2; int r = i * 2 + 1; int largest; if (l <= H && A[l] > A[i]) largest = l; else largest = i; if (r <= H && A[r] > A[largest]) largest = r; if (largest != i) { swap(A[i], A[largest]); maxHeapify(largest); } } void buildHeap() { for (int i = H / 2; i >= 1; i--) maxHeapify(i); } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> A[i]; H = n; buildHeap(); for (int i = 1; i <= n; i++) cout << " " << A[i]; cout << endl; return 0; }
replace
2
3
2
3
0
p02288
C++
Runtime Error
#include <cstdio> #include <iostream> #include <math.h> using namespace std; int p[250]; int id, q; int k; int parent(int i) { if (i % 2 == 0) { return i / 2; } else { return (i - 1) / 2; } } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } int inside(int i) { if (i > id || i <= 0) { return 0; } else { return 1; } } void maxHeapify(int i) { if (inside(left(i)) == 0 || (p[right(i)] <= p[i] && p[left(i)] <= p[i])) { } else { if (p[left(i)] >= p[right(i)]) { q = p[left(i)]; p[left(i)] = p[i]; p[i] = q; maxHeapify(left(i)); } else { q = p[right(i)]; p[right(i)] = p[i]; p[i] = q; maxHeapify(right(i)); } } } int main() { cin >> id; for (int m = 0; m <= 8; m++) { if (id >= pow(2, m)) { k = (int)pow(2, m); } } for (int m = 1; m <= id; m++) { cin >> p[m]; } for (int m = id; m >= 1; m--) { maxHeapify(m); } for (int m = 1; m <= id; m++) { printf(" %d", p[m]); } printf("\n"); return 0; }
#include <cstdio> #include <iostream> #include <math.h> using namespace std; int p[500000]; int id, q; int k; int parent(int i) { if (i % 2 == 0) { return i / 2; } else { return (i - 1) / 2; } } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } int inside(int i) { if (i > id || i <= 0) { return 0; } else { return 1; } } void maxHeapify(int i) { if (inside(left(i)) == 0 || (p[right(i)] <= p[i] && p[left(i)] <= p[i])) { } else { if (p[left(i)] >= p[right(i)]) { q = p[left(i)]; p[left(i)] = p[i]; p[i] = q; maxHeapify(left(i)); } else { q = p[right(i)]; p[right(i)] = p[i]; p[i] = q; maxHeapify(right(i)); } } } int main() { cin >> id; for (int m = 0; m <= 8; m++) { if (id >= pow(2, m)) { k = (int)pow(2, m); } } for (int m = 1; m <= id; m++) { cin >> p[m]; } for (int m = id; m >= 1; m--) { maxHeapify(m); } for (int m = 1; m <= id; m++) { printf(" %d", p[m]); } printf("\n"); return 0; }
replace
5
6
5
6
0
p02288
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; const int maxx = 200000; int A[maxx + 1]; int n; void maxHeapify(int i) { int left = 2 * i, right = 2 * i + 1; int largest; // ?????????????????????????????????????????????????????§??????????????¶?????§??????????????? if (left <= n && A[left] > A[i]) largest = left; else largest = i; if (right <= n && A[right] > A[largest]) largest = right; if (largest != i) { swap(A[i], A[largest]); maxHeapify(largest); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &A[i]); for (int i = n / 2; i >= 1; i--) maxHeapify(i); for (int i = 1; i <= n; i++) printf(" %d", A[i]); printf("\n"); return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; const int maxx = 2000000; int A[maxx + 1]; int n; void maxHeapify(int i) { int left = 2 * i, right = 2 * i + 1; int largest; // ?????????????????????????????????????????????????????§??????????????¶?????§??????????????? if (left <= n && A[left] > A[i]) largest = left; else largest = i; if (right <= n && A[right] > A[largest]) largest = right; if (largest != i) { swap(A[i], A[largest]); maxHeapify(largest); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &A[i]); for (int i = n / 2; i >= 1; i--) maxHeapify(i); for (int i = 1; i <= n; i++) printf(" %d", A[i]); printf("\n"); return 0; }
replace
4
5
4
5
0
p02288
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct Node { int key; int id, parent, left, right; }; Node A[100000]; int H; void heap(int H) { for (int i = 1; i <= H; i++) { if (i == 1) { A[i].left = A[2 * i].key; A[i].right = A[2 * i + 1].key; } else { A[i].parent = A[i / 2].key; A[i].left = A[2 * i].key; A[i].right = A[2 * i + 1].key; } } } void maxheap(int i) { int l = 0, r = 0; int lag; int tmp; l = 2 * i; r = 2 * i + 1; if (l <= H && A[l].key > A[i].key) { lag = l; } else { lag = i; } if (r <= H && A[r].key > A[lag].key) { lag = r; } if (lag != i) { tmp = A[i].key; A[i].key = A[lag].key; A[lag].key = tmp; maxheap(lag); } } int main() { cin >> H; for (int i = 1; i <= H; i++) { cin >> A[i].key; A[i].id = i; } heap(H); for (int i = H / 2; i >= 1; i--) { maxheap(i); } for (int i = 1; i <= H; i++) { cout << " " << A[i].key; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct Node { int key; int id, parent, left, right; }; Node A[20000000]; int H; void heap(int H) { for (int i = 1; i <= H; i++) { if (i == 1) { A[i].left = A[2 * i].key; A[i].right = A[2 * i + 1].key; } else { A[i].parent = A[i / 2].key; A[i].left = A[2 * i].key; A[i].right = A[2 * i + 1].key; } } } void maxheap(int i) { int l = 0, r = 0; int lag; int tmp; l = 2 * i; r = 2 * i + 1; if (l <= H && A[l].key > A[i].key) { lag = l; } else { lag = i; } if (r <= H && A[r].key > A[lag].key) { lag = r; } if (lag != i) { tmp = A[i].key; A[i].key = A[lag].key; A[lag].key = tmp; maxheap(lag); } } int main() { cin >> H; for (int i = 1; i <= H; i++) { cin >> A[i].key; A[i].id = i; } heap(H); for (int i = H / 2; i >= 1; i--) { maxheap(i); } for (int i = 1; i <= H; i++) { cout << " " << A[i].key; } cout << endl; return 0; }
replace
7
8
7
8
0
p02288
C++
Runtime Error
#include <iostream> #include <queue> #include <stack> #include <stdio.h> #include <string> #include <vector> #define N 251 #define NIL 2000000001 using namespace std; int n; void swap(int *, int *); void maxHeapify(int[], int); void buildMaxHeap(int[]); int main() { int heap[N]; cin >> n; for (int i = 1; i <= n; i++) { cin >> heap[i]; } buildMaxHeap(heap); for (int i = 1; i <= n; i++) { cout << " " << heap[i]; } cout << endl; return 0; } void maxHeapify(int a[], int i) { int l = 2 * i; int largest; int r = 2 * i + 1; if (l <= n && a[l] > a[i]) largest = l; else largest = i; if (r <= n && a[r] > a[largest]) largest = r; if (largest != i) { swap(&a[i], &a[largest]); maxHeapify(a, largest); } } void buildMaxHeap(int a[]) { for (int i = n / 2; i >= 1; i--) maxHeapify(a, i); } void swap(int *a, int *b) { int w = *a; *a = *b; *b = w; }
#include <iostream> #include <queue> #include <stack> #include <stdio.h> #include <string> #include <vector> #define N 500000 #define NIL 2000000001 using namespace std; int n; void swap(int *, int *); void maxHeapify(int[], int); void buildMaxHeap(int[]); int main() { int heap[N]; cin >> n; for (int i = 1; i <= n; i++) { cin >> heap[i]; } buildMaxHeap(heap); for (int i = 1; i <= n; i++) { cout << " " << heap[i]; } cout << endl; return 0; } void maxHeapify(int a[], int i) { int l = 2 * i; int largest; int r = 2 * i + 1; if (l <= n && a[l] > a[i]) largest = l; else largest = i; if (r <= n && a[r] > a[largest]) largest = r; if (largest != i) { swap(&a[i], &a[largest]); maxHeapify(a, largest); } } void buildMaxHeap(int a[]) { for (int i = n / 2; i >= 1; i--) maxHeapify(a, i); } void swap(int *a, int *b) { int w = *a; *a = *b; *b = w; }
replace
6
7
6
7
0
p02288
C++
Runtime Error
#include <bits/stdc++.h> #define reps(i, s, n) for (int i = (s); i <= (n); ++i) using namespace std; int H, heap[200001]; void maxHeapify(int i) { int l, r, largest; l = i * 2; r = i * 2 + 1; // 左の子、自分、右の子の中で最大のノードを選ぶ if (l <= H && heap[l] > heap[i]) largest = l; else largest = i; if (r <= H && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); maxHeapify(largest); } } int main() { scanf("%d", &H); reps(i, 1, H) { scanf("%d", &heap[i]); } for (int i = H / 2; i >= 1; --i) maxHeapify(i); reps(i, 1, H) printf(" %d", heap[i]); putchar_unlocked('\n'); }
#include <bits/stdc++.h> #define reps(i, s, n) for (int i = (s); i <= (n); ++i) using namespace std; int H, heap[500010]; void maxHeapify(int i) { int l, r, largest; l = i * 2; r = i * 2 + 1; // 左の子、自分、右の子の中で最大のノードを選ぶ if (l <= H && heap[l] > heap[i]) largest = l; else largest = i; if (r <= H && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); maxHeapify(largest); } } int main() { scanf("%d", &H); reps(i, 1, H) { scanf("%d", &heap[i]); } for (int i = H / 2; i >= 1; --i) maxHeapify(i); reps(i, 1, H) printf(" %d", heap[i]); putchar_unlocked('\n'); }
replace
4
5
4
5
0
p02288
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int n; int parent(int i); int left(int i); int right(int i); void maxHeap(int node[], int i); void buildMaxHeap(int node[]); int main() { int i, node[252]; cin >> n; for (i = 1; i <= n; i++) cin >> node[i]; buildMaxHeap(node); for (i = 1; i <= n; i++) cout << " " << node[i]; cout << endl; return 0; } int parent(int i) { return i / 2; } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void maxHeap(int node[], int i) { int r, l, largest; if (left(i) <= n) l = left(i); else l = i; if (right(i) <= n) r = right(i); else r = i; if (node[l] < node[r]) largest = r; else largest = l; if (node[largest] < node[i]) largest = i; if (largest != i) { swap(node[i], node[largest]); maxHeap(node, largest); } } void buildMaxHeap(int node[]) { int i; for (i = n / 2; i > 0; i--) maxHeap(node, i); }
#include <algorithm> #include <iostream> using namespace std; int n; int parent(int i); int left(int i); int right(int i); void maxHeap(int node[], int i); void buildMaxHeap(int node[]); int main() { int i, node[500000]; cin >> n; for (i = 1; i <= n; i++) cin >> node[i]; buildMaxHeap(node); for (i = 1; i <= n; i++) cout << " " << node[i]; cout << endl; return 0; } int parent(int i) { return i / 2; } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void maxHeap(int node[], int i) { int r, l, largest; if (left(i) <= n) l = left(i); else l = i; if (right(i) <= n) r = right(i); else r = i; if (node[l] < node[r]) largest = r; else largest = l; if (node[largest] < node[i]) largest = i; if (largest != i) { swap(node[i], node[largest]); maxHeap(node, largest); } } void buildMaxHeap(int node[]) { int i; for (i = n / 2; i > 0; i--) maxHeap(node, i); }
replace
13
14
13
14
0
p02288
C++
Runtime Error
#include <stdio.h> #define N 250 int it = 0, n, tree[N + 1]; void swapInt(int *x, int *y) { int tmp = *x; *x = *y; *y = tmp; } void maxHeap(int u) { int left, right, largest; left = u * 2; right = u * 2 + 1; if (left <= it && tree[u] < tree[left]) largest = left; else largest = u; if (right <= it && tree[largest] < tree[right]) largest = right; if (largest != u) { swapInt(&tree[u], &tree[largest]); maxHeap(largest); } } int main() { int i; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &tree[i]); it++; } for (i = it / 2; i >= 1; i--) maxHeap(i); for (i = 1; i <= n; i++) printf(" %d", tree[i]); printf("\n"); }
#include <stdio.h> #define N 500000 int it = 0, n, tree[N + 1]; void swapInt(int *x, int *y) { int tmp = *x; *x = *y; *y = tmp; } void maxHeap(int u) { int left, right, largest; left = u * 2; right = u * 2 + 1; if (left <= it && tree[u] < tree[left]) largest = left; else largest = u; if (right <= it && tree[largest] < tree[right]) largest = right; if (largest != u) { swapInt(&tree[u], &tree[largest]); maxHeap(largest); } } int main() { int i; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &tree[i]); it++; } for (i = it / 2; i >= 1; i--) maxHeap(i); for (i = 1; i <= n; i++) printf(" %d", tree[i]); printf("\n"); }
replace
1
2
1
2
0
p02288
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; #define INF 2139062143 void heap(int); int arr[300]; int n; int main(int argc, char *argv[]) { memset(arr, 127, sizeof(arr)); // input cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; // calc for (int i = n / 2; i > 0; i--) heap(i); // output for (int i = 1; i <= n; i++) cout << " " << arr[i]; cout << endl; return 0; } void heap(int nd) { int lft = nd * 2, rht = nd * 2 + 1, largest = nd; if (lft <= n && arr[largest] < arr[lft]) largest = lft; if (rht <= n && arr[largest] < arr[rht]) largest = rht; if (largest != nd) { swap(arr[nd], arr[largest]); heap(largest); } }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; #define INF 2139062143 void heap(int); int arr[500001]; int n; int main(int argc, char *argv[]) { memset(arr, 127, sizeof(arr)); // input cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; // calc for (int i = n / 2; i > 0; i--) heap(i); // output for (int i = 1; i <= n; i++) cout << " " << arr[i]; cout << endl; return 0; } void heap(int nd) { int lft = nd * 2, rht = nd * 2 + 1, largest = nd; if (lft <= n && arr[largest] < arr[lft]) largest = lft; if (rht <= n && arr[largest] < arr[rht]) largest = rht; if (largest != nd) { swap(arr[nd], arr[largest]); heap(largest); } }
replace
11
12
11
12
0
p02288
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int H, A[500001] = {}; void mh(int i) { int l = i * 2, r = i * 2 + 1, lar = i; if (A[l] > A[i] && l <= H) lar = l; if (A[r] > A[lar] && r <= H) lar = r; if (lar != i) { swap(A[lar], A[i]); mh(lar); } } int main() { cin >> H; for (int i = 1; i <= H; i++) cin >> A[i]; for (int i = H / 2; i > 0; i--) mh(i); for (int i = 1; i <= H; i++) cout << " " << A[i]; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int H, A[5000001] = {}; void mh(int i) { int l = i * 2, r = i * 2 + 1, lar = i; if (A[l] > A[i] && l <= H) lar = l; if (A[r] > A[lar] && r <= H) lar = r; if (lar != i) { swap(A[lar], A[i]); mh(lar); } } int main() { cin >> H; for (int i = 1; i <= H; i++) cin >> A[i]; for (int i = H / 2; i > 0; i--) mh(i); for (int i = 1; i <= H; i++) cout << " " << A[i]; cout << endl; return 0; }
replace
2
3
2
3
0
p02288
C++
Runtime Error
#include <iostream> using namespace std; #define MAX 200000 int H, A[MAX + 1]; void maxHeapify(int i) { int l, r, largest, tmp; l = 2 * i; r = 2 * i + 1; if (l <= H && A[l] > A[i]) { largest = l; } else { largest = i; } if (r <= H && A[r] > A[largest]) { largest = r; } if (largest != i) { tmp = A[i]; A[i] = A[largest]; A[largest] = tmp; maxHeapify(largest); } } int main() { int i; cin >> H; for (i = 1; i <= H; i++) { cin >> A[i]; } for (i = H / 2; i >= 1; i--) { maxHeapify(i); } for (i = 1; i <= H; i++) { cout << " " << A[i]; } cout << endl; return 0; }
#include <iostream> using namespace std; #define MAX 2000000 int H, A[MAX + 1]; void maxHeapify(int i) { int l, r, largest, tmp; l = 2 * i; r = 2 * i + 1; if (l <= H && A[l] > A[i]) { largest = l; } else { largest = i; } if (r <= H && A[r] > A[largest]) { largest = r; } if (largest != i) { tmp = A[i]; A[i] = A[largest]; A[largest] = tmp; maxHeapify(largest); } } int main() { int i; cin >> H; for (i = 1; i <= H; i++) { cin >> A[i]; } for (i = H / 2; i >= 1; i--) { maxHeapify(i); } for (i = 1; i <= H; i++) { cout << " " << A[i]; } cout << endl; return 0; }
replace
2
3
2
3
0
p02288
C++
Runtime Error
#include <stdio.h> long long int a[100]; int x; void build(int n) { long long int temp; int top = n, l = n * 2, r = n * 2 + 1; if (a[r] > a[n] && r <= x) top = r; if (a[l] > a[top] && l <= x) top = l; if (top != n) { temp = a[top]; a[top] = a[n]; a[n] = temp; build(top); } } int main() { scanf("%d", &x); for (int i = 1; i <= x; i++) { scanf("%lld", &a[i]); a[i * 2] = a[i * 2 + 1] = 0; } for (int i = x / 2; i > 0; i--) { build(i); } for (int i = 1; i <= x; i++) { printf(" %lld", a[i]); } printf("\n"); }
#include <stdio.h> long long int a[1000000]; int x; void build(int n) { long long int temp; int top = n, l = n * 2, r = n * 2 + 1; if (a[r] > a[n] && r <= x) top = r; if (a[l] > a[top] && l <= x) top = l; if (top != n) { temp = a[top]; a[top] = a[n]; a[n] = temp; build(top); } } int main() { scanf("%d", &x); for (int i = 1; i <= x; i++) { scanf("%lld", &a[i]); a[i * 2] = a[i * 2 + 1] = 0; } for (int i = x / 2; i > 0; i--) { build(i); } for (int i = 1; i <= x; i++) { printf(" %lld", a[i]); } printf("\n"); }
replace
1
2
1
2
0
p02288
C++
Runtime Error
#include <iostream> using namespace std; int n; int H[251]; void buildMaxHeap(void); void maxHeapify(int); int main() { cin >> n; for (int i = 1; i < n + 1; ++i) { cin >> H[i]; } buildMaxHeap(); for (int i = 1; i < n + 1; ++i) { cout << " " << H[i] << flush; } cout << endl; return 0; } void buildMaxHeap() { for (int i = n / 2; i > 0; --i) { maxHeapify(i); } } void maxHeapify(int i) { int r, l, largest, tmp; l = i * 2; r = i * 2 + 1; if (l <= n && H[l] > H[i]) largest = l; else largest = i; if (r <= n && H[r] > H[largest]) largest = r; if (largest != i) { tmp = H[i]; H[i] = H[largest]; H[largest] = tmp; maxHeapify(largest); } }
#include <iostream> using namespace std; int n; int H[5000000]; void buildMaxHeap(void); void maxHeapify(int); int main() { cin >> n; for (int i = 1; i < n + 1; ++i) { cin >> H[i]; } buildMaxHeap(); for (int i = 1; i < n + 1; ++i) { cout << " " << H[i] << flush; } cout << endl; return 0; } void buildMaxHeap() { for (int i = n / 2; i > 0; --i) { maxHeapify(i); } } void maxHeapify(int i) { int r, l, largest, tmp; l = i * 2; r = i * 2 + 1; if (l <= n && H[l] > H[i]) largest = l; else largest = i; if (r <= n && H[r] > H[largest]) largest = r; if (largest != i) { tmp = H[i]; H[i] = H[largest]; H[largest] = tmp; maxHeapify(largest); } }
replace
3
4
3
4
0
p02288
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int A[250 + 1], h; int parent(int i) { return i / 2; } int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } void maxHeap(int i) { int l = left(i), r = right(i); int largest; if (l <= h && A[l] > A[i]) largest = l; else largest = i; if (r <= h && A[r] > A[largest]) largest = r; if (largest != i) { swap(A[i], A[largest]); maxHeap(largest); } } int main(void) { int i; scanf("%d", &h); for (i = 1; i <= h; i++) scanf("%d", &A[i]); for (i = h / 2; i >= 1; i--) maxHeap(i); for (i = 1; i <= h; i++) printf(" %d", A[i]); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int A[500000 + 1], h; int parent(int i) { return i / 2; } int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } void maxHeap(int i) { int l = left(i), r = right(i); int largest; if (l <= h && A[l] > A[i]) largest = l; else largest = i; if (r <= h && A[r] > A[largest]) largest = r; if (largest != i) { swap(A[i], A[largest]); maxHeap(largest); } } int main(void) { int i; scanf("%d", &h); for (i = 1; i <= h; i++) scanf("%d", &A[i]); for (i = h / 2; i >= 1; i--) maxHeap(i); for (i = 1; i <= h; i++) printf(" %d", A[i]); printf("\n"); return 0; }
replace
2
3
2
3
0
p02288
C++
Runtime Error
#include <stdio.h> void max_heapify(int *a, int i, int n) { int j, temp; temp = a[i]; j = 2 * i; while (j <= n) { if (j < n && a[j + 1] > a[j]) j = j + 1; if (temp > a[j]) break; else if (temp <= a[j]) { a[j / 2] = a[j]; j = 2 * j; } } a[j / 2] = temp; return; } void build_maxheap(int *a, int n) { int i; for (i = n / 2; i >= 1; i--) { max_heapify(a, i, n); } } int main() { int n, i, x; scanf("%d", &n); int a[20]; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } build_maxheap(a, n); for (i = 1; i <= n; i++) { printf(" %d", a[i]); } printf("\n"); return 0; }
#include <stdio.h> void max_heapify(int *a, int i, int n) { int j, temp; temp = a[i]; j = 2 * i; while (j <= n) { if (j < n && a[j + 1] > a[j]) j = j + 1; if (temp > a[j]) break; else if (temp <= a[j]) { a[j / 2] = a[j]; j = 2 * j; } } a[j / 2] = temp; return; } void build_maxheap(int *a, int n) { int i; for (i = n / 2; i >= 1; i--) { max_heapify(a, i, n); } } int main() { int n, i, x; scanf("%d", &n); int a[500001]; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } build_maxheap(a, n); for (i = 1; i <= n; i++) { printf(" %d", a[i]); } printf("\n"); return 0; }
replace
62
63
62
63
0
p02288
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> #include <vector> #define _USE_MATH_DEFINES #include <cmath> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) using namespace std; typedef long long ll; const int MAXH = 260; void maxHeap(int i, int H, int *A) { int l, r, largest; l = 2 * i; r = 2 * i + 1; if (l <= H && A[l] > A[i]) largest = l; else largest = i; if (r <= H && A[r] > A[largest]) largest = r; if (largest != i) { swap(A[i], A[largest]); maxHeap(largest, H, A); } } int main() { int H, A[MAXH]; cin >> H; for (int i = 1; i <= H; i++) { scanf("%d", &A[i]); } for (int i = H / 2; i >= 1; i--) { maxHeap(i, H, A); } for (int i = 1; i <= H; i++) { cout << " " << A[i]; } cout << endl; return (0); }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> #include <vector> #define _USE_MATH_DEFINES #include <cmath> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) using namespace std; typedef long long ll; const int MAXH = 500100; void maxHeap(int i, int H, int *A) { int l, r, largest; l = 2 * i; r = 2 * i + 1; if (l <= H && A[l] > A[i]) largest = l; else largest = i; if (r <= H && A[r] > A[largest]) largest = r; if (largest != i) { swap(A[i], A[largest]); maxHeap(largest, H, A); } } int main() { int H, A[MAXH]; cin >> H; for (int i = 1; i <= H; i++) { scanf("%d", &A[i]); } for (int i = H / 2; i >= 1; i--) { maxHeap(i, H, A); } for (int i = 1; i <= H; i++) { cout << " " << A[i]; } cout << endl; return (0); }
replace
16
17
16
17
0
p02288
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long lli; typedef vector<lli> vll; typedef vector<bool> vbl; typedef vector<vector<lli>> mat; typedef vector<vector<bool>> matb; typedef vector<string> vst; typedef pair<lli, lli> pll; typedef pair<double, double> pdd; lli h; vll a; void max_heapify(lli i) { lli l = i * 2; lli r = i * 2 + 1; lli est = max({i, r, l}, [](lli s, lli t) { return a[s] < a[t]; }); swap(a[i], a[est]); if (i != est) max_heapify(est); } int main() { cin >> h; a = vll(h + 1); for (lli i = 1; i <= h; i++) cin >> a[i]; for (lli i = h; i > 0; i--) max_heapify(i); for (lli i = 1; i <= h; i++) cout << " " << a[i]; cout << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long lli; typedef vector<lli> vll; typedef vector<bool> vbl; typedef vector<vector<lli>> mat; typedef vector<vector<bool>> matb; typedef vector<string> vst; typedef pair<lli, lli> pll; typedef pair<double, double> pdd; lli h; vll a; void max_heapify(lli i) { lli l = i * 2; lli r = i * 2 + 1; lli est = max({i, r, l}, [](lli s, lli t) { if (s > h) return true; if (t > h) return false; return a[s] < a[t]; }); swap(a[i], a[est]); if (i != est) max_heapify(est); } int main() { cin >> h; a = vll(h + 1); for (lli i = 1; i <= h; i++) cin >> a[i]; for (lli i = h; i > 0; i--) max_heapify(i); for (lli i = 1; i <= h; i++) cout << " " << a[i]; cout << endl; return 0; }
replace
27
28
27
34
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p02288
C++
Runtime Error
#include <iostream> #include <stdio.h> #define NUM 100000000 #define NIL -2000000001 using namespace std; void print(int *heap, int H) { for (int i = 0; i < H; i++) { printf(" %d", heap[i + 1]); } printf("\n"); } int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } int largest_(int *heap, int a, int b, int c) { if (heap[a] < heap[b]) { if (heap[b] < heap[c]) { return c; } else { return b; } } else { if (heap[a] < heap[c]) { return c; } else { return a; } } } void maxHeapify(int *heap, int i) { int l = left(i); int r = right(i); int largest = largest_(heap, l, r, i); if (largest != i) { swap(heap[i], heap[largest]); maxHeapify(heap, largest); } } void buildMaxHeap(int *heap, int H) { for (int i = H / 2; i >= 1; i--) { maxHeapify(heap, i); } } int main(void) { int H; scanf("%d", &H); int heap[NUM]; for (int i = 0; i < NUM; i++) { heap[i] = NIL; } for (int i = 1; i <= H; i++) { scanf("%d", &heap[i]); } buildMaxHeap(heap, H); print(heap, H); return 0; }
#include <iostream> #include <stdio.h> #define NUM 10000000 #define NIL -2000000001 using namespace std; void print(int *heap, int H) { for (int i = 0; i < H; i++) { printf(" %d", heap[i + 1]); } printf("\n"); } int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } int largest_(int *heap, int a, int b, int c) { if (heap[a] < heap[b]) { if (heap[b] < heap[c]) { return c; } else { return b; } } else { if (heap[a] < heap[c]) { return c; } else { return a; } } } void maxHeapify(int *heap, int i) { int l = left(i); int r = right(i); int largest = largest_(heap, l, r, i); if (largest != i) { swap(heap[i], heap[largest]); maxHeapify(heap, largest); } } void buildMaxHeap(int *heap, int H) { for (int i = H / 2; i >= 1; i--) { maxHeapify(heap, i); } } int main(void) { int H; scanf("%d", &H); int heap[NUM]; for (int i = 0; i < NUM; i++) { heap[i] = NIL; } for (int i = 1; i <= H; i++) { scanf("%d", &heap[i]); } buildMaxHeap(heap, H); print(heap, H); return 0; }
replace
2
3
2
3
-11
p02288
C++
Runtime Error
#include <iostream> using namespace std; int H; int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void maxHeapify(int *A, int i) { int l = left(i); int r = right(i); int largest; if (l <= H && A[i] < A[l]) { largest = l; } else { largest = i; } if (r <= H && A[largest] < A[r]) { largest = r; } if (largest != i) { swap(A[i], A[largest]); maxHeapify(A, largest); } } void buildmaxHeap(int *A) { for (int i = H / 2; i > 0; i--) { maxHeapify(A, i); } } int main() { int A[250]; cin >> H; for (int i = 1; i <= H; i++) cin >> A[i]; buildmaxHeap(A); for (int i = 1; i <= H; i++) cout << " " << A[i]; cout << endl; return 0; }
#include <iostream> using namespace std; int H; int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } void maxHeapify(int *A, int i) { int l = left(i); int r = right(i); int largest; if (l <= H && A[i] < A[l]) { largest = l; } else { largest = i; } if (r <= H && A[largest] < A[r]) { largest = r; } if (largest != i) { swap(A[i], A[largest]); maxHeapify(A, largest); } } void buildmaxHeap(int *A) { for (int i = H / 2; i > 0; i--) { maxHeapify(A, i); } } int main() { int A[1999999]; cin >> H; for (int i = 1; i <= H; i++) cin >> A[i]; buildmaxHeap(A); for (int i = 1; i <= H; i++) cout << " " << A[i]; cout << endl; return 0; }
replace
33
34
33
34
0
p02288
C++
Runtime Error
#include <cmath> #include <iostream> constexpr size_t MAX_HEAP_SIZE = 250; int H[MAX_HEAP_SIZE + 1]; int parent(int i) { return i / 2; } int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } int MaxHeapify(int *H, int i, int heap_size) { int l = left(i); int r = right(i); int largest; if ((l <= heap_size) && (H[l]) > H[i]) { largest = l; } else { largest = i; } if ((r <= heap_size) && (H[r] > H[largest])) { largest = r; } if (largest != i) { std::swap(H[i], H[largest]); MaxHeapify(H, largest, heap_size); } return 0; } int BuildMaxHeap(int *H, int heap_size) { for (int i = heap_size / 2; i > 0; i--) { MaxHeapify(H, i, heap_size); } return 0; } int main() { int n; std::cin >> n; for (int i = 1; i <= n; i++) std::cin >> H[i]; BuildMaxHeap(H, n); for (int i = 1; i <= n; i++) { std::cout << " "; std::cout << H[i]; } std::cout << std::endl; }
#include <cmath> #include <iostream> constexpr size_t MAX_HEAP_SIZE = 500000; ; int H[MAX_HEAP_SIZE + 1]; int parent(int i) { return i / 2; } int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } int MaxHeapify(int *H, int i, int heap_size) { int l = left(i); int r = right(i); int largest; if ((l <= heap_size) && (H[l]) > H[i]) { largest = l; } else { largest = i; } if ((r <= heap_size) && (H[r] > H[largest])) { largest = r; } if (largest != i) { std::swap(H[i], H[largest]); MaxHeapify(H, largest, heap_size); } return 0; } int BuildMaxHeap(int *H, int heap_size) { for (int i = heap_size / 2; i > 0; i--) { MaxHeapify(H, i, heap_size); } return 0; } int main() { int n; std::cin >> n; for (int i = 1; i <= n; i++) std::cin >> H[i]; BuildMaxHeap(H, n); for (int i = 1; i <= n; i++) { std::cout << " "; std::cout << H[i]; } std::cout << std::endl; }
replace
3
4
3
5
0
p02288
C++
Runtime Error
#include <cstdio> #include <iostream> constexpr int MAXN = 250; int Heap[MAXN + 1]; int parent(int i) { return i / 2; } int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } #define FOR(i, a, b) for (int i = (a); i < (b); i++) int n; void ShowHeap(int *Heap) { FOR(i, 1, n + 1) { std::printf("node %d: key = %d, ", i, Heap[i]); if (parent(i)) std::printf("parent key = %d, ", Heap[parent(i)]); if (left(i) <= n) std::printf("left key = %d, ", Heap[left(i)]); if (right(i) <= n) std::printf("right key = %d, ", Heap[right(i)]); std::cout << std::endl; } } void MaxHeapify(int *H, int i) { int l = left(i), r = right(i); int largest = i; if (l <= n && H[largest] < H[l]) { largest = l; } if (r <= n && H[largest] < H[r]) { largest = r; } if (largest != i) { std::swap(H[i], H[largest]); MaxHeapify(H, largest); } } void BuildHeap(int *H) { for (int i = n / 2; i > 0; --i) { MaxHeapify(H, i); } } int main() { std::cin >> n; FOR(i, 0, n) std::cin >> Heap[i + 1]; BuildHeap(Heap); FOR(i, 0, n) std::cout << " " << Heap[i + 1]; std::cout << std::endl; }
#include <cstdio> #include <iostream> constexpr int MAXN = 500000; int Heap[MAXN + 1]; int parent(int i) { return i / 2; } int left(int i) { return i * 2; } int right(int i) { return i * 2 + 1; } #define FOR(i, a, b) for (int i = (a); i < (b); i++) int n; void ShowHeap(int *Heap) { FOR(i, 1, n + 1) { std::printf("node %d: key = %d, ", i, Heap[i]); if (parent(i)) std::printf("parent key = %d, ", Heap[parent(i)]); if (left(i) <= n) std::printf("left key = %d, ", Heap[left(i)]); if (right(i) <= n) std::printf("right key = %d, ", Heap[right(i)]); std::cout << std::endl; } } void MaxHeapify(int *H, int i) { int l = left(i), r = right(i); int largest = i; if (l <= n && H[largest] < H[l]) { largest = l; } if (r <= n && H[largest] < H[r]) { largest = r; } if (largest != i) { std::swap(H[i], H[largest]); MaxHeapify(H, largest); } } void BuildHeap(int *H) { for (int i = n / 2; i > 0; --i) { MaxHeapify(H, i); } } int main() { std::cin >> n; FOR(i, 0, n) std::cin >> Heap[i + 1]; BuildHeap(Heap); FOR(i, 0, n) std::cout << " " << Heap[i + 1]; std::cout << std::endl; }
replace
3
4
3
4
0
p02288
C++
Runtime Error
#include <iostream> using namespace std; #define MAX 2000000 int gHeap[MAX + 1], gSize; void maxHeapify(int i) { int left, right, largest; left = 2 * i; right = 2 * i + 1; // 左の子、自分、右の子で値が最大のノードを選択 if (left <= gSize && gHeap[left] > gHeap[i]) { largest = left; } else { largest = i; } // 右の子と比較 if (right <= gSize && gHeap[right] > gHeap[largest]) largest = right; if (largest != i) { swap(gHeap[i], gHeap[largest]); maxHeapify(largest); } } int main() { cin >> gSize; for (int i = 1; i <= gSize; ++i) cin >> gHeap[i]; for (int i = gSize / 2; i <= 1; --i) maxHeapify(i); for (int i = 1; i <= gSize; ++i) { cout << " " << gHeap[i]; } cout << endl; return 0; }
#include <iostream> using namespace std; #define MAX 2000000 int gHeap[MAX + 1], gSize; void maxHeapify(int i) { int left, right, largest; left = 2 * i; right = 2 * i + 1; // 左の子、自分、右の子で値が最大のノードを選択 if (left <= gSize && gHeap[left] > gHeap[i]) { largest = left; } else { largest = i; } // 右の子と比較 if (right <= gSize && gHeap[right] > gHeap[largest]) largest = right; if (largest != i) { swap(gHeap[i], gHeap[largest]); maxHeapify(largest); } } int main() { cin >> gSize; for (int i = 1; i <= gSize; ++i) cin >> gHeap[i]; // buildMaxHeap for (int i = gSize / 2; i >= 1; --i) maxHeapify(i); for (int i = 1; i <= gSize; ++i) { cout << " " << gHeap[i]; } cout << endl; return 0; }
replace
32
34
32
34
0
p02288
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int main(void) { int n; int a[250]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; make_heap(a, a + n); for (int i = 0; i < n; i++) { cout << " " << a[i]; } cout << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main(void) { int n; int a[500000]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; make_heap(a, a + n); for (int i = 0; i < n; i++) { cout << " " << a[i]; } cout << endl; return 0; }
replace
6
7
6
7
0
p02289
C++
Time Limit Exceeded
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; int H[2000001]; void printResult(int i, int n) { printf("node %d: key = %d, ", i, H[i]); if (i != 1) printf("parent key = %d, ", H[i / 2]); if (2 * i <= n) printf("left key = %d, ", H[2 * i]); if (2 * i + 1 <= n) printf("right key = %d, ", H[2 * i + 1]); printf("\n"); } void maxHeapify(int i, int n) { int l = 2 * i; int r = 2 * i + 1; int largest = i; if (l <= n and H[l] >= H[i]) largest = l; if (r <= n and H[r] >= H[largest]) largest = r; if (largest != i) { swap(H[i], H[largest]); maxHeapify(largest, n); } } void buildMaxHeap(int n) { for (int i = n / 2; i > 0; i--) maxHeapify(i, n); } void heapincreasekey(int n) { while (n > 1 and H[n / 2] < H[n]) { swap(H[n], H[n / 2]); n = n / 2; } } int main() { int n, i, key; string com; n = 0; while (true) { cin >> com; if (com == "insert") { n++; cin >> H[n]; heapincreasekey(n); } else if (com == "extract") { cout << H[1] << endl; H[1] = H[n]; n--; buildMaxHeap(n); } else if (com == "end") { break; } } return 0; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; int H[2000001]; void printResult(int i, int n) { printf("node %d: key = %d, ", i, H[i]); if (i != 1) printf("parent key = %d, ", H[i / 2]); if (2 * i <= n) printf("left key = %d, ", H[2 * i]); if (2 * i + 1 <= n) printf("right key = %d, ", H[2 * i + 1]); printf("\n"); } void maxHeapify(int i, int n) { int l = 2 * i; int r = 2 * i + 1; int largest = i; if (l <= n and H[l] >= H[i]) largest = l; if (r <= n and H[r] >= H[largest]) largest = r; if (largest != i) { swap(H[i], H[largest]); maxHeapify(largest, n); } } void buildMaxHeap(int n) { for (int i = n / 2; i > 0; i--) maxHeapify(i, n); } void heapincreasekey(int n) { while (n > 1 and H[n / 2] < H[n]) { swap(H[n], H[n / 2]); n = n / 2; } } int main() { int n, i, key; string com; n = 0; while (true) { cin >> com; if (com == "insert") { n++; cin >> H[n]; heapincreasekey(n); } else if (com == "extract") { cout << H[1] << endl; H[1] = H[n]; n--; maxHeapify(1, n); } else if (com == "end") { break; } } return 0; }
replace
59
60
59
60
TLE
p02289
C++
Runtime Error
#include <iostream> using namespace std; typedef long long signed int ll; constexpr ll NIL = -20000000000; ll a[500010] = {NIL}; int h; ll rk(int n) { if (n * 2 + 1 <= h) return a[n * 2 + 1]; return NIL; } ll lk(int n) { if (n * 2 <= h) return a[n * 2]; return NIL; } ll pk(int n) { if (n / 2 <= h) return a[n / 2]; return NIL; } void print(int n) { cout << "node " << n << ": key = " << a[n] << ", "; pk(n); lk(n); rk(n); cout << endl; } void heaplify(int n) { int l = n; if (a[l] < rk(n)) l = n * 2 + 1; if (a[l] < lk(n)) l = n * 2; if (l != n) { swap(a[l], a[n]); heaplify(l); } } void ins(int v) { a[h] = v; int d = h; h++; while (pk(d) < a[d]) { swap(a[d / 2], a[d]); d = d / 2; } } ll ext() { ll res = a[1]; h--; a[1] = a[h]; a[h] = NIL; heaplify(1); return res; } int main() { h = 1; a[0] = -NIL; string s; int t; while (!cin.eof()) { cin >> s; if (s == "extract") cout << ext() << endl; else { cin >> t; ins(t); } } return 0; }
#include <iostream> using namespace std; typedef long long signed int ll; constexpr ll NIL = -20000000000; ll a[20000000] = {NIL}; int h; ll rk(int n) { if (n * 2 + 1 <= h) return a[n * 2 + 1]; return NIL; } ll lk(int n) { if (n * 2 <= h) return a[n * 2]; return NIL; } ll pk(int n) { if (n / 2 <= h) return a[n / 2]; return NIL; } void print(int n) { cout << "node " << n << ": key = " << a[n] << ", "; pk(n); lk(n); rk(n); cout << endl; } void heaplify(int n) { int l = n; if (a[l] < rk(n)) l = n * 2 + 1; if (a[l] < lk(n)) l = n * 2; if (l != n) { swap(a[l], a[n]); heaplify(l); } } void ins(int v) { a[h] = v; int d = h; h++; while (pk(d) < a[d]) { swap(a[d / 2], a[d]); d = d / 2; } } ll ext() { ll res = a[1]; h--; a[1] = a[h]; a[h] = NIL; heaplify(1); return res; } int main() { h = 1; a[0] = -NIL; string s; int t; while (!cin.eof()) { cin >> s; if (s == "extract") cout << ext() << endl; else { cin >> t; ins(t); } } return 0; }
replace
8
9
8
9
0
p02289
C++
Runtime Error
#include <stdio.h> #include <string> void max_heapify(int *A, int i, int H) { int l = 2 * i, r = 2 * i + 1; if (l > H && r > H) return; int maxid = i, x; if (l <= H) { if (A[l] > A[maxid]) maxid = l; } if (r <= H) { if (A[r] > A[maxid]) maxid = r; } if (maxid == i) return; x = A[maxid]; A[maxid] = A[i]; A[i] = x; max_heapify(A, maxid, H); } int main() { int A[2001]; int i, H, key; char cmd[8]; scanf("%d", &H); for (i = 1; i <= H; i++) scanf("%d", &A[i]); H = 0; while (1) { scanf("%s", cmd); if (cmd[2] == 'd') break; // end. if (cmd[2] == 't') { // extract. if (H == 0) continue; printf("%d\n", A[1]); A[1] = A[H]; H--; if (H > 0) max_heapify(A, 1, H); } else { scanf("%d", &key); // H++; // insert key. i = ++H; while (i > 1) { if (A[i / 2] > key) break; A[i] = A[i / 2]; i /= 2; }; A[i] = key; } }; return 0; }
#include <stdio.h> #include <string> void max_heapify(int *A, int i, int H) { int l = 2 * i, r = 2 * i + 1; if (l > H && r > H) return; int maxid = i, x; if (l <= H) { if (A[l] > A[maxid]) maxid = l; } if (r <= H) { if (A[r] > A[maxid]) maxid = r; } if (maxid == i) return; x = A[maxid]; A[maxid] = A[i]; A[i] = x; max_heapify(A, maxid, H); } int main() { int A[2000001]; int i, H, key; char cmd[8]; scanf("%d", &H); for (i = 1; i <= H; i++) scanf("%d", &A[i]); H = 0; while (1) { scanf("%s", cmd); if (cmd[2] == 'd') break; // end. if (cmd[2] == 't') { // extract. if (H == 0) continue; printf("%d\n", A[1]); A[1] = A[H]; H--; if (H > 0) max_heapify(A, 1, H); } else { scanf("%d", &key); // H++; // insert key. i = ++H; while (i > 1) { if (A[i / 2] > key) break; A[i] = A[i / 2]; i /= 2; }; A[i] = key; } }; return 0; }
replace
25
26
25
26
0
p02289
C++
Runtime Error
#include <stdio.h> #include <string> void max_heapify(int *A, int i, int H) { int l = 2 * i, r = 2 * i + 1; if (l > H && r > H) return; int maxid = i, x; if (l <= H) maxid = (A[l] > A[maxid] ? l : maxid); if (r <= H) maxid = (A[r] > A[maxid] ? r : maxid); if (maxid == i) return; x = A[maxid]; A[maxid] = A[i]; A[i] = x; max_heapify(A, maxid, H); } int main() { int A[201]; int i, H, key; char cmd[8]; scanf("%d", &H); for (i = 1; i <= H; i++) scanf("%d", &A[i]); H = 0; while (1) { scanf("%s", cmd); if (cmd[2] == 'd') break; // end. if (cmd[2] == 't') { // extract. if (H == 0) continue; printf("%d\n", A[1]); A[1] = A[H]; H--; if (H > 0) max_heapify(A, 1, H); } else { scanf("%d", &key); // H++; // insert key. i = ++H; while (i > 1) { if (A[i / 2] > key) break; A[i] = A[i / 2]; i /= 2; }; A[i] = key; } }; return 0; }
#include <stdio.h> #include <string> void max_heapify(int *A, int i, int H) { int l = 2 * i, r = 2 * i + 1; if (l > H && r > H) return; int maxid = i, x; if (l <= H) maxid = (A[l] > A[maxid] ? l : maxid); if (r <= H) maxid = (A[r] > A[maxid] ? r : maxid); if (maxid == i) return; x = A[maxid]; A[maxid] = A[i]; A[i] = x; max_heapify(A, maxid, H); } int main() { int A[2000001]; int i, H, key; char cmd[8]; scanf("%d", &H); for (i = 1; i <= H; i++) scanf("%d", &A[i]); H = 0; while (1) { scanf("%s", cmd); if (cmd[2] == 'd') break; // end. if (cmd[2] == 't') { // extract. if (H == 0) continue; printf("%d\n", A[1]); A[1] = A[H]; H--; if (H > 0) max_heapify(A, 1, H); } else { scanf("%d", &key); // H++; // insert key. i = ++H; while (i > 1) { if (A[i / 2] > key) break; A[i] = A[i / 2]; i /= 2; }; A[i] = key; } }; return 0; }
replace
21
22
21
22
0
p02289
C++
Runtime Error
#include <cassert> #include <cstdint> #include <cstdlib> #include <utility> /* template<typename T, bool P = false> class PairingHeap; PairingHeapは融合可能なヒープ(優先度付きキュー)です 空間計算量 O(N) テンプレートパラメータ -typename T operator< によって大小が定義された構造体 要素の型になります -bool P true を与えると最大ヒープになります デフォルトでは false で最小ヒープです メンバ関数 -(constructor) () 空のヒープを構築します 時間計算量 O(1) -top (void)->const T & 先頭の要素のconst参照を返します 時間計算量 O(1) -pop (void)->const T & 先頭の要素を削除し、そのconst参照を返します 時間計算量 償却 O(logN) -push (T data)->node_t * data を要素としてヒープに追加します 追加された要素へのポインタを返します 時間計算量 O(1) -empty ()->bool ヒープが空かどうかを返します 時間計算量 O(1) -size ()->uint32 要素数を返します 時間計算量 O(1) -meld (ParingHeap<T, P> &other) other の持つ要素全てをヒープに追加します otherは空になります 時間計算量 O(1) -decrease (node_t *x, T data) x のキーの値を data に変更します data は変更前の値以下(P=true ならば以上)である必要があります 時間計算量 O(1) メンバ型 -node_t 要素を格納しているノードの型 push の返り値として与えられ、decrease の際に使用する ※N:要素数 ※T の比較に掛かる時間計算量を O(1) と仮定 */ template <typename T, bool P = false> class PairingHeap { static constexpr std::uint_fast64_t ALLOCSIZE = (std::uint_fast64_t)1 << 10; public: struct node_t { T data; node_t *left, *right, *per; }; private: node_t *root; std::uint_fast32_t size_; static node_t *merge(node_t *x, node_t *y) { if (P ^ (x->data < y->data)) std::swap(x, y); if (x->right = y->left) y->left->per = x; y->left = x; x->per = y; return y; } public: PairingHeap() : root(nullptr), size_(0) {} const T &top() const { assert(root); return root->data; } const T &pop() { assert(root); --size_; const T &ret = root->data; node_t *n = nullptr, *x = root->left, *y; while (x) { y = x; if (x->right) { x = x->right->right; y = merge(y, y->right); } else { x = nullptr; } y->right = n; n = y; } if (!n) { root = nullptr; return ret; } root = n; n = n->right; while (n) { x = n; n = n->right; root = merge(root, x); } root->per = nullptr; root->right = nullptr; return ret; } node_t *push(const T &data) { static node_t *pool = nullptr; static std::uint_fast64_t it = ALLOCSIZE; if (it == ALLOCSIZE) { pool = (node_t *)malloc(ALLOCSIZE * sizeof(node_t)); it = 0; } node_t *top = &pool[it++]; top->data = data; top->left = top->right = top->per = nullptr; root = root ? merge(root, top) : top; ++size_; return top; } bool empty() { return !root; } std::uint_fast32_t size() { return size_; } void meld(PairingHeap<T, P> &other) { size_ += other.size_; other.size_ = 0; if (other.root) { root = root ? merge(root, other.root) : other.root; other.root = nullptr; } } void decrease(node_t *x, const T &data) { assert(x); assert(!(P ^ (x->data < data))); if (!x->per) { x->data = data; return; } if (x->right) x->right->per = x->per; if (x->per->right != x) x->per->left = x->right; else x->per->right = x->right; x->per = nullptr; x->right = nullptr; root = merge(root, x); } }; // #define NDEBUG #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <cassert> #include <climits> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <ctime> #include <functional> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <type_traits> #include <utility> #include <vector> static constexpr double PI = 3.1415926535897932; using int32 = std::int_fast32_t; using int64 = std::int_fast64_t; using uint32 = std::uint_fast32_t; using uint64 = std::uint_fast64_t; using intl32 = std::int_least32_t; using intl64 = std::int_least64_t; using uintl32 = std::uint_least32_t; using uintl64 = std::uint_least64_t; namespace n91 { template <typename T> auto scan(T &d) -> typename std::enable_if<std::is_same<T, std::string>::value>::type { d = std::string(); int c = 0; while (c < 'a' || 'z' < c) c = fgetc(stdin); while ('a' <= c && c <= 'z') { d.push_back(c); c = fgetc(stdin); } } template <typename T> auto scan(T &d) -> typename std::enable_if<std::is_same<T, double>::value>::type { scanf("%lf", &d); } template <typename T> auto scan(T &d) -> typename std::enable_if<std::is_signed<T>::value == std::is_same<T, std::string>::value>::type { d = 0; int c = 0; while (c < '0' || '9' < c) c = fgetc(stdin); while ('0' <= c && c <= '9') { d = d * 10 + c - '0'; c = fgetc(stdin); } } template <typename T> auto scan(T &d) -> typename std::enable_if<std::is_signed<T>::value != std::is_same<T, double>::value>::type { d = 0; int c = 0; bool f = 0; while (c < '0' || '9' < c) { if (c == '-') f = 1; c = fgetc(stdin); } while ('0' <= c && c <= '9') { d = d * 10 + c - '0'; c = fgetc(stdin); } if (f) d = -d; } } // namespace n91 int main(void) { std::string s; uint32 k; PairingHeap<uint32, true> Q; n91::scan(s); while (s[2] != 'd') { if (s[0] == 'e') { printf("%u\n", Q.pop()); } else { n91::scan(k); Q.push(k); } n91::scan(s); } auto t = Q.push(1); Q.decrease(t, 0); return 0; }
#include <cassert> #include <cstdint> #include <cstdlib> #include <utility> /* template<typename T, bool P = false> class PairingHeap; PairingHeapは融合可能なヒープ(優先度付きキュー)です 空間計算量 O(N) テンプレートパラメータ -typename T operator< によって大小が定義された構造体 要素の型になります -bool P true を与えると最大ヒープになります デフォルトでは false で最小ヒープです メンバ関数 -(constructor) () 空のヒープを構築します 時間計算量 O(1) -top (void)->const T & 先頭の要素のconst参照を返します 時間計算量 O(1) -pop (void)->const T & 先頭の要素を削除し、そのconst参照を返します 時間計算量 償却 O(logN) -push (T data)->node_t * data を要素としてヒープに追加します 追加された要素へのポインタを返します 時間計算量 O(1) -empty ()->bool ヒープが空かどうかを返します 時間計算量 O(1) -size ()->uint32 要素数を返します 時間計算量 O(1) -meld (ParingHeap<T, P> &other) other の持つ要素全てをヒープに追加します otherは空になります 時間計算量 O(1) -decrease (node_t *x, T data) x のキーの値を data に変更します data は変更前の値以下(P=true ならば以上)である必要があります 時間計算量 O(1) メンバ型 -node_t 要素を格納しているノードの型 push の返り値として与えられ、decrease の際に使用する ※N:要素数 ※T の比較に掛かる時間計算量を O(1) と仮定 */ template <typename T, bool P = false> class PairingHeap { static constexpr std::uint_fast64_t ALLOCSIZE = (std::uint_fast64_t)1 << 10; public: struct node_t { T data; node_t *left, *right, *per; }; private: node_t *root; std::uint_fast32_t size_; static node_t *merge(node_t *x, node_t *y) { if (P ^ (x->data < y->data)) std::swap(x, y); if (x->right = y->left) y->left->per = x; y->left = x; x->per = y; return y; } public: PairingHeap() : root(nullptr), size_(0) {} const T &top() const { assert(root); return root->data; } const T &pop() { assert(root); --size_; const T &ret = root->data; node_t *n = nullptr, *x = root->left, *y; while (x) { y = x; if (x->right) { x = x->right->right; y = merge(y, y->right); } else { x = nullptr; } y->right = n; n = y; } if (!n) { root = nullptr; return ret; } root = n; n = n->right; while (n) { x = n; n = n->right; root = merge(root, x); } root->per = nullptr; root->right = nullptr; return ret; } node_t *push(const T &data) { static node_t *pool = nullptr; static std::uint_fast64_t it = ALLOCSIZE; if (it == ALLOCSIZE) { pool = (node_t *)malloc(ALLOCSIZE * sizeof(node_t)); it = 0; } node_t *top = &pool[it++]; top->data = data; top->left = top->right = top->per = nullptr; root = root ? merge(root, top) : top; ++size_; return top; } bool empty() { return !root; } std::uint_fast32_t size() { return size_; } void meld(PairingHeap<T, P> &other) { size_ += other.size_; other.size_ = 0; if (other.root) { root = root ? merge(root, other.root) : other.root; other.root = nullptr; } } void decrease(node_t *x, const T &data) { assert(x); assert(!(P ^ (x->data < data))); if (!x->per) { x->data = data; return; } if (x->right) x->right->per = x->per; if (x->per->right != x) x->per->left = x->right; else x->per->right = x->right; x->per = nullptr; x->right = nullptr; root = merge(root, x); } }; // #define NDEBUG #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <cassert> #include <climits> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <ctime> #include <functional> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <type_traits> #include <utility> #include <vector> static constexpr double PI = 3.1415926535897932; using int32 = std::int_fast32_t; using int64 = std::int_fast64_t; using uint32 = std::uint_fast32_t; using uint64 = std::uint_fast64_t; using intl32 = std::int_least32_t; using intl64 = std::int_least64_t; using uintl32 = std::uint_least32_t; using uintl64 = std::uint_least64_t; namespace n91 { template <typename T> auto scan(T &d) -> typename std::enable_if<std::is_same<T, std::string>::value>::type { d = std::string(); int c = 0; while (c < 'a' || 'z' < c) c = fgetc(stdin); while ('a' <= c && c <= 'z') { d.push_back(c); c = fgetc(stdin); } } template <typename T> auto scan(T &d) -> typename std::enable_if<std::is_same<T, double>::value>::type { scanf("%lf", &d); } template <typename T> auto scan(T &d) -> typename std::enable_if<std::is_signed<T>::value == std::is_same<T, std::string>::value>::type { d = 0; int c = 0; while (c < '0' || '9' < c) c = fgetc(stdin); while ('0' <= c && c <= '9') { d = d * 10 + c - '0'; c = fgetc(stdin); } } template <typename T> auto scan(T &d) -> typename std::enable_if<std::is_signed<T>::value != std::is_same<T, double>::value>::type { d = 0; int c = 0; bool f = 0; while (c < '0' || '9' < c) { if (c == '-') f = 1; c = fgetc(stdin); } while ('0' <= c && c <= '9') { d = d * 10 + c - '0'; c = fgetc(stdin); } if (f) d = -d; } } // namespace n91 int main(void) { std::string s; uint32 k; PairingHeap<uint32, true> Q; n91::scan(s); while (s[2] != 'd') { if (s[0] == 'e') { printf("%u\n", Q.pop()); } else { n91::scan(k); Q.push(k); } n91::scan(s); } auto t = Q.push(1); Q.decrease(t, 2); return 0; }
replace
270
271
270
271
-6
63082062-9b26-4ab1-bd24-ca43547ed4c5.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02289/C++/s646044302.cpp:158: void PairingHeap<T, P>::decrease(node_t*, const T&) [with T = long unsigned int; bool P = true]: Assertion `!(P ^ (x->data < data))' failed.
p02289
C++
Time Limit Exceeded
// #define NDEBUG #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <cassert> #include <climits> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <ctime> #include <functional> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <type_traits> #include <utility> #include <vector> using int32 = std::int_fast32_t; using int64 = std::int_fast64_t; using uint32 = std::uint_fast32_t; using uint64 = std::uint_fast64_t; /* 二重連鎖木によるpairing heapの実装例 */ template <class T, bool P = false> class pairing_heap { static constexpr uint64 ALLOCSIZE = (uint64)1 << 10; struct node_t { T data; node_t *left, *right; }; node_t *root; static node_t *pool; static node_t *merge(node_t *x, node_t *y) { if (P ^ (x->data < y->data)) std::swap(x, y); x->right = y->left; y->left = x; return y; } public: pairing_heap() : root(nullptr) {} T pop() { assert(root); node_t *n = nullptr, *x = root->left, *y; root->right = pool; pool = root; while (x) { y = x; if (x->right) { x = x->right->right; y = merge(y, y->right); } else { x = nullptr; } y->right = n; n = y; } if (!n) { root = nullptr; return pool->data; } root = n; n = n->right; while (n) { x = n; n = n->right; root = merge(root, x); } return pool->data; } void push(const T &data) { if (!pool) { pool = (node_t *)malloc(ALLOCSIZE * sizeof(node_t)); uint32 i = ALLOCSIZE; pool[i - 1].left = nullptr; while (--i) { pool[i - 1].left = pool + i; } } node_t *top = pool; pool = pool->left; top->data = data; top->left = nullptr; root = root ? merge(root, top) : top; } pairing_heap &operator+=(pairing_heap &other) { root = merge(root, other.root); other.root = nullptr; return *this; } }; template <class T, bool P> typename pairing_heap<T, P>::node_t *pairing_heap<T, P>::pool = nullptr; template <typename T> void uscan(T &d) { d = 0; int c = 0; while (c < '0' || '9' < c) c = fgetc(stdin); while ('0' <= c && c <= '9') { d = (d << 3) + (d << 1) + c - '0'; c = fgetc(stdin); } } void sscan(std::string &s) { s = std::string(); int c = 0; while (c < 'a' || 'z' < c) c = fgetc(stdin); while ('a' <= c && c <= 'z') { s.push_back(c); c = fgetc(stdin); } } int main(void) { std::string s; uint32 k; pairing_heap<uint32, true> Q; sscan(s); while (s[2] != 'd') { if (s[0] == 'e') { printf("%u\n", Q.pop()); } else { uscan(k); Q.push(k); } sscan(s); } return 0; }
// #define NDEBUG #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <cassert> #include <climits> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <ctime> #include <functional> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <type_traits> #include <utility> #include <vector> using int32 = std::int_fast32_t; using int64 = std::int_fast64_t; using uint32 = std::uint_fast32_t; using uint64 = std::uint_fast64_t; /* 二重連鎖木によるpairing heapの実装例 */ template <class T, bool P = false> class pairing_heap { static constexpr uint64 ALLOCSIZE = (uint64)1 << 10; struct node_t { T data; node_t *left, *right; }; node_t *root; static node_t *pool; static node_t *merge(node_t *x, node_t *y) { if (P ^ (x->data < y->data)) std::swap(x, y); x->right = y->left; y->left = x; return y; } public: pairing_heap() : root(nullptr) {} T pop() { assert(root); node_t *n = nullptr, *x = root->left, *y; root->left = pool; pool = root; while (x) { y = x; if (x->right) { x = x->right->right; y = merge(y, y->right); } else { x = nullptr; } y->right = n; n = y; } if (!n) { root = nullptr; return pool->data; } root = n; n = n->right; while (n) { x = n; n = n->right; root = merge(root, x); } return pool->data; } void push(const T &data) { if (!pool) { pool = (node_t *)malloc(ALLOCSIZE * sizeof(node_t)); uint32 i = ALLOCSIZE; pool[i - 1].left = nullptr; while (--i) { pool[i - 1].left = pool + i; } } node_t *top = pool; pool = pool->left; top->data = data; top->left = nullptr; root = root ? merge(root, top) : top; } pairing_heap &operator+=(pairing_heap &other) { root = merge(root, other.root); other.root = nullptr; return *this; } }; template <class T, bool P> typename pairing_heap<T, P>::node_t *pairing_heap<T, P>::pool = nullptr; template <typename T> void uscan(T &d) { d = 0; int c = 0; while (c < '0' || '9' < c) c = fgetc(stdin); while ('0' <= c && c <= '9') { d = (d << 3) + (d << 1) + c - '0'; c = fgetc(stdin); } } void sscan(std::string &s) { s = std::string(); int c = 0; while (c < 'a' || 'z' < c) c = fgetc(stdin); while ('a' <= c && c <= 'z') { s.push_back(c); c = fgetc(stdin); } } int main(void) { std::string s; uint32 k; pairing_heap<uint32, true> Q; sscan(s); while (s[2] != 'd') { if (s[0] == 'e') { printf("%u\n", Q.pop()); } else { uscan(k); Q.push(k); } sscan(s); } return 0; }
replace
49
50
49
50
TLE
p02289
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; // --------------------- // repetition #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; // --------------------- #define INF 922337203685477580 typedef long long ll; int H; void maxHeapify(ll *A, int i) { int l = 2 * i; int r = 2 * i + 1; // ????????????????????????????????§???????????§????????????????????¶ int largest = -11111; if (l <= H && A[l] > A[i]) { largest = l; } else { largest = i; } if (r <= H && A[r] > A[largest]) { largest = r; } if (largest != i) { // i ???????????????????????§????????´??? ll t = A[i]; A[i] = A[largest]; A[largest] = t; maxHeapify(A, largest); // ?????°?????????????????? } } void buildMaxHeap(ll *A) { for (int i = H / 2; i >= 1; i--) { maxHeapify(A, i); } } void insert(ll *S, ll k) { H++; S[H] = k; int i = H; int parent = i / 2; while (i > 1 && S[parent] < S[i]) { swap(S[parent], S[i]); i = parent; parent = i / 2; } } ll extractMax(ll *S) { ll m = S[1]; S[1] = S[H]; H--; maxHeapify(S, 1); return m; } int main() { H = 0; ll A[200000]; while (1) { char cmd[20]; // cin >> cmd; scanf("%s", cmd); if (cmd[0] == 'i') { // insert ll key; // cin >> key; scanf("%lld", &key); insert(A, key); // } else if (cmd == "extract") { } else if (cmd[0] == 'e' && cmd[1] == 'x') { ll key = extractMax(A); // cout << key << endl; printf("%lld\n", key); } else { break; } } return 0; }
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; // --------------------- // repetition #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; // --------------------- #define INF 922337203685477580 typedef long long ll; int H; void maxHeapify(ll *A, int i) { int l = 2 * i; int r = 2 * i + 1; // ????????????????????????????????§???????????§????????????????????¶ int largest = -11111; if (l <= H && A[l] > A[i]) { largest = l; } else { largest = i; } if (r <= H && A[r] > A[largest]) { largest = r; } if (largest != i) { // i ???????????????????????§????????´??? ll t = A[i]; A[i] = A[largest]; A[largest] = t; maxHeapify(A, largest); // ?????°?????????????????? } } void buildMaxHeap(ll *A) { for (int i = H / 2; i >= 1; i--) { maxHeapify(A, i); } } void insert(ll *S, ll k) { H++; S[H] = k; int i = H; int parent = i / 2; while (i > 1 && S[parent] < S[i]) { swap(S[parent], S[i]); i = parent; parent = i / 2; } } ll extractMax(ll *S) { ll m = S[1]; S[1] = S[H]; H--; maxHeapify(S, 1); return m; } int main() { H = 0; ll A[2000000]; while (1) { char cmd[20]; // cin >> cmd; scanf("%s", cmd); if (cmd[0] == 'i') { // insert ll key; // cin >> key; scanf("%lld", &key); insert(A, key); // } else if (cmd == "extract") { } else if (cmd[0] == 'e' && cmd[1] == 'x') { ll key = extractMax(A); // cout << key << endl; printf("%lld\n", key); } else { break; } } return 0; }
replace
86
87
86
87
0
p02289
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define MAX 2000000 #define INFTY (1 << 30) int H, A[MAX + 1]; void maxHeap(int i) { int l, r, large; l = 2 * i; r = 2 * i + 1; if (l <= H && A[l] > A[i]) { large = l; } else { large = i; } if (r <= H && A[r] > A[large]) { large = r; } if (large != i) { swap(A[large], A[i]); maxHeap(large); } } int extract() { int maxv; if (H < 1) return -INFTY; maxv = A[1]; A[1] = A[H--]; maxHeap(1); return maxv; } void increasekey(int i, int key) { if (key < A[i]) return; A[i] = key; while (i > 1 && A[i / 2] < A[i]) { swap(A[i], A[i / 2]); i = i / 2; } } int insert(int key) { H++; A[H] = -INFTY; increasekey(H, key); } int main() { int key; char com[10]; while (1) { cin >> com; if (com == "end") break; if (com == "insert") { cin >> key; insert(key); } else { cout << extract() << endl; } } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define MAX 2000000 #define INFTY (1 << 30) int H, A[MAX + 1]; void maxHeap(int i) { int l, r, large; l = 2 * i; r = 2 * i + 1; if (l <= H && A[l] > A[i]) { large = l; } else { large = i; } if (r <= H && A[r] > A[large]) { large = r; } if (large != i) { swap(A[large], A[i]); maxHeap(large); } } int extract() { int maxv; if (H < 1) return -INFTY; maxv = A[1]; A[1] = A[H--]; maxHeap(1); return maxv; } void increasekey(int i, int key) { if (key < A[i]) return; A[i] = key; while (i > 1 && A[i / 2] < A[i]) { swap(A[i], A[i / 2]); i = i / 2; } } int insert(int key) { H++; A[H] = -INFTY; increasekey(H, key); } int main() { int key; string com; while (1) { cin >> com; if (com == "end") break; if (com == "insert") { cin >> key; insert(key); } else { cout << extract() << endl; } } return 0; }
replace
56
57
56
57
TLE
p02289
C++
Runtime Error
#include <algorithm> #include <stdio.h> using namespace std; int val, size_, heap[2097168]; char c[9]; inline void insert(int x) { int ptr = size_++; heap[ptr] = x; while (ptr > 1) { if (heap[ptr >> 1] < heap[ptr]) swap(heap[ptr], heap[ptr >> 1]); ptr >>= 1; } } inline int extract() { int ret = heap[1], ptr = 1; while ((ptr << 1) + 1 < size_) { ptr <<= 1; if (heap[ptr] > heap[ptr + 1]) swap(heap[ptr >> 1], heap[ptr]); else swap(heap[ptr + 1], heap[ptr >> 1]), ptr++; } heap[ptr] = heap[--size_], heap[size_] = 0; while (ptr > 1) { if (heap[ptr >> 1] < heap[ptr]) swap(heap[ptr], heap[ptr >> 1]); ptr >>= 1; } return ret; } int main() { size_ = 1; while (scanf("%s", c) != 3) { if (c[0] == 'i') scanf("%d", &val), insert(val); else printf("%d\n", extract()); } return 0; }
#include <algorithm> #include <stdio.h> using namespace std; int val, size_, heap[2097168]; char c[9]; inline void insert(int x) { int ptr = size_++; heap[ptr] = x; while (ptr > 1) { if (heap[ptr >> 1] < heap[ptr]) swap(heap[ptr], heap[ptr >> 1]); ptr >>= 1; } } inline int extract() { int ret = heap[1], ptr = 1; while ((ptr << 1) + 1 < size_) { ptr <<= 1; if (heap[ptr] > heap[ptr + 1]) swap(heap[ptr >> 1], heap[ptr]); else swap(heap[ptr + 1], heap[ptr >> 1]), ptr++; } heap[ptr] = heap[--size_], heap[size_] = 0; while (ptr > 1) { if (heap[ptr >> 1] < heap[ptr]) swap(heap[ptr], heap[ptr >> 1]); ptr >>= 1; } return ret; } int main() { size_ = 1; while (scanf("%s", c), c[3]) { if (c[0] == 'i') scanf("%d", &val), insert(val); else printf("%d\n", extract()); } return 0; }
replace
33
34
33
34
TLE
p02289
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <list> #include <set> #include <stack> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) #define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++) #define PUSH(n, v) \ for (int i = 0; i < (n); i++) { \ int j; \ cin >> j; \ v.push_back(j); \ } #define ALL(v) v.begin(), v.end() class Heap { vector<int> v; int getParentIndex(int index); int getLeftChildIndex(int index); int getRightChildIndex(int index); void exchange(int i, int j); bool isSmallerThanParent(int index); bool isBiggerThanLeftChild(int index); bool isBiggerThanRightChild(int index); int getBiggerChildIndex(int index); public: void push(int n); int getMax(); void eraseMax(); }; int Heap::getBiggerChildIndex(int index) { int leftIndex = getLeftChildIndex(index); int rightIndex = getRightChildIndex(index); if (isBiggerThanLeftChild(index)) { return rightIndex; } if (isBiggerThanRightChild(index)) { return leftIndex; } if (v[getLeftChildIndex(index)] > v[getRightChildIndex(index)]) { return leftIndex; } else { return rightIndex; } } bool Heap::isBiggerThanLeftChild(int index) { int leftIndex = getLeftChildIndex(index); if (leftIndex == -1) return true; else { if (v[leftIndex] < v[index]) return true; else return false; } } bool Heap::isBiggerThanRightChild(int index) { int rightIndex = getRightChildIndex(index); if (rightIndex == -1) return true; else { if (v[rightIndex] < v[index]) return true; else return false; } } bool Heap::isSmallerThanParent(int index) { int parentIndex = getParentIndex(index); if (parentIndex == -1) return true; else { if (v[parentIndex] >= v[index]) return true; else return false; } } int Heap::getParentIndex(int index) { return (index - 1) / 2; } int Heap::getLeftChildIndex(int index) { int a = index * 2 + 1; if (a >= v.size()) { return -1; } else { return a; } } int Heap::getRightChildIndex(int index) { int a = index * 2 + 2; if (a >= v.size()) { return -1; } else { return a; } } void Heap::exchange(int i, int j) { iter_swap(v.begin() + i, v.begin() + j); } void Heap::push(int n) { v.push_back(n); int nindex = v.size() - 1; int parentIndex = getParentIndex(nindex); while (!isSmallerThanParent(nindex)) { exchange(nindex, parentIndex); nindex = parentIndex; } } int Heap::getMax() { return v[0]; } void Heap::eraseMax() { exchange(0, v.size() - 1); v.pop_back(); int nindex = 0; while (!(isBiggerThanLeftChild(nindex) && isBiggerThanRightChild(nindex))) { int biggerChildIndex = getBiggerChildIndex(nindex); exchange(nindex, biggerChildIndex); nindex = biggerChildIndex; } } int main() { string s; Heap heap; while (cin >> s) { if (s[0] == 'i') { int k; cin >> k; heap.push(k); } else if (s[1] == 'x') { cout << heap.getMax() << endl; heap.eraseMax(); } else { break; } } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <list> #include <set> #include <stack> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) #define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++) #define PUSH(n, v) \ for (int i = 0; i < (n); i++) { \ int j; \ cin >> j; \ v.push_back(j); \ } #define ALL(v) v.begin(), v.end() class Heap { vector<int> v; int getParentIndex(int index); int getLeftChildIndex(int index); int getRightChildIndex(int index); void exchange(int i, int j); bool isSmallerThanParent(int index); bool isBiggerThanLeftChild(int index); bool isBiggerThanRightChild(int index); int getBiggerChildIndex(int index); public: void push(int n); int getMax(); void eraseMax(); }; int Heap::getBiggerChildIndex(int index) { int leftIndex = getLeftChildIndex(index); int rightIndex = getRightChildIndex(index); if (isBiggerThanLeftChild(index)) { return rightIndex; } if (isBiggerThanRightChild(index)) { return leftIndex; } if (v[getLeftChildIndex(index)] > v[getRightChildIndex(index)]) { return leftIndex; } else { return rightIndex; } } bool Heap::isBiggerThanLeftChild(int index) { int leftIndex = getLeftChildIndex(index); if (leftIndex == -1) return true; else { if (v[leftIndex] < v[index]) return true; else return false; } } bool Heap::isBiggerThanRightChild(int index) { int rightIndex = getRightChildIndex(index); if (rightIndex == -1) return true; else { if (v[rightIndex] < v[index]) return true; else return false; } } bool Heap::isSmallerThanParent(int index) { int parentIndex = getParentIndex(index); if (parentIndex == -1) return true; else { if (v[parentIndex] >= v[index]) return true; else return false; } } int Heap::getParentIndex(int index) { return (index - 1) / 2; } int Heap::getLeftChildIndex(int index) { int a = index * 2 + 1; if (a >= v.size()) { return -1; } else { return a; } } int Heap::getRightChildIndex(int index) { int a = index * 2 + 2; if (a >= v.size()) { return -1; } else { return a; } } void Heap::exchange(int i, int j) { iter_swap(v.begin() + i, v.begin() + j); } void Heap::push(int n) { v.push_back(n); int nindex = v.size() - 1; int parentIndex = getParentIndex(nindex); while (!isSmallerThanParent(nindex)) { exchange(nindex, parentIndex); nindex = parentIndex; parentIndex = getParentIndex(nindex); } } int Heap::getMax() { return v[0]; } void Heap::eraseMax() { exchange(0, v.size() - 1); v.pop_back(); int nindex = 0; while (!(isBiggerThanLeftChild(nindex) && isBiggerThanRightChild(nindex))) { int biggerChildIndex = getBiggerChildIndex(nindex); exchange(nindex, biggerChildIndex); nindex = biggerChildIndex; } } int main() { string s; Heap heap; while (cin >> s) { if (s[0] == 'i') { int k; cin >> k; heap.push(k); } else if (s[1] == 'x') { cout << heap.getMax() << endl; heap.eraseMax(); } else { break; } } return 0; }
insert
118
118
118
119
TLE
p02289
C++
Runtime Error
#include <cstdio> #include <iostream> #include <math.h> using namespace std; int p[250]; int id, q; int k; int parent(int i) { if (i % 2 == 0) { return i / 2; } else { return (i - 1) / 2; } } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } int inside(int i) { if (i > id || i <= 0) { return 0; } else { return 1; } } void maxHeapify(int i) { if (inside(left(i)) == 0 || (p[right(i)] <= p[i] && p[left(i)] <= p[i])) { } else { if (p[left(i)] >= p[right(i)]) { q = p[left(i)]; p[left(i)] = p[i]; p[i] = q; maxHeapify(left(i)); } else { q = p[right(i)]; p[right(i)] = p[i]; p[i] = q; maxHeapify(right(i)); } } } void insert(int k) { id++; int i = id; p[id] = k; for (;;) { if (i != 1 && p[parent(i)] <= p[i]) { q = p[parent(i)]; p[parent(i)] = p[i]; p[i] = q; i = parent(i); } else { break; } } } int extract() { int ret = p[1]; p[1] = p[id]; id--; maxHeapify(1); return (ret); } int main() { for (;;) { char s_[100]; scanf("%s", s_); string s = s_; if (s == "end") { break; } else if (s == "insert") { int k; scanf("%d", &k); insert(k); } else if (s == "extract") { printf("%d\n", extract()); } } return 0; }
#include <cstdio> #include <iostream> #include <math.h> using namespace std; int p[2000000]; int id, q; int k; int parent(int i) { if (i % 2 == 0) { return i / 2; } else { return (i - 1) / 2; } } int left(int i) { return 2 * i; } int right(int i) { return 2 * i + 1; } int inside(int i) { if (i > id || i <= 0) { return 0; } else { return 1; } } void maxHeapify(int i) { if (inside(left(i)) == 0 || (p[right(i)] <= p[i] && p[left(i)] <= p[i])) { } else { if (p[left(i)] >= p[right(i)]) { q = p[left(i)]; p[left(i)] = p[i]; p[i] = q; maxHeapify(left(i)); } else { q = p[right(i)]; p[right(i)] = p[i]; p[i] = q; maxHeapify(right(i)); } } } void insert(int k) { id++; int i = id; p[id] = k; for (;;) { if (i != 1 && p[parent(i)] <= p[i]) { q = p[parent(i)]; p[parent(i)] = p[i]; p[i] = q; i = parent(i); } else { break; } } } int extract() { int ret = p[1]; p[1] = p[id]; id--; maxHeapify(1); return (ret); } int main() { for (;;) { char s_[100]; scanf("%s", s_); string s = s_; if (s == "end") { break; } else if (s == "insert") { int k; scanf("%d", &k); insert(k); } else if (s == "extract") { printf("%d\n", extract()); } } return 0; }
replace
5
6
5
6
0
p02289
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using Int = long long; #include <bits/stdc++.h> using namespace std; using Int = long long; // BEGIN CUT HERE template <typename T, typename E> struct SkewHeap { using F = function<T(T, T)>; using G = function<T(T, E)>; using C = function<bool(T, T)>; F f; G g; C c; T INF; E ei; SkewHeap(F f, G g, C c, T INF, E ei) : f(f), g(g), c(c), INF(INF), ei(ei) {} struct Node { Node *l, *r; T val; E add; Node(T val, E add) : val(val), add(add) { l = r = nullptr; } }; void eval(Node *a) { if (a == nullptr) return; if (a->add == ei) return; if (a->l) a->l->add = g(a->l->add, a->add); if (a->r) a->r->add = g(a->r->add, a->add); a->val = f(a->val, a->add); a->add = ei; } T top(Node *a) { return a != nullptr ? f(a->val, a->add) : INF; } T snd(Node *a) { eval(a); return a != nullptr ? min(top(a->l), top(a->r)) : INF; } Node *add(Node *a, E d) { if (a != nullptr) a->add = g(a->add, d); return a; } Node *push(T v) { return new Node(v, ei); } Node *meld(Node *a, Node *b) { if (a == nullptr) return b; if (b == nullptr) return a; if (c(top(a), top(b))) swap(a, b); eval(a); a->r = meld(a->r, b); swap(a->l, a->r); return a; } Node *pop(Node *a) { eval(a); auto res = meld(a->l, a->r); delete a; return res; } }; // END CUT HERE struct ALDS1_9_C { signed solve() { cin.tie(0); ios::sync_with_stdio(0); using Heap = SkewHeap<int, int>; Heap::F f = [](int a, int b) { return a + b; }; Heap::G g = [](int a, int b) { return a + b; }; Heap::C c = [](int a, int b) { return a < b; }; int INF = -1; Heap heap(f, g, c, INF, 0); auto base = heap.push(0); string s; while (cin >> s, s != "end") { int x; if (s == "insert") { cin >> x; base = heap.meld(base, heap.push(x)); } if (s == "extract") { x = heap.top(base); base = heap.pop(base); cout << x << endl; } } return 0; } /* verified on 2018/02/16 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_9_C&lang=jp */ }; struct APC001_D { struct UnionFind { int n; vector<int> r, p; UnionFind() {} UnionFind(int sz) : n(sz), r(sz, 1), p(sz, 0) { iota(p.begin(), p.end(), 0); } int find(int x) { return (x == p[x] ? x : p[x] = find(p[x])); } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (r[x] < r[y]) swap(x, y); r[x] += r[y]; p[y] = x; } }; signed solve() { using Heap = SkewHeap<Int, Int>; Int n, m; cin >> n >> m; vector<Int> a(n); for (Int i = 0; i < n; i++) cin >> a[i]; Heap::F f = [](Int a, Int b) { return a + b; }; Heap::G g = [](Int a, Int b) { return a + b; }; Heap::C c = [](Int a, Int b) { return a > b; }; const Int INF = 1e16; Heap heap(f, g, c, INF, 0); vector<Heap::Node *> v(n); for (Int i = 0; i < n; i++) v[i] = heap.push(a[i]); UnionFind uf(n); for (Int i = 0; i < m; i++) { Int x, y; cin >> x >> y; x = uf.find(x); y = uf.find(y); uf.unite(x, y); if (uf.find(x) != x) swap(x, y); v[x] = heap.meld(v[x], v[y]); v[y] = NULL; } if (m == n - 1) { cout << 0 << endl; return 0; } Heap::Node *base = NULL; Int ans = 0, cnt = 0; for (Int i = 0; i < n; i++) { if (uf.find(i) == i) { ans += heap.top(v[i]); v[i] = heap.pop(v[i]); base = heap.meld(base, v[i]); cnt++; } } while (m * 2 + cnt < (n - 1) * 2) { if (base == NULL) { cout << "Impossible" << endl; return 0; } ans += heap.top(base); base = heap.pop(base); cnt++; } cout << ans << endl; return 0; } /* verified on 2018/02/16 https://beta.atcoder.jp/contests/apc001/tasks/apc001_d */ }; signed main() { // ALDS1_9_C ans; APC001_D ans; ans.solve(); }
#include <bits/stdc++.h> using namespace std; using Int = long long; #include <bits/stdc++.h> using namespace std; using Int = long long; // BEGIN CUT HERE template <typename T, typename E> struct SkewHeap { using F = function<T(T, T)>; using G = function<T(T, E)>; using C = function<bool(T, T)>; F f; G g; C c; T INF; E ei; SkewHeap(F f, G g, C c, T INF, E ei) : f(f), g(g), c(c), INF(INF), ei(ei) {} struct Node { Node *l, *r; T val; E add; Node(T val, E add) : val(val), add(add) { l = r = nullptr; } }; void eval(Node *a) { if (a == nullptr) return; if (a->add == ei) return; if (a->l) a->l->add = g(a->l->add, a->add); if (a->r) a->r->add = g(a->r->add, a->add); a->val = f(a->val, a->add); a->add = ei; } T top(Node *a) { return a != nullptr ? f(a->val, a->add) : INF; } T snd(Node *a) { eval(a); return a != nullptr ? min(top(a->l), top(a->r)) : INF; } Node *add(Node *a, E d) { if (a != nullptr) a->add = g(a->add, d); return a; } Node *push(T v) { return new Node(v, ei); } Node *meld(Node *a, Node *b) { if (a == nullptr) return b; if (b == nullptr) return a; if (c(top(a), top(b))) swap(a, b); eval(a); a->r = meld(a->r, b); swap(a->l, a->r); return a; } Node *pop(Node *a) { eval(a); auto res = meld(a->l, a->r); delete a; return res; } }; // END CUT HERE struct ALDS1_9_C { signed solve() { cin.tie(0); ios::sync_with_stdio(0); using Heap = SkewHeap<int, int>; Heap::F f = [](int a, int b) { return a + b; }; Heap::G g = [](int a, int b) { return a + b; }; Heap::C c = [](int a, int b) { return a < b; }; int INF = -1; Heap heap(f, g, c, INF, 0); auto base = heap.push(0); string s; while (cin >> s, s != "end") { int x; if (s == "insert") { cin >> x; base = heap.meld(base, heap.push(x)); } if (s == "extract") { x = heap.top(base); base = heap.pop(base); cout << x << endl; } } return 0; } /* verified on 2018/02/16 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_9_C&lang=jp */ }; struct APC001_D { struct UnionFind { int n; vector<int> r, p; UnionFind() {} UnionFind(int sz) : n(sz), r(sz, 1), p(sz, 0) { iota(p.begin(), p.end(), 0); } int find(int x) { return (x == p[x] ? x : p[x] = find(p[x])); } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (r[x] < r[y]) swap(x, y); r[x] += r[y]; p[y] = x; } }; signed solve() { using Heap = SkewHeap<Int, Int>; Int n, m; cin >> n >> m; vector<Int> a(n); for (Int i = 0; i < n; i++) cin >> a[i]; Heap::F f = [](Int a, Int b) { return a + b; }; Heap::G g = [](Int a, Int b) { return a + b; }; Heap::C c = [](Int a, Int b) { return a > b; }; const Int INF = 1e16; Heap heap(f, g, c, INF, 0); vector<Heap::Node *> v(n); for (Int i = 0; i < n; i++) v[i] = heap.push(a[i]); UnionFind uf(n); for (Int i = 0; i < m; i++) { Int x, y; cin >> x >> y; x = uf.find(x); y = uf.find(y); uf.unite(x, y); if (uf.find(x) != x) swap(x, y); v[x] = heap.meld(v[x], v[y]); v[y] = NULL; } if (m == n - 1) { cout << 0 << endl; return 0; } Heap::Node *base = NULL; Int ans = 0, cnt = 0; for (Int i = 0; i < n; i++) { if (uf.find(i) == i) { ans += heap.top(v[i]); v[i] = heap.pop(v[i]); base = heap.meld(base, v[i]); cnt++; } } while (m * 2 + cnt < (n - 1) * 2) { if (base == NULL) { cout << "Impossible" << endl; return 0; } ans += heap.top(base); base = heap.pop(base); cnt++; } cout << ans << endl; return 0; } /* verified on 2018/02/16 https://beta.atcoder.jp/contests/apc001/tasks/apc001_d */ }; signed main() { ALDS1_9_C ans; // APC001_D ans; ans.solve(); }
replace
198
200
198
200
0
p02289
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using Int = long long; #include <bits/stdc++.h> using namespace std; using Int = long long; // BEGIN CUT HERE template <typename T, typename E> struct FGC { typedef function<T(T, E)> F; typedef function<E(E, E)> G; typedef function<bool(T, T)> C; F &f; G &g; C &c; FGC(F &f, G &g, C &c) : f(f), g(g), c(c) {} }; template <typename T, typename E> struct SkewHeap { SkewHeap *l, *r; T val; E add, e; FGC<T, E> &fgc; SkewHeap(T val, E e, FGC<T, E> &fgc) : val(val), add(e), e(e), fgc(fgc) { l = r = NULL; } void eval() { if (add == e) return; if (l) l->add = fgc.g(l->add, add); if (r) r->add = fgc.g(r->add, add); val = fgc.f(val, add); add = e; } T top() { return fgc.f(val, add); } }; template <typename T, typename E> SkewHeap<T, E> *meld(SkewHeap<T, E> *a, SkewHeap<T, E> *b) { if (!a) return b; if (!b) return a; if (a->fgc.c(a->top(), b->top())) swap(a, b); a->eval(); a->r = meld(a->r, b); swap(a->l, a->r); return a; } template <typename T, typename E> SkewHeap<T, E> *pop(SkewHeap<T, E> *a) { a->eval(); auto res = meld(a->l, a->r); free(a); return res; }; // END CUT HERE signed main() { using Heap = SkewHeap<int, int>; FGC<int, int>::F f = [](int a, int b) { return a + b; }; FGC<int, int>::G g = [](int a, int b) { return a + b; }; FGC<int, int>::C c = [](int a, int b) { return a < b; }; FGC<int, int> fgc(f, g, c); Heap *base = new Heap(0, 0, fgc); string s; while (cin >> s, s != "end") { int x; if (s == "insert") { cin >> x; base = meld(base, new Heap(x, 0, fgc)); } if (s == "extract") { x = base->top(); base = pop(base); cout << x << endl; } } return 0; } /* verified on 2018/01/04 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_9_C&lang=jp */
#include <bits/stdc++.h> using namespace std; using Int = long long; #include <bits/stdc++.h> using namespace std; using Int = long long; // BEGIN CUT HERE template <typename T, typename E> struct FGC { typedef function<T(T, E)> F; typedef function<E(E, E)> G; typedef function<bool(T, T)> C; F &f; G &g; C &c; FGC(F &f, G &g, C &c) : f(f), g(g), c(c) {} }; template <typename T, typename E> struct SkewHeap { SkewHeap *l, *r; T val; E add, e; FGC<T, E> &fgc; SkewHeap(T val, E e, FGC<T, E> &fgc) : val(val), add(e), e(e), fgc(fgc) { l = r = NULL; } void eval() { if (add == e) return; if (l) l->add = fgc.g(l->add, add); if (r) r->add = fgc.g(r->add, add); val = fgc.f(val, add); add = e; } T top() { return fgc.f(val, add); } }; template <typename T, typename E> SkewHeap<T, E> *meld(SkewHeap<T, E> *a, SkewHeap<T, E> *b) { if (!a) return b; if (!b) return a; if (a->fgc.c(a->top(), b->top())) swap(a, b); a->eval(); a->r = meld(a->r, b); swap(a->l, a->r); return a; } template <typename T, typename E> SkewHeap<T, E> *pop(SkewHeap<T, E> *a) { a->eval(); auto res = meld(a->l, a->r); free(a); return res; }; // END CUT HERE signed main() { cin.tie(0); ios::sync_with_stdio(0); using Heap = SkewHeap<int, int>; FGC<int, int>::F f = [](int a, int b) { return a + b; }; FGC<int, int>::G g = [](int a, int b) { return a + b; }; FGC<int, int>::C c = [](int a, int b) { return a < b; }; FGC<int, int> fgc(f, g, c); Heap *base = new Heap(0, 0, fgc); string s; while (cin >> s, s != "end") { int x; if (s == "insert") { cin >> x; base = meld(base, new Heap(x, 0, fgc)); } if (s == "extract") { x = base->top(); base = pop(base); cout << x << endl; } } return 0; } /* verified on 2018/01/04 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_9_C&lang=jp */
insert
63
63
63
65
TLE
p02289
C++
Time Limit Exceeded
#include <iostream> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define MAX_V 5000000 int heap[MAX_V]{0}; int heap_size = 0; int left(int i) { return 2 * i + 1; } int right(int i) { return 2 * i + 2; } int parent(int i) { return (i - 1) / 2; } bool has_left(int i) { return heap_size > left(i); } bool has_right(int i) { return heap_size > right(i); } void max_heapify(int i) { int l, r, largest; l = left(i); r = right(i); if (has_left(i) && heap[l] > heap[i]) largest = l; else largest = i; if (has_right(i) && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); max_heapify(largest); } } void heap_increase_key(int i, int key) { if (key < heap[i]) return; heap[i] = key; while (i > 0 && heap[parent(i)] < heap[i]) { swap(heap[i], heap[parent(i)]); i = parent(i); } } void max_heap_insert(int key) { heap[heap_size] = 0x80000000; heap_size++; heap_increase_key(heap_size - 1, key); } int heap_extract_max() { if (heap_size < 1) return -1; int max = heap[0]; heap[0] = heap[--heap_size]; max_heapify(0); return max; } int main() { for (;;) { string str; cin >> str; if (str == "end") break; else if (str == "insert") { int key; cin >> key; max_heap_insert(key); } else if (str == "extract") { int item = heap_extract_max(); cout << item << "\n"; } } }
#include <iostream> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define MAX_V 5000000 int heap[MAX_V]{0}; int heap_size = 0; int left(int i) { return 2 * i + 1; } int right(int i) { return 2 * i + 2; } int parent(int i) { return (i - 1) / 2; } bool has_left(int i) { return heap_size > left(i); } bool has_right(int i) { return heap_size > right(i); } void max_heapify(int i) { int l, r, largest; l = left(i); r = right(i); if (has_left(i) && heap[l] > heap[i]) largest = l; else largest = i; if (has_right(i) && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); max_heapify(largest); } } void heap_increase_key(int i, int key) { if (key < heap[i]) return; heap[i] = key; while (i > 0 && heap[parent(i)] < heap[i]) { swap(heap[i], heap[parent(i)]); i = parent(i); } } void max_heap_insert(int key) { heap[heap_size] = 0x80000000; heap_size++; heap_increase_key(heap_size - 1, key); } int heap_extract_max() { if (heap_size < 1) return -1; int max = heap[0]; heap[0] = heap[--heap_size]; max_heapify(0); return max; } int main() { ios::sync_with_stdio(false); cin.tie(0); for (;;) { string str; cin >> str; if (str == "end") break; else if (str == "insert") { int key; cin >> key; max_heap_insert(key); } else if (str == "extract") { int item = heap_extract_max(); cout << item << "\n"; } } }
insert
51
51
51
53
TLE
p02289
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; #define MAX 1000000 class PriorityQueue { public: int size; int buffer[MAX + 1]; PriorityQueue() { size = 0; } void insert(int x) { size++; buffer[size] = x; upHeap(size); } int get() { int v = buffer[1]; buffer[1] = buffer[size]; size--; downHeap(1); return v; } private: void upHeap(int cursor) { int parent; while (1) { parent = cursor / 2; if (parent < 1) break; if (buffer[parent] < buffer[cursor]) { swap(buffer[parent], buffer[cursor]); } else break; cursor = parent; } } void downHeap(int cursor) { int child; while (1) { if (cursor > size / 2) break; child = cursor * 2; if (child < size && buffer[child] < buffer[child + 1]) { child++; } if (buffer[cursor] < buffer[child]) { swap(buffer[cursor], buffer[child]); } else break; cursor = child; } } }; int main(void) { PriorityQueue PQ = PriorityQueue(); int x; string command; while (1) { cin >> command; if (command == "insert") { cin >> x; PQ.insert(x); } else if (command == "extract") { cout << PQ.get() << endl; } else break; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; #define MAX 2000000 class PriorityQueue { public: int size; int buffer[MAX + 1]; PriorityQueue() { size = 0; } void insert(int x) { size++; buffer[size] = x; upHeap(size); } int get() { int v = buffer[1]; buffer[1] = buffer[size]; size--; downHeap(1); return v; } private: void upHeap(int cursor) { int parent; while (1) { parent = cursor / 2; if (parent < 1) break; if (buffer[parent] < buffer[cursor]) { swap(buffer[parent], buffer[cursor]); } else break; cursor = parent; } } void downHeap(int cursor) { int child; while (1) { if (cursor > size / 2) break; child = cursor * 2; if (child < size && buffer[child] < buffer[child + 1]) { child++; } if (buffer[cursor] < buffer[child]) { swap(buffer[cursor], buffer[child]); } else break; cursor = child; } } }; int main(void) { PriorityQueue PQ = PriorityQueue(); int x; string command; while (1) { cin >> command; if (command == "insert") { cin >> x; PQ.insert(x); } else if (command == "extract") { cout << PQ.get() << endl; } else break; } return 0; }
replace
6
7
6
7
0