QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#679478#9519. Build a Computerucup-team5062#AC ✓1ms3820kbC++172.5kb2024-10-26 17:42:022024-10-26 17:42:02

Judging History

This is the latest submission verdict.

  • [2024-10-26 17:42:02]
  • Judged
  • Verdict: AC
  • Time: 1ms
  • Memory: 3820kb
  • [2024-10-26 17:42:02]
  • Submitted

answer

#include <array>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

struct interval {
	int l, r;
};

string make_binary(int x) {
	string res;
	while (x > 0) {
		res += '0' + x % 2;
		x /= 2;
	}
	reverse(res.begin(), res.end());
	return res;
}

vector<interval> interval_decomposition(int L, int R) {
	vector<interval> res;
	auto dfs = [&](auto& self, int s, int t) -> void {
		if (L <= s && t <= R) {
			res.push_back(interval{s, t});
			return;
		}
		if (R <= s || t <= L) {
			return;
		}
		self(self, s, (s + t) / 2);
		self(self, (s + t) / 2, t);
	};
	int maxR = 1;
	while (maxR < R) {
		maxR *= 2;
	}
	dfs(dfs, 0, maxR);
	return res;
}

struct edge {
	int t, c;
};

int main() {
	// step #1. input
	int L, R;
	cin >> L >> R;
	R++;

	// step #2. decompose to intervals
	vector<interval> ivls = interval_decomposition(L, R);
	int Z = ivls.size();

	// step #3. make trie
	vector<array<int, 2> > trie;
	trie.push_back({-1, -1});
	vector<int> lastpos(Z, -1);
	vector<int> nextcode(Z, -1);
	vector<int> level(Z, -1);
	int max_level = 0;
	for (int i = 0; i < Z; i++) {
		interval p = ivls[i];
		string prefix = make_binary(p.l);
		level[i] = 31 - __builtin_clz(p.r - p.l);
		nextcode[i] = int(prefix[int(prefix.size()) - level[i] - 1] - '0');
		prefix = prefix.substr(0, int(prefix.size()) - level[i] - 1);
		int pos = 0;
		for (int j = 0; j < int(prefix.size()); j++) {
			int c = int(prefix[j] - '0');
			if (trie[pos][c] != -1) {
				pos = trie[pos][c];
			} else {
				trie[pos][c] = trie.size();
				pos = trie.size();
				trie.push_back({-1, -1});
			}
		}
		lastpos[i] = pos;
		max_level = max(max_level, level[i]);
	}

	// step #4. make graph
	vector<vector<edge> > G(trie.size() + max_level + 1);
	for (int i = 0; i < int(trie.size()); i++) {
		for (int j = 0; j < 2; j++) {
			if (trie[i][j] != -1) {
				G[i].push_back(edge{trie[i][j], j});
			}
		}
	}
	for (int i = 0; i < Z; i++) {
		int nextpos = int(trie.size()) + max_level - level[i];
		G[lastpos[i]].push_back(edge{nextpos, nextcode[i]});
	}
	for (int i = 0; i < max_level; i++) {
		G[int(trie.size()) + i].push_back(edge{int(trie.size()) + i + 1, 0});
		G[int(trie.size()) + i].push_back(edge{int(trie.size()) + i + 1, 1});
	}

	// step #5. output
	cout << G.size() << endl;
	for (int i = 0; i < int(G.size()); i++) {
		cout << G[i].size();
		for (edge e : G[i]) {
			cout << ' ' << e.t + 1 << ' ' << e.c;
		}
		cout << endl;
	}

	return 0;
}

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

詳細信息

Test #1:

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

input:

5 7

output:

5
1 2 1
2 3 0 4 1
1 5 1
2 5 0 5 1
0

result:

ok ok

Test #2:

score: 0
Accepted
time: 0ms
memory: 3760kb

input:

10 27

output:

8
1 2 1
4 3 0 4 1 6 1 5 0
1 7 1
1 6 0
2 6 0 6 1
2 7 0 7 1
2 8 0 8 1
0

result:

ok ok

Test #3:

score: 0
Accepted
time: 0ms
memory: 3532kb

input:

5 13

output:

7
1 2 1
4 3 0 4 1 6 1 5 0
1 7 1
1 6 0
2 6 0 6 1
2 7 0 7 1
0

result:

ok ok

Test #4:

score: 0
Accepted
time: 0ms
memory: 3460kb

input:

1 1000000

output:

39
20 2 1 39 1 38 1 37 1 36 1 35 1 34 1 33 1 32 1 31 1 30 1 29 1 28 1 27 1 26 1 25 1 24 1 23 1 22 1 21 1
2 3 1 21 0
2 4 1 22 0
2 5 1 23 0
1 6 0
2 7 1 25 0
1 8 0
1 9 0
1 10 0
1 11 0
2 12 1 30 0
1 13 0
1 14 0
2 15 1 33 0
1 16 0
1 17 0
1 18 0
1 19 0
1 20 0
1 39 0
2 22 0 22 1
2 23 0 23 1
2 24 0 24 1
2 2...

result:

ok ok

Test #5:

score: 0
Accepted
time: 1ms
memory: 3600kb

input:

1 1

output:

2
1 2 1
0

result:

ok ok

Test #6:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

7 9

output:

6
1 2 1
2 4 0 3 1
1 6 1
1 5 0
2 6 0 6 1
0

result:

ok ok

Test #7:

score: 0
Accepted
time: 0ms
memory: 3460kb

input:

3 7

output:

5
2 2 1 3 1
1 5 1
2 4 0 4 1
2 5 0 5 1
0

result:

ok ok

Test #8:

score: 0
Accepted
time: 0ms
memory: 3456kb

input:

1 5

output:

4
3 2 1 4 1 3 1
1 3 0
2 4 0 4 1
0

result:

ok ok

Test #9:

score: 0
Accepted
time: 0ms
memory: 3504kb

input:

1 4

output:

5
3 2 1 5 1 4 1
1 3 0
1 5 0
2 5 0 5 1
0

result:

ok ok

Test #10:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

8 9

output:

5
1 2 1
1 3 0
1 4 0
2 5 0 5 1
0

result:

ok ok

Test #11:

score: 0
Accepted
time: 0ms
memory: 3784kb

input:

7 51

output:

9
3 2 1 6 1 5 1
2 3 1 5 0
2 4 0 9 1
1 7 0
2 6 0 6 1
2 7 0 7 1
2 8 0 8 1
2 9 0 9 1
0

result:

ok ok

Test #12:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

51 79

output:

12
1 2 1
2 7 0 3 1
2 4 0 9 1
2 5 0 10 1
1 6 1
1 12 1
1 8 0
2 9 0 9 1
2 10 0 10 1
2 11 0 11 1
2 12 0 12 1
0

result:

ok ok

Test #13:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

92 99

output:

11
1 2 1
2 3 0 6 1
1 4 1
1 5 1
1 9 1
1 7 0
1 8 0
1 9 0
2 10 0 10 1
2 11 0 11 1
0

result:

ok ok

Test #14:

score: 0
Accepted
time: 0ms
memory: 3600kb

input:

27 36

output:

12
1 2 1
2 6 0 3 1
2 4 0 10 1
1 5 1
1 12 1
1 7 0
2 8 1 10 0
1 9 0
1 12 0
2 11 0 11 1
2 12 0 12 1
0

result:

ok ok

Test #15:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

55 84

output:

16
1 2 1
2 7 0 3 1
2 4 0 13 1
1 5 1
1 6 1
1 16 1
2 8 1 12 0
1 9 0
2 10 1 14 0
1 11 0
1 16 0
2 13 0 13 1
2 14 0 14 1
2 15 0 15 1
2 16 0 16 1
0

result:

ok ok

Test #16:

score: 0
Accepted
time: 0ms
memory: 3744kb

input:

297208 929600

output:

53
1 2 1
4 3 0 17 1 36 1 35 0
2 4 0 37 1
1 5 1
2 6 0 39 1
2 7 0 40 1
2 8 0 41 1
1 9 1
2 10 0 43 1
2 11 0 44 1
2 12 0 45 1
1 13 1
1 14 1
1 15 1
1 16 1
1 50 1
2 18 1 36 0
1 19 0
1 20 0
1 21 0
2 22 1 40 0
1 23 0
2 24 1 42 0
2 25 1 43 0
2 26 1 44 0
2 27 1 45 0
1 28 0
2 29 1 47 0
1 30 0
1 31 0
1 32 0
1 3...

result:

ok ok

Test #17:

score: 0
Accepted
time: 0ms
memory: 3820kb

input:

45728 589156

output:

47
4 2 1 31 1 30 1 29 1
2 3 0 33 1
2 12 0 4 1
1 5 1
2 6 0 36 1
2 7 0 37 1
1 8 1
2 9 0 39 1
1 10 1
2 11 0 41 1
1 42 1
1 13 0
2 14 1 32 0
2 15 1 33 0
2 16 1 34 0
2 17 1 35 0
2 18 1 36 0
2 19 1 37 0
1 20 0
2 21 1 39 0
1 22 0
2 23 1 41 0
2 24 1 42 0
1 25 0
1 26 0
2 27 1 45 0
1 28 0
1 47 0
2 30 0 30 1
2 ...

result:

ok ok

Test #18:

score: 0
Accepted
time: 0ms
memory: 3736kb

input:

129152 138000

output:

39
1 2 1
2 11 0 3 1
1 4 1
1 5 1
1 6 1
1 7 1
2 8 0 29 1
2 9 0 30 1
2 10 0 31 1
1 32 1
1 12 0
1 13 0
1 14 0
2 15 1 27 0
2 16 1 28 0
1 17 0
2 18 1 30 0
2 19 1 31 0
1 20 0
1 21 0
1 22 0
2 23 1 35 0
1 24 0
1 25 0
1 26 0
1 39 0
2 28 0 28 1
2 29 0 29 1
2 30 0 30 1
2 31 0 31 1
2 32 0 32 1
2 33 0 33 1
2 34 0...

result:

ok ok

Test #19:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

245280 654141

output:

49
2 2 1 31 1
2 14 0 3 1
1 4 1
2 5 0 35 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
2 11 0 41 1
2 12 0 42 1
2 13 0 43 1
1 44 1
1 15 0
2 16 1 33 0
2 17 1 34 0
2 18 1 35 0
2 19 1 36 0
2 20 1 37 0
2 21 1 38 0
1 22 0
2 23 1 40 0
2 24 1 41 0
1 25 0
1 26 0
2 27 1 44 0
2 28 1 45 0
2 29 1 46 0
2 30 1 47 0
1 48 0
2 32 ...

result:

ok ok

Test #20:

score: 0
Accepted
time: 0ms
memory: 3596kb

input:

202985 296000

output:

51
1 2 1
2 19 0 3 1
2 4 0 36 1
2 5 0 37 1
2 6 0 38 1
1 7 1
1 8 1
2 9 0 41 1
2 10 0 42 1
2 11 0 43 1
1 12 1
1 13 1
1 14 1
2 15 0 47 1
1 16 1
2 17 0 49 1
2 18 0 50 1
1 51 1
1 20 0
2 21 1 36 0
1 22 0
1 23 0
1 24 0
1 25 0
2 26 1 41 0
1 27 0
1 28 0
1 29 0
2 30 1 45 0
1 31 0
1 32 0
1 33 0
1 34 0
1 35 0
1 ...

result:

ok ok

Test #21:

score: 0
Accepted
time: 0ms
memory: 3588kb

input:

438671 951305

output:

54
1 2 1
2 3 1 36 0
4 4 0 20 1 38 1 37 0
1 5 1
2 6 0 40 1
1 7 1
1 8 1
2 9 0 43 1
2 10 0 44 1
2 11 0 45 1
1 12 1
1 13 1
2 14 0 48 1
2 15 0 49 1
2 16 0 50 1
1 17 1
1 18 1
1 19 1
1 54 1
1 21 0
2 22 1 39 0
1 23 0
1 24 0
1 25 0
1 26 0
2 27 1 44 0
1 28 0
1 29 0
1 30 0
1 31 0
1 32 0
1 33 0
2 34 1 51 0
1 35...

result:

ok ok

Test #22:

score: 0
Accepted
time: 0ms
memory: 3760kb

input:

425249 739633

output:

54
1 2 1
2 20 0 3 1
2 4 0 38 1
2 5 0 39 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
2 11 0 45 1
1 12 1
2 13 0 47 1
2 14 0 48 1
1 15 1
2 16 0 50 1
2 17 0 51 1
2 18 0 52 1
2 19 0 53 1
1 54 1
2 21 1 37 0
2 22 1 38 0
1 23 0
2 24 1 40 0
1 25 0
1 26 0
2 27 1 43 0
1 28 0
1 29 0
2 30 1 46 0
1 31 0
1 32 0
2 33 1 49 0
2...

result:

ok ok

Test #23:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

551207 961718

output:

56
1 2 1
2 3 0 21 1
2 4 0 39 1
2 5 0 40 1
2 6 0 41 1
1 7 1
1 8 1
2 9 0 44 1
1 10 1
2 11 0 46 1
2 12 0 47 1
1 13 1
2 14 0 49 1
2 15 0 50 1
1 16 1
2 17 0 52 1
2 18 0 53 1
1 19 1
1 20 1
1 56 1
2 22 1 39 0
1 23 0
2 24 1 41 0
1 25 0
2 26 1 43 0
1 27 0
2 28 1 45 0
2 29 1 46 0
1 30 0
1 31 0
2 32 1 49 0
1 3...

result:

ok ok

Test #24:

score: 0
Accepted
time: 0ms
memory: 3500kb

input:

114691 598186

output:

54
3 2 1 37 1 36 1
2 18 0 3 1
1 4 1
2 5 0 41 1
2 6 0 42 1
2 7 0 43 1
2 8 0 44 1
2 9 0 45 1
2 10 0 46 1
2 11 0 47 1
2 12 0 48 1
2 13 0 49 1
2 14 0 50 1
2 15 0 51 1
2 16 0 52 1
1 17 1
1 54 1
1 19 0
2 20 1 38 0
1 21 0
1 22 0
2 23 1 41 0
1 24 0
1 25 0
1 26 0
1 27 0
1 28 0
2 29 1 47 0
1 30 0
2 31 1 49 0
...

result:

ok ok

Test #25:

score: 0
Accepted
time: 0ms
memory: 3592kb

input:

234654 253129

output:

44
1 2 1
1 3 1
1 4 1
2 5 0 18 1
2 6 0 31 1
1 7 1
2 8 0 33 1
1 9 1
2 10 0 35 1
2 11 0 36 1
1 12 1
2 13 0 38 1
2 14 0 39 1
1 15 1
1 16 1
1 17 1
1 43 1
1 19 0
2 20 1 32 0
2 21 1 33 0
2 22 1 34 0
1 23 0
1 24 0
2 25 1 37 0
2 26 1 38 0
1 27 0
1 28 0
2 29 1 41 0
1 30 0
1 43 0
2 32 0 32 1
2 33 0 33 1
2 34 0...

result:

ok ok

Test #26:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

554090 608599

output:

48
1 2 1
1 3 0
1 4 0
2 5 0 20 1
2 6 0 33 1
1 7 1
1 8 1
1 9 1
2 10 0 37 1
1 11 1
2 12 0 39 1
2 13 0 40 1
2 14 0 41 1
1 15 1
1 16 1
2 17 0 44 1
1 18 1
2 19 0 46 1
1 47 1
1 21 0
2 22 1 34 0
1 23 0
1 24 0
2 25 1 37 0
1 26 0
1 27 0
2 28 1 40 0
1 29 0
2 30 1 42 0
1 31 0
2 32 1 44 0
1 45 0
2 34 0 34 1
2 35...

result:

ok ok

Extra Test:

score: 0
Extra Test Passed