Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

void solve(int tc) {
    int l, r;
    cin >> l >> r;

    int res = 0;
    auto f = [](int x, int y) -> int {
        if (x != y) {
            return 1;
        }
        return 1 - x;
    };
    auto g = [](int x, int y) -> int {
        if (x != y) {
            return 0;
        }
        return 1 - x;
    };

    for (int i = 30; i >= 0; --i) {
        int x = r >> i & 1;
        int y = l >> i & 1;
        if (x == y) {
            res |= x << i;
            continue;
        }
        int s = r >> i - 1 & 1;
        int t = l >> i - 1 & 1;
        if (s == t) {
            if (s == 1) {
                res |= 1 << i;
            }
            else {
                res |= 1 << i - 1;
            }
            for (int j = i - 2; j >= 0; --j) {
                res |= f(r >> j & 1, l >> j & 1) << j;
            }
            // cout << "debug" << '\n';
            break;
        }
        if (s == 1) {
            res |= 1 << i;
            for (int j = i - 2; j >= 0; --j) {
                res |= f(r >> j & 1, l >> j & 1) << j;
            }
        }
        else {
            assert(x == 1);
            assert(y == 0);
            assert(s == 0);
            assert(t == 1);

            bool ok = true;
            for (int j = i - 2; j >= 0; --j) {
                int a = r >> j & 1;
                int b = l >> j & 1;
                if (a == 0 && b == 1) {
                    continue;
                }
                if (b == 0) {
                    ok = true;
                }
                else {
                    ok = false;
                }
                break;
            }
            if (ok) {
                res |= 1 << i - 1;
            }
            else {
                res |= 1 << i;
            }
            for (int j = i - 2; j >= 0; --j) {
                if (ok) {
                    res |= f(r >> j & 1, l >> j & 1) << j;
                }
                else {
                    res |= g(r >> j & 1, l >> j & 1) << j;
                }
            }
        }
        break;
    }
    assert(l < res);
    assert(res < r);

    // cout << l << ' ' << res << ' ' << r << '\n';

    cout << (l ^ res) + (res ^ r) + (r ^ l) << '\n';
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int t = 1;
    cin >> t;
    for (int i = 1; i <= t; ++i) {
        solve(i);
    }
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

void solve(int tc) {
    int l, r;
    cin >> l >> r;

    int res = 0;
    auto f = [](int x, int y) -> int {
        if (x != y) {
            return 1;
        }
        return 1 - x;
    };
    auto g = [](int x, int y) -> int {
        if (x != y) {
            return 0;
        }
        return 1 - x;
    };

    for (int i = 30; i >= 0; --i) {
        int x = r >> i & 1;
        int y = l >> i & 1;
        if (x == y) {
            res |= x << i;
            continue;
        }
        int s = r >> i - 1 & 1;
        int t = l >> i - 1 & 1;
        if (s == t) {
            if (s == 1) {
                res |= 1 << i;
            }
            else {
                res |= 1 << i - 1;
            }
            for (int j = i - 2; j >= 0; --j) {
                res |= f(r >> j & 1, l >> j & 1) << j;
            }
            // cout << "debug" << '\n';
            break;
        }
        if (s == 1) {
            res |= 1 << i;
            for (int j = i - 2; j >= 0; --j) {
                res |= f(r >> j & 1, l >> j & 1) << j;
            }
        }
        else {
            assert(x == 1);
            assert(y == 0);
            assert(s == 0);
            assert(t == 1);

            bool ok = true;
            for (int j = i - 2; j >= 0; --j) {
                int a = r >> j & 1;
                int b = l >> j & 1;
                if (a == 0 && b == 1) {
                    continue;
                }
                if (b == 0) {
                    ok = true;
                }
                else {
                    ok = false;
                }
                break;
            }
            if (ok) {
                res |= 1 << i - 1;
            }
            else {
                res |= 1 << i;
            }
            for (int j = i - 2; j >= 0; --j) {
                if (ok) {
                    res |= f(r >> j & 1, l >> j & 1) << j;
                }
                else {
                    res |= g(r >> j & 1, l >> j & 1) << j;
                }
            }
        }
        break;
    }
    assert(l < res);
    assert(res < r);

    cout << l << ' ' << res << ' ' << r << '\n';

    // cout << (l ^ res) + (res ^ r) + (r ^ l) << '\n';
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int t = 1;
    cin >> t;
    for (int i = 1; i <= t; ++i) {
        solve(i);
    }
}