QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#763519 | #6994. Data Structure | hejinming983282# | WA | 0ms | 3800kb | C++23 | 2.7kb | 2024-11-19 20:47:13 | 2024-11-19 20:47:13 |
Judging History
answer
// Author : hejinming2012
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define dbg(x) cout << #x " = " << (x) << endl
#define quickio ios::sync_with_stdio(false);
#define quickin cin.tie(0);
#define quickout cout.tie(0);
using namespace std;
inline int read() {
int now = 0, nev = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') nev = -1; c = getchar(); }
while(c >= '0' && c <= '9') { now = (now << 1) + (now << 3) + (c & 15); c = getchar(); }
return now * nev;
}
void write(int x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
struct node {
vector <int> keys;
vector <node*> ch;
node() {}
};
class TTFT {
public :
node *root;
TTFT() : root(nullptr) {}
void insert(int val) {
if(root == nullptr) {
root = new node();
root -> keys.push_back(val);
return ;
}
if(root -> keys.size() == 3) {
node *nr = new node();
nr -> ch.push_back(root);
split(nr, root, 0);
root = nr;
}
node *cur = root;
while(1) {
if(cur -> ch.empty()) {
cur -> keys.push_back(val);
sort(cur -> keys.begin(), cur -> keys.end());
return ;
}
int chid = 0;
while(chid < cur -> keys.size() && val > cur -> keys[chid]) chid++;
node *child = cur -> ch[chid];
if(child -> keys.size() == 3) {
split(cur, child, chid);
if(val > cur -> keys[chid]) chid++;
child = cur -> ch[chid];
}
cur = child;
}
}
void pre(vector < vector <int> > &res) {
preOrder(root, res);
}
private :
void split(node *fa, node *ch, int chid) {
int k2 = ch -> keys[1];
node *l = new node();
l -> keys.push_back(ch -> keys[0]);
node *r = new node();
r -> keys.push_back(ch -> keys[2]);
if(!ch -> ch.empty()) {
l -> ch.push_back(ch -> ch[0]);
l -> ch.push_back(ch -> ch[1]);
r -> ch.push_back(ch -> ch[2]);
r -> ch.push_back(ch -> ch[3]);
}
fa -> keys.insert(fa -> keys.begin() + chid, k2);
fa -> ch.erase(fa -> ch.begin() + chid);
fa -> ch.insert(fa -> ch.begin() + chid, l);
fa -> ch.insert(fa -> ch.begin() + chid + 1, r);
delete ch;
}
void preOrder(node *nd, vector < vector <int> > &res) {
if(nd == nullptr) return ;
res.emplace_back(nd -> keys);
for(auto ch : nd -> ch)
preOrder(ch, res);
}
};
signed main() {
quickio
quickin
quickout
int T = read();
for(int _ = 1; _ <= T; _++) {
int n = read();
vector <int> val(n);
for(int i = 0; i < n; i++)
val[i] = read();
TTFT tree;
for(int i = 0; i < n; i++)
tree.insert(val[i]);
vector < vector <int> > Pre;
tree.pre(Pre);
printf("Case #%lld\n", _);
for(auto &ndk : Pre) {
for(int i : ndk) write(i), putchar(' ');
putchar(10);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3800kb
input:
3 4 1 2 3 4 4 4 3 2 1 17 6 3 5 7 1 10 2 9 4 8 11 12 13 14 15 16 17
output:
Case #1 2 1 3 4 Case #2 3 1 2 4 Case #3 5 9 2 1 3 4 7 6 8 11 13 15 10 12 14 16 17
result:
wrong answer 2nd words differ - expected: '#1:', found: '#1'