QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#689456#9519. Build a ComputerKeeperHihiAC ✓11ms3864kbC++205.6kb2024-10-30 17:10:162024-10-30 17:10:17

Judging History

This is the latest submission verdict.

  • [2024-10-30 17:10:17]
  • Judged
  • Verdict: AC
  • Time: 11ms
  • Memory: 3864kb
  • [2024-10-30 17:10:16]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
vector<vector<pair<int, int>>> adj(101);
int l, r;

void solve() {
    cin >> l >> r;
    int rb = 0, lb = 0;
    for (int i = 25; i >= 0; i--) {
        if (r >> i & 1) {
            rb = i;
            break;
        }
    }
    for (int i = 25; i >= 0; i--) {
        if (l >> i & 1) {
            lb = i;
            break;
        }
    }

    int n = 100;
    vector<vector<int>> ans(n + 1);
    int idx = 2;
    auto print = [&]() {
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < ans[i].size(); j += 2) {
                // cout << i << ' ' << ans[i][j] << ' ' << ans[i][j + 1] << endl;
                adj[i].emplace_back(ans[i][j], ans[i][j + 1]);
            }
        }
    };
    auto addedge = [&](int a, int b, int w) {
    	assert(idx <= 100);
        // cout << a << ' ' << b << ' ' << w << endl;
        ans[a].emplace_back(b);
        ans[a].emplace_back(w);
    };

    int s = 1, t = n;

    for (int i = 80; i < 100; i++) {
        addedge(i, i + 1, 1);
        addedge(i, i + 1, 0);
    }

    auto add_power_edge = [&](int idx, int bit, int w) {
        // 目的地是 bit 位
        addedge(idx, 99 - bit, w);
    };

    auto work = [&](int l, int r) {
        // 保证进入时,idx 是一个新的空节点,返回时,idx 是一个新的节点
    	// cout << l << ' ' << r << endl;
    	// cout << bitset<21>(l) << ' ' << bitset<21>(r) << endl;
        if (l > r) return;
        int rb = 0, lb = 0, diff = 0;
        for (int i = 25; i >= 0; i--) {
            if (r >> i & 1) {
                rb = i;
                break;
            }
        }
        for (int i = 25; i >= 0; i--) {
            if (l >> i & 1) {
                lb = i;
                break;
            }
        }
        for (int i = rb; i >= 0; i--) {
            if ((r >> i & 1) && !(l >> i & 1)) {
                diff = i;
                break;
            }
        }
        assert(rb == lb);
        if (l == r) {
            if (l == 1) {
                addedge(s, t, 1);
            } else {
                addedge(s, idx, 1);
                for (int i = rb - 1; i > 0; i--) {
                    addedge(idx, idx + 1, r >> i & 1);
                    idx++;
                }
                addedge(idx, t, r & 1);
                idx++;
            }
            return;
        }

        if (diff == 0) {
            assert(lb > 0);
            addedge(s, idx, 1);
            for (int i = rb - 1; i > 0; i--) {
                addedge(idx, idx + 1, r >> i & 1);
                idx++;
            }
            addedge(idx, t, 1);
            addedge(idx, t, 0);
            idx++;
            return;
        }

        // cout << l << ' ' << r << ' ' << rb << ' ' << lb << ' ' << diff << endl;
        addedge(s, idx, 1);
        assert(diff < rb);

        for (int i = rb - 1; i > diff; i--) {
            addedge(idx, idx + 1, r >> i & 1);
            idx++;
        }
        int pre = idx;

        addedge(idx, idx + 1, 1);
        idx++;
        for (int i = diff - 1; i > 0; i--) {
            if (r >> i & 1) { // 这里有大问题!!!
                add_power_edge(idx, i - 1, 0);
                addedge(idx, idx + 1, 1);
                idx++;
            } else {
                addedge(idx, idx + 1, 0);
                idx++;
            }
        }
        if (r & 1) {
            addedge(idx, t, 1);
            addedge(idx, t, 0);
        } else {
            addedge(idx, t, 0);
        }

        addedge(pre, idx + 1, 0);
        idx++;
        for (int i = diff - 1; i > 0; i--) {
            if (l >> i & 1) {
                addedge(idx, idx + 1, 1);
                idx++;
            } else {
                add_power_edge(idx, i - 1, 1);
                addedge(idx, idx + 1, 0);
                idx++;
            }
        }
        if (l & 1) {
            addedge(idx, t, 1);
        } else {
            addedge(idx, t, 1);
            addedge(idx, t, 0);
        }
        idx++;
    };

    if (rb <= lb) {
        work(l, r);
        print();
        return;
    }
    work(1 << rb, r);
    work(l, (1 << lb + 1) - 1);

    if (rb - 1 >= lb + 1) {
        for (int i = rb - 1; i >= lb + 1; i--) {
            addedge(s, 100 - i, 1);
        }
    }
    print();

}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }

	vector<set<pair<int, int>>> g(101);
    auto dfs = [&](auto self, int u) -> void {
        for (auto [v, w] : adj[u]) {
            g[u].insert({v, w});
            self(self, v);
        }
    };
    dfs(dfs, 1);
    for (int i = 1; i <= 100; i++) {
        adj[i].clear();
        for (auto [v, w] : g[i]) {
            adj[i].emplace_back(v, w);
        }
    }

    int a = 1, b = 99;
    while (!adj[a].empty()) {
    	a++;
    }
    while (!adj[b].empty()) {
    	b--;
    }
    b++;
    map<int, int> map;
    while (b <= 100) {
    	swap(adj[a], adj[b]);
    	map[b] = a;
    	a++, b++;
    }
    for (int i = 1; i <= a; i++) {
    	for (auto &[x, y] : adj[i]) {
    		if (map.count(x)) x = map[x];
    	}
    }
 	
 	cout << a - 1 << "\n";
    for (int i = 1; i < a; i++) {
    	cout << adj[i].size() << " ";
        for (auto [v, w] : adj[i]) {
            cout << v << ' ' << w << " ";
        }
        cout << "\n";
    }

    return 0;
}

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

详细

Test #1:

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

input:

5 7

output:

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

result:

ok ok

Test #2:

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

input:

10 27

output:

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

result:

ok ok

Test #3:

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

input:

5 13

output:

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

result:

ok ok

Test #4:

score: 0
Accepted
time: 11ms
memory: 3632kb

input:

1 1000000

output:

57
20 2 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 
2 3 1 21 0 
2 4 1 40 0 
2 5 1 41 0 
1 6 0 
2 7 1 43 0 
1 8 0 
1 9 0 
1 10 0 
1 11 0 
2 12 1 48 0 
1 13 0 
1 14 0 
2 15 1 51 0 
1 16 0 
1 17 0 
1 18 0 
1 19 0 
1 20 0 
1 57 0 
2 22 0 40 1 
2 23 0...

result:

ok ok

Test #5:

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

input:

1 1

output:

2
1 2 1 
0 

result:

ok ok

Test #6:

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

input:

7 9

output:

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

result:

ok ok

Test #7:

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

input:

3 7

output:

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

result:

ok ok

Test #8:

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

input:

1 5

output:

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

result:

ok ok

Test #9:

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

input:

1 4

output:

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

result:

ok ok

Test #10:

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

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: 3580kb

input:

7 51

output:

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

result:

ok ok

Test #12:

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

input:

51 79

output:

21
2 2 1 11 1 
1 3 0 
1 4 0 
2 5 1 8 0 
2 6 1 19 0 
2 7 1 20 0 
2 21 0 21 1 
2 9 0 19 1 
2 10 0 20 1 
2 21 0 21 1 
1 12 1 
2 13 1 16 0 
2 14 1 19 0 
2 15 1 20 0 
2 21 0 21 1 
2 17 0 19 1 
1 18 1 
1 21 1 
2 20 0 20 1 
2 21 0 21 1 
0 

result:

ok ok

Test #13:

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

input:

92 99

output:

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

result:

ok ok

Test #14:

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

input:

27 36

output:

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

result:

ok ok

Test #15:

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

input:

55 84

output:

23
2 2 1 12 1 
1 3 0 
2 4 1 8 0 
1 5 0 
2 6 1 21 0 
1 7 0 
1 23 0 
2 9 0 20 1 
2 10 0 21 1 
2 11 0 22 1 
2 23 0 23 1 
1 13 1 
2 14 1 17 0 
2 15 1 21 0 
2 16 1 22 0 
2 23 0 23 1 
1 18 1 
1 19 1 
1 23 1 
2 21 0 21 1 
2 22 0 22 1 
2 23 0 23 1 
0 

result:

ok ok

Test #16:

score: 0
Accepted
time: 7ms
memory: 3520kb

input:

297208 929600

output:

91
2 2 1 39 1 
2 3 1 21 0 
2 4 1 74 0 
1 5 0 
1 6 0 
1 7 0 
2 8 1 78 0 
1 9 0 
2 10 1 80 0 
2 11 1 81 0 
2 12 1 82 0 
2 13 1 83 0 
1 14 0 
2 15 1 85 0 
1 16 0 
1 17 0 
1 18 0 
1 19 0 
1 20 0 
1 91 0 
2 22 0 74 1 
2 23 0 75 1 
2 24 0 76 1 
2 25 0 77 1 
2 26 0 78 1 
2 27 0 79 1 
2 28 0 80 1 
2 29 0 81...

result:

ok ok

Test #17:

score: 0
Accepted
time: 6ms
memory: 3640kb

input:

45728 589156

output:

83
5 2 1 36 1 65 1 66 1 67 1 
1 3 0 
1 4 0 
1 5 0 
2 6 1 21 0 
2 7 1 69 0 
2 8 1 70 0 
2 9 1 71 0 
2 10 1 72 0 
2 11 1 73 0 
1 12 0 
2 13 1 75 0 
1 14 0 
2 15 1 77 0 
2 16 1 78 0 
1 17 0 
1 18 0 
2 19 1 81 0 
1 20 0 
1 83 0 
2 22 0 69 1 
2 23 0 70 1 
2 24 0 71 1 
2 25 0 72 1 
2 26 0 73 1 
2 27 0 74 ...

result:

ok ok

Test #18:

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

input:

129152 138000

output:

68
2 2 1 31 1 
1 3 0 
1 4 0 
1 5 0 
1 6 0 
2 7 1 19 0 
2 8 1 57 0 
1 9 0 
2 10 1 59 0 
2 11 1 60 0 
1 12 0 
1 13 0 
1 14 0 
2 15 1 64 0 
1 16 0 
1 17 0 
1 18 0 
1 68 0 
2 20 0 57 1 
2 21 0 58 1 
2 22 0 59 1 
2 23 0 60 1 
2 24 0 61 1 
2 25 0 62 1 
2 26 0 63 1 
2 27 0 64 1 
2 28 0 65 1 
2 29 0 66 1 
2...

result:

ok ok

Test #19:

score: 0
Accepted
time: 5ms
memory: 3616kb

input:

245280 654141

output:

86
3 2 1 37 1 68 1 
1 3 0 
1 4 0 
2 5 1 21 0 
2 6 1 71 0 
2 7 1 72 0 
2 8 1 73 0 
2 9 1 74 0 
2 10 1 75 0 
1 11 0 
2 12 1 77 0 
2 13 1 78 0 
1 14 0 
1 15 0 
2 16 1 81 0 
2 17 1 82 0 
2 18 1 83 0 
2 19 1 84 0 
1 20 0 
2 86 0 86 1 
2 22 0 71 1 
2 23 0 72 1 
2 24 0 73 1 
2 25 0 74 1 
2 26 0 75 1 
2 27 ...

result:

ok ok

Test #20:

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

input:

202985 296000

output:

81
2 2 1 35 1 
1 3 0 
1 4 0 
2 5 1 20 0 
1 6 0 
1 7 0 
1 8 0 
1 9 0 
2 10 1 71 0 
1 11 0 
1 12 0 
1 13 0 
2 14 1 75 0 
1 15 0 
1 16 0 
1 17 0 
1 18 0 
1 19 0 
1 81 0 
2 21 0 67 1 
2 22 0 68 1 
2 23 0 69 1 
2 24 0 70 1 
2 25 0 71 1 
2 26 0 72 1 
2 27 0 73 1 
2 28 0 74 1 
2 29 0 75 1 
2 30 0 76 1 
2 3...

result:

ok ok

Test #21:

score: 0
Accepted
time: 3ms
memory: 3644kb

input:

438671 951305

output:

90
2 2 1 39 1 
2 3 1 21 0 
2 4 1 73 0 
1 5 0 
2 6 1 75 0 
1 7 0 
1 8 0 
1 9 0 
1 10 0 
2 11 1 80 0 
1 12 0 
1 13 0 
1 14 0 
1 15 0 
1 16 0 
1 17 0 
2 18 1 87 0 
1 19 0 
1 20 0 
2 90 0 90 1 
2 22 0 73 1 
2 23 0 74 1 
2 24 0 75 1 
2 25 0 76 1 
2 26 0 77 1 
2 27 0 78 1 
2 28 0 79 1 
2 29 0 80 1 
2 30 0...

result:

ok ok

Test #22:

score: 0
Accepted
time: 4ms
memory: 3580kb

input:

425249 739633

output:

88
2 2 1 38 1 
1 3 0 
2 4 1 21 0 
2 5 1 72 0 
1 6 0 
2 7 1 74 0 
1 8 0 
1 9 0 
2 10 1 77 0 
1 11 0 
1 12 0 
2 13 1 80 0 
1 14 0 
1 15 0 
2 16 1 83 0 
2 17 1 84 0 
1 18 0 
1 19 0 
1 20 0 
2 88 0 88 1 
2 22 0 72 1 
2 23 0 73 1 
2 24 0 74 1 
2 25 0 75 1 
2 26 0 76 1 
2 27 0 77 1 
2 28 0 78 1 
2 29 0 79...

result:

ok ok

Test #23:

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

input:

551207 961718

output:

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

result:

ok ok

Test #24:

score: 0
Accepted
time: 5ms
memory: 3832kb

input:

114691 598186

output:

84
4 2 1 37 1 66 1 67 1 
1 3 0 
1 4 0 
2 5 1 21 0 
1 6 0 
1 7 0 
2 8 1 71 0 
1 9 0 
1 10 0 
1 11 0 
1 12 0 
1 13 0 
2 14 1 77 0 
1 15 0 
2 16 1 79 0 
1 17 0 
2 18 1 81 0 
1 19 0 
2 20 1 83 0 
1 84 0 
2 22 0 69 1 
2 23 0 70 1 
2 24 0 71 1 
2 25 0 72 1 
2 26 0 73 1 
2 27 0 74 1 
2 28 0 75 1 
2 29 0 76...

result:

ok ok

Test #25:

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

input:

234654 253129

output:

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

result:

ok ok

Test #26:

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

input:

554090 608599

output:

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

result:

ok ok

Extra Test:

score: 0
Extra Test Passed