QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#679365#9519. Build a Computerucup-team3584#AC ✓1ms3840kbC++235.5kb2024-10-26 17:27:432024-10-26 17:27:44

Judging History

This is the latest submission verdict.

  • [2024-10-26 17:27:44]
  • Judged
  • Verdict: AC
  • Time: 1ms
  • Memory: 3840kb
  • [2024-10-26 17:27:43]
  • Submitted

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

void slv(int a, int b, bool DEBUG = false) {
    int A = a, B = b;
    string L, R;
    {
        while (a) {
            L += (char)('0' + a % 2);
            a /= 2;
        }
        while (b) {
            R += (char)('0' + b % 2);
            b /= 2;
        }
    }

    int n = 1, s = 0;
    vector<vector<pair<int, int>>> g(100);
    while (L.size() == R.size() and !L.empty() and L.back() == R.back()) {
        g[n - 1].emplace_back(n, L.back() - '0');
        L.pop_back();
        R.pop_back();
        n += 1, s += 1;
    }

    if (!R.empty()) {
        while (L.size() < R.size()) {
            L.push_back('0');
        }
        reverse(L.begin(), L.end());
        reverse(R.begin(), R.end());
        if (L.size() == 1) {
            g[n - 1].emplace_back(n, 0);
            g[n - 1].emplace_back(n, 1);
            n += 1;
        } else {
            // ゴール作った
            n += 1;
            int l = n - 1, r = l;
            assert(L[0] == '0' and R[0] == '1');

            {
                int mx = 0;
                for (int i = 1; i < R.size(); ++i) {
                    if (R[i] == '1') {
                        mx = max(mx, (int)R.size() - 1 - i);
                        break;
                    }
                }
                for (int i = 1; i < L.size(); ++i) {
                    if (L[i] == '0') {
                        mx = max(mx, (int)L.size() - i - 1);
                        break;
                    }
                }
                for (int i = 0; i < mx; ++i) {
                    g[n - 1].emplace_back(n, 0);
                    g[n - 1].emplace_back(n, 1);
                    r += 1, n += 1;
                }
            }
            // r制約気にする方
            {
                g[s].emplace_back(n, 1);
                n += 1;
                int cur = n - 1;
                for (int i = 1; i < R.size(); ++i) {
                    if (R[i] == '1') {
                        g[cur].emplace_back(r - (R.size() - 1 - i), 0);
                    }
                    int to = (i == R.size() - 1 ? r : n);
                    if (to == n) {
                        n += 1;
                    }
                    g[cur].emplace_back(to, R[i] - '0');
                    cur = to;
                }
            }
            // l制約気にする方
            if (s != 0) {
                g[s].emplace_back(n, 0);
                n += 1;
                int cur = n - 1;
                for (int i = 1; i < L.size(); ++i) {
                    if (L[i] == '0') {
                        g[cur].emplace_back(r - (L.size() - 1 - i), 1);
                    }
                    int to = (i == L.size() - 1 ? r : n);
                    if (to == n) {
                        n += 1;
                    }
                    g[cur].emplace_back(to, L[i] - '0');
                    cur = to;
                }
            } else {
                int cur = s;
                for (int i = 1; i < L.size(); ++i) {
                    if (i + 1 != L.size()) {
                        if (L[i] == '0') {
                            g[cur].emplace_back(r - (L.size() - 1 - i), 1);
                            if (cur != s) {
                                g[cur].emplace_back(n, 0);
                                cur = n;
                                n += 1;
                            }
                        } else {
                            g[cur].emplace_back(n, 1);
                            cur = n;
                            n += 1;
                        }
                    } else {
                        g[cur].emplace_back(r, 1);
                        if (L.back() == '0') g[cur].emplace_back(r, 0);
                    }
                }
            }
        }
    }

    if (!DEBUG) {
        cout << n << "\n";
        for (int i = 0; i < n; ++i) {
            cout << g[i].size();
            for (auto [v, w] : g[i]) {
                cout << ' ' << v + 1 << ' ' << w;
            }
            cout << '\n';
        }
    } else {
        vector<int> v;
        auto dfs = [&](auto dfs, int s, int cur) -> void {
            for (auto [to, w] : g[s]) {
                dfs(dfs, to, cur * 2 + w);
            }
            if (g[s].size() == 0) {
                v.push_back(cur);
            }
        };
        dfs(dfs, 0, 0);
        sort(v.begin(), v.end());
        for (int i = 0; i < v.size(); ++i) {
            cout << v[i] << ' ';
        }
        cout << endl;
        for (int i = 0; i < v.size(); ++i) {
            assert(v[i] == A + i);
        }
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q = 0;
    if (q) {
        for (int i = 0; i < q; ++i) {
            int l = myRand(15) + 1, r = myRand(15) + 1;
            if (l > r) swap(l, r);
            cout << l << ' ' << r << endl;
            slv(l, r, true);
            cout << endl;
        }
    } else {
        int l, r;
        cin >> l >> r;
        slv(l, r);
    }
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5 7

output:

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

result:

ok ok

Test #2:

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

input:

10 27

output:

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

result:

ok ok

Test #3:

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

input:

5 13

output:

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

result:

ok ok

Test #4:

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

input:

1 1000000

output:

39
20 21 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 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 18 1
2 19 0 19 1
2 20 0 20 1
...

result:

ok ok

Test #5:

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

input:

1 1

output:

2
1 2 1
0

result:

ok ok

Test #6:

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

input:

7 9

output:

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

result:

ok ok

Test #7:

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

input:

3 7

output:

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

result:

ok ok

Test #8:

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

input:

1 5

output:

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

result:

ok ok

Test #9:

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

input:

1 4

output:

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

result:

ok ok

Test #10:

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

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

input:

7 51

output:

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

result:

ok ok

Test #12:

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

input:

51 79

output:

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

result:

ok ok

Test #13:

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

input:

92 99

output:

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

result:

ok ok

Test #14:

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

input:

27 36

output:

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

result:

ok ok

Test #15:

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

input:

55 84

output:

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

result:

ok ok

Test #16:

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

input:

297208 929600

output:

57
2 21 1 40 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 18 1
2 19 0 19 1
2 20 0 20 1
0
2 2 0 22 1
2 3 0 23 1
1 24 0
1 25 0
1 26 0
2 7 0 27 1
1 28 0
2 9 0 29 1
2 10 0 30...

result:

ok ok

Test #17:

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

input:

45728 589156

output:

54
5 21 1 2 1 3 1 4 1 40 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 18 1
2 19 0 19 1
2 20 0 20 1
0
1 22 0
1 23 0
1 24 0
2 5 0 25 1
2 6 0 26 1
2 7 0 27 1
2 8 0 28 1
2 9 ...

result:

ok ok

Test #18:

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

input:

129152 138000

output:

47
2 15 1 32 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
0
1 16 0
1 17 0
1 18 0
1 19 0
2 2 0 20 1
2 3 0 21 1
1 22 0
2 5 0 23 1
2 6 0 24 1
1 25 0
1 26 0
1 27 0
2 10 0 28 1
1 29 0
1 30 0
1 31 0
1 14 0
1 33 1
1 34 1...

result:

ok ok

Test #19:

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

input:

245280 654141

output:

56
3 21 1 2 1 40 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 18 1
2 19 0 19 1
2 20 0 20 1
0
1 22 0
1 23 0
2 4 0 24 1
2 5 0 25 1
2 6 0 26 1
2 7 0 27 1
2 8 0 28 1
2 9 0 29...

result:

ok ok

Test #20:

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

input:

202985 296000

output:

52
2 18 1 36 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
0
1 19 0
1 20 0
2 2 0 21 1
1 22 0
1 23 0
1 24 0
1 25 0
2 7 0 26 1
1 27 0
1 28 0
1 29 0
2 11 0 30 1
1 31 0
1 32 0
1 33 0...

result:

ok ok

Test #21:

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

input:

438671 951305

output:

57
2 21 1 40 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 18 1
2 19 0 19 1
2 20 0 20 1
0
2 2 0 22 1
2 3 0 23 1
1 24 0
2 5 0 25 1
1 26 0
1 27 0
1 28 0
1 29 0
2 10 0 30 1
1...

result:

ok ok

Test #22:

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

input:

425249 739633

output:

56
2 20 1 39 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 18 1
2 19 0 19 1
0
1 21 0
2 2 0 22 1
2 3 0 23 1
1 24 0
2 5 0 25 1
1 26 0
1 27 0
2 8 0 28 1
1 29 0
1 30 0
2 11 0 ...

result:

ok ok

Test #23:

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

input:

551207 961718

output:

56
1 2 1
2 21 1 39 0
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 18 1
2 19 0 19 1
2 20 0 20 1
0
2 3 0 22 1
1 23 0
2 5 0 24 1
1 25 0
2 7 0 26 1
1 27 0
2 9 0 28 1
2 10 0 29 1
1 30 0
1...

result:

ok ok

Test #24:

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

input:

114691 598186

output:

55
4 21 1 2 1 3 1 40 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 18 1
2 19 0 19 1
2 20 0 20 1
0
1 22 0
1 23 0
2 4 0 24 1
1 25 0
1 26 0
2 7 0 27 1
1 28 0
1 29 0
1 30 0
1 ...

result:

ok ok

Test #25:

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

input:

234654 253129

output:

46
1 2 1
1 3 1
1 4 1
2 19 1 33 0
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 18 1
0
1 20 0
2 6 0 21 1
2 7 0 22 1
2 8 0 23 1
1 24 0
1 25 0
2 11 0 26 1
2 12 0 27 1
1 28 0
1 29 0
2 15 0 30 1
1 31 0
1 32 0...

result:

ok ok

Test #26:

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

input:

554090 608599

output:

52
1 2 1
1 3 0
1 4 0
2 21 1 37 0
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 18 1
2 19 0 19 1
2 20 0 20 1
0
1 22 0
2 6 0 23 1
1 24 0
1 25 0
2 9 0 26 1
1 27 0
1 28 0
2 12 0 29 1
1 30 0
2 14 0 31 1
1 32 ...

result:

ok ok

Extra Test:

score: 0
Extra Test Passed