QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#697944 | #9519. Build a Computer | hkr04 | AC ✓ | 56ms | 8552kb | C++14 | 3.7kb | 2024-11-01 16:32:18 | 2024-11-01 16:32:18 |
Judging History
answer
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 100 + 10;
vector<int> master_id;
struct Node {
int ch[2] = {0, 0};
} t[maxn];
int tot;
int read() {
int res = 0, p = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
p = -1;
ch = getchar();
}
while (isdigit(ch))
res = res * 10 + ch - '0', ch = getchar();
return res * p;
}
int high_bit(int x) {
int res = 0;
for (int i = 0; x; i++) {
if (x & 1)
res = i;
x >>= 1;
}
return res;
}
bool is_power(int x) {
return (x & (x - 1)) == 0;
}
int get_master(int pos) {
if (master_id.size() - 1 < pos) {
for (int i = master_id.size(); i <= pos; i++) {
++tot;
t[tot].ch[0] = t[tot].ch[1] = master_id[i - 1];
master_id.push_back(tot);
}
}
return master_id[pos];
}
void solve(int l, int r, int pos, int id) {
int lv = l >> pos & 1;
int rv = r >> pos & 1;
if (pos == 0) {
t[id].ch[lv] = t[id].ch[rv] = get_master(pos);
return;
}
if (lv == rv) {
t[id].ch[lv] = ++tot;
if (lv)
solve(l - (1 << pos), r - (1 << pos), pos - 1, tot);
else
solve(l, r, pos - 1, tot);
} else {
if (l == 0) {
t[id].ch[0] = get_master(pos);
t[id].ch[1] = ++tot;
solve(0, r - (1 << pos), pos - 1, tot);
} else if (r == (1 << (pos + 1)) - 1) {
t[id].ch[0] = ++tot;
solve(l, (1 << pos) - 1, pos - 1, tot);
t[id].ch[1] = get_master(pos);
} else {
t[id].ch[0] = ++tot;
solve(l, (1 << pos) - 1, pos - 1, tot);
t[id].ch[1] = ++tot;
solve(0, r - (1 << pos), pos - 1, tot);
}
}
}
vector<int> path, tmp;
void dfs(int u, int val) {
if (u == 2) {
path.push_back(val);
return;
}
for (int i = 0; i < 2; i++)
if (t[u].ch[i])
dfs(t[u].ch[i], val * 2 + i);
if (u == 1)
for (int id : tmp)
dfs(id, val * 2 + 1);
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int l = read(), r = read();
master_id.push_back(2); // 叶子
tot = 2;
solve(l, r, high_bit(r), 1);
// 压缩前导零
int len = 0, cur = 1;
while (t[cur].ch[0] != 0) {
len++;
int id = t[cur].ch[0];
t[cur].ch[0] = 0;
cur = id;
if (t[cur].ch[1])
tmp.push_back(t[cur].ch[1]);
}
dfs(1, 0);
sort(path.begin(), path.end());
// cout << "----" << path.size() << endl;
// for (int val : path)
// cout << val << endl;
cout << tot - len << endl;
for (int i = 1; i <= tot; i++) {
if (i > 2 && i <= 2 + len)
continue;
int cnt = (t[i].ch[0] != 0) + (t[i].ch[1] != 0);
if (i == 1)
cnt += tmp.size();
// cout << "----" << i << ' ';
cout << cnt << ' ';
for (int j = 0; j < 2; j++) {
if (t[i].ch[j]) {
if (t[i].ch[j] == 2)
cout << t[i].ch[j] << ' ' << j << ' ' ;
else
cout << t[i].ch[j] - len << ' ' << j << ' ' ;
}
}
if (i == 1) {
for (int id : tmp) {
if (id == 2)
cout << id << ' ' << 1 << ' ' ;
else
cout << id - len << ' ' << 1 << ' ' ;
}
}
cout << endl;
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3592kb
input:
5 7
output:
5 1 3 1 0 2 4 0 5 1 1 2 1 2 2 0 2 1
result:
ok ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 3724kb
input:
10 27
output:
12 2 8 1 3 1 0 2 4 0 7 1 1 5 1 2 2 0 2 1 2 2 0 2 1 2 6 0 6 1 2 9 0 10 1 2 7 0 7 1 1 11 0 2 6 0 12 1 2 2 0 2 1
result:
ok ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
5 13
output:
9 2 6 1 3 1 0 2 4 0 5 1 1 2 1 2 2 0 2 1 2 7 0 8 1 2 5 0 5 1 1 9 0 2 2 0 2 1
result:
ok ok
Test #4:
score: 0
Accepted
time: 56ms
memory: 8552kb
input:
1 1000000
output:
39 20 21 1 20 1 19 1 18 1 17 1 16 1 15 1 14 1 13 1 12 1 11 1 10 1 9 1 8 1 7 1 6 1 5 1 4 1 3 1 2 1 0 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 2 17 0 17 1 2 18 0...
result:
ok ok
Test #5:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
1 1
output:
2 1 2 1 0
result:
ok ok
Test #6:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
7 9
output:
7 2 5 1 3 1 0 1 4 1 1 2 1 1 6 0 1 7 0 2 2 0 2 1
result:
ok ok
Test #7:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
3 7
output:
5 2 5 1 3 1 0 1 2 1 2 2 0 2 1 2 4 0 4 1
result:
ok ok
Test #8:
score: 0
Accepted
time: 0ms
memory: 3728kb
input:
1 5
output:
5 3 4 1 3 1 2 1 0 2 2 0 2 1 1 5 0 2 2 0 2 1
result:
ok ok
Test #9:
score: 0
Accepted
time: 0ms
memory: 3720kb
input:
1 4
output:
5 3 4 1 3 1 2 1 0 2 2 0 2 1 1 5 0 1 2 0
result:
ok ok
Test #10:
score: 0
Accepted
time: 0ms
memory: 3736kb
input:
8 9
output:
5 1 3 1 0 1 4 0 1 5 0 2 2 0 2 1
result:
ok ok
Test #11:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
7 51
output:
13 4 9 1 8 1 7 1 3 1 0 1 4 1 1 2 1 2 2 0 2 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 10 1 1 11 0 1 12 0 2 5 0 13 1 2 2 0 2 1
result:
ok ok
Test #12:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
51 79
output:
16 2 11 1 3 1 0 1 4 1 2 5 0 10 1 2 6 0 9 1 1 7 1 1 2 1 2 2 0 2 1 2 8 0 8 1 2 9 0 9 1 1 12 0 1 13 0 2 10 0 14 1 2 9 0 15 1 2 8 0 16 1 2 2 0 2 1
result:
ok ok
Test #13:
score: 0
Accepted
time: 0ms
memory: 3728kb
input:
92 99
output:
14 1 3 1 0 2 4 0 10 1 1 5 1 1 6 1 1 7 1 2 8 0 9 1 2 2 0 2 1 2 2 0 2 1 1 11 0 1 12 0 1 13 0 2 8 0 14 1 2 2 0 2 1
result:
ok ok
Test #14:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
27 36
output:
13 2 9 1 3 1 0 1 4 1 2 5 0 8 1 1 6 1 1 2 1 2 2 0 2 1 2 7 0 7 1 1 10 0 1 11 0 2 8 0 12 1 1 13 0 1 2 0
result:
ok ok
Test #15:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
55 84
output:
17 2 11 1 3 1 0 1 4 1 2 5 0 10 1 1 6 1 1 7 1 1 2 1 2 2 0 2 1 2 8 0 8 1 2 9 0 9 1 1 12 0 2 13 0 14 1 2 10 0 10 1 1 15 0 2 9 0 16 1 1 17 0 1 2 0
result:
ok ok
Test #16:
score: 0
Accepted
time: 17ms
memory: 8048kb
input:
297208 929600
output:
57 2 38 1 3 1 0 2 4 0 37 1 2 5 0 36 1 1 6 1 2 7 0 34 1 2 8 0 33 1 2 9 0 32 1 1 10 1 2 11 0 30 1 2 12 0 29 1 2 13 0 28 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 2 20 0 21 1 2 2 0 2 1 2 19 0 19 1 2 19 0 22 1 2 2 0 2 1 2 20 0 20 1 2 23 0 23 1 2 24 0 24 1 2 25 0 25 1 2 26 0 26 1 2 ...
result:
ok ok
Test #17:
score: 0
Accepted
time: 33ms
memory: 8016kb
input:
45728 589156
output:
54 5 36 1 35 1 34 1 33 1 3 1 0 2 4 0 31 1 1 5 1 1 6 1 2 7 0 28 1 2 8 0 27 1 1 9 1 2 10 0 25 1 1 11 1 2 12 0 23 1 1 13 1 2 17 0 18 1 2 2 0 2 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 2 16 0 19 1 2 15 0 20 1 2 14 0 21 1 2 2 0 2 1 2 17 0 17 1 2 22 0 22 1 2 23 0 23 1 2 24 0 24 1 2 2...
result:
ok ok
Test #18:
score: 0
Accepted
time: 1ms
memory: 3692kb
input:
129152 138000
output:
47 2 29 1 3 1 0 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 2 9 0 28 1 2 10 0 27 1 2 11 0 26 1 1 12 1 2 18 0 19 1 2 2 0 2 1 2 13 0 13 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 2 17 0 17 1 2 17 0 20 1 2 16 0 21 1 2 15 0 22 1 2 14 0 23 1 2 13 0 24 1 2 2 0 2 1 2 18 0 18 1 2 25 0 25 1 2 26 0 26 ...
result:
ok ok
Test #19:
score: 0
Accepted
time: 25ms
memory: 5204kb
input:
245280 654141
output:
56 3 38 1 37 1 3 1 0 1 4 1 1 5 1 2 6 0 33 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 2 12 0 27 1 2 13 0 26 1 2 14 0 25 1 1 15 1 2 19 0 20 1 2 2 0 2 1 2 16 0 16 1 2 17 0 17 1 2 18 0 18 1 2 18 0 21 1 2 17 0 22 1 2 16 0 23 1 2 2 0 2 1 2 19 0 19 1 2 24 0 24 1 2 25 0 25 1 2 26 0 26 1 2 ...
result:
ok ok
Test #20:
score: 0
Accepted
time: 3ms
memory: 3716kb
input:
202985 296000
output:
52 2 35 1 3 1 0 1 4 1 2 5 0 34 1 2 6 0 33 1 2 7 0 32 1 1 8 1 1 9 1 2 10 0 29 1 2 11 0 28 1 2 12 0 27 1 1 13 1 1 14 1 1 15 1 2 16 0 23 1 1 17 1 2 18 0 21 1 2 19 0 20 1 1 2 1 2 2 0 2 1 2 20 0 20 1 2 21 0 21 1 2 22 0 22 1 2 23 0 23 1 2 24 0 24 1 2 25 0 25 1 2 26 0 26 1 2 27 0...
result:
ok ok
Test #21:
score: 0
Accepted
time: 23ms
memory: 5156kb
input:
438671 951305
output:
57 2 37 1 3 1 0 1 4 1 2 5 0 36 1 1 6 1 2 7 0 34 1 1 8 1 1 9 1 2 10 0 31 1 2 11 0 30 1 2 12 0 29 1 1 13 1 1 14 1 2 15 0 26 1 2 16 0 25 1 2 17 0 24 1 1 18 1 1 19 1 1 20 1 1 2 1 2 2 0 2 1 2 21 0 21 1 2 22 0 22 1 2 23 0 23 1 2 24 0 24 1 2 25 0 25 1 2 26 0 26 1 2 27 0 27 1 2 2...
result:
ok ok
Test #22:
score: 0
Accepted
time: 7ms
memory: 5328kb
input:
425249 739633
output:
56 2 37 1 3 1 0 1 4 1 2 5 0 36 1 2 6 0 35 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 2 12 0 29 1 1 13 1 2 14 0 27 1 2 15 0 26 1 1 16 1 2 17 0 24 1 2 18 0 23 1 2 19 0 22 1 2 20 0 21 1 1 2 1 2 2 0 2 1 2 21 0 21 1 2 22 0 22 1 2 23 0 23 1 2 24 0 24 1 2 25 0 25 1 2 26 0 26 1 2 27 0 27 1...
result:
ok ok
Test #23:
score: 0
Accepted
time: 3ms
memory: 5268kb
input:
551207 961718
output:
56 1 3 1 0 2 4 0 39 1 2 5 0 38 1 2 6 0 37 1 2 7 0 36 1 1 8 1 1 9 1 2 10 0 33 1 1 11 1 2 12 0 31 1 2 13 0 30 1 1 14 1 2 15 0 28 1 2 16 0 27 1 1 17 1 2 18 0 25 1 2 19 0 24 1 1 20 1 1 21 1 1 2 1 2 2 0 2 1 2 22 0 22 1 2 23 0 23 1 2 24 0 24 1 2 25 0 25 1 2 26 0 26 1 2 27 0 27 1...
result:
ok ok
Test #24:
score: 0
Accepted
time: 18ms
memory: 5216kb
input:
114691 598186
output:
55 4 37 1 36 1 35 1 3 1 0 1 4 1 1 5 1 2 6 0 31 1 2 7 0 30 1 2 8 0 29 1 2 9 0 28 1 2 10 0 27 1 2 11 0 26 1 2 12 0 25 1 2 13 0 24 1 2 14 0 23 1 2 15 0 22 1 2 16 0 21 1 2 17 0 20 1 1 18 1 1 2 1 2 2 0 2 1 2 19 0 19 1 2 20 0 20 1 2 21 0 21 1 2 22 0 22 1 2 23 0 23 1 2 24 0 24 1 2 ...
result:
ok ok
Test #25:
score: 0
Accepted
time: 1ms
memory: 3780kb
input:
234654 253129
output:
46 1 3 1 0 1 4 1 1 5 1 2 6 0 33 1 2 7 0 32 1 1 8 1 2 9 0 30 1 1 10 1 2 11 0 28 1 2 12 0 27 1 1 13 1 2 14 0 25 1 2 15 0 24 1 1 16 1 1 17 1 1 18 1 1 19 1 2 2 0 2 1 2 2 0 2 1 2 20 0 20 1 2 21 0 21 1 2 22 0 22 1 2 23 0 23 1 2 24 0 24 1 2 25 0 25 1 2 26 0 26 1 2 27 0 27 1 2 28...
result:
ok ok
Test #26:
score: 0
Accepted
time: 1ms
memory: 3768kb
input:
554090 608599
output:
52 1 3 1 0 1 4 0 1 5 0 2 6 0 37 1 2 7 0 36 1 1 8 1 1 9 1 1 10 1 2 11 0 32 1 1 12 1 2 13 0 30 1 2 14 0 29 1 2 15 0 28 1 1 16 1 1 17 1 2 18 0 25 1 1 19 1 2 20 0 23 1 1 21 1 2 2 0 2 1 2 2 0 2 1 2 22 0 22 1 2 23 0 23 1 2 24 0 24 1 2 25 0 25 1 2 26 0 26 1 2 27 0 27 1 2 28 0 28...
result:
ok ok
Extra Test:
score: 0
Extra Test Passed