QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#763522#6994. Data Structurehejinming983282#AC ✓32ms12632kbC++232.7kb2024-11-19 20:47:582024-11-19 20:47:58

Judging History

你现在查看的是最新测评结果

  • [2024-11-19 20:47:58]
  • 评测
  • 测评结果:AC
  • 用时:32ms
  • 内存:12632kb
  • [2024-11-19 20:47:58]
  • 提交

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;
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3804kb

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:

ok 31 tokens

Test #2:

score: 0
Accepted
time: 32ms
memory: 12632kb

input:

50
3
2 3 1
176
7 25 23 51 117 83 123 171 101 165 144 58 30 159 69 112 15 173 74 98 172 85 160 6 78 35 64 126 92 55 115 131 79 121 91 39 68 133 158 169 127 22 162 86 13 97 149 8 62 122 145 33 24 48 137 118 40 113 84 89 154 2 63 10 95 53 170 67 90 151 12 5 37 93 120 168 128 73 41 14 150 108 76 65 148 ...

output:

Case #1:
1 2 3 
Case #2:
83 
51 
23 35 
5 10 
2 
1 
3 4 
7 
6 
8 9 
13 15 19 
11 12 
14 
16 17 18 
20 21 22 
27 
25 
24 
26 
30 32 
28 29 
31 
33 34 
45 
38 40 42 
36 37 
39 
41 
43 44 
48 
46 47 
49 50 
64 
58 
53 55 
52 
54 
56 57 
62 
59 60 61 
63 
69 78 
67 
65 66 
68 
72 74 
70 71 
73 
75 76 77...

result:

ok 125018 tokens