Problem: 2057C - Trip to the Olympiad
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 1):
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#include <bits/stdc++.h>
using namespace std;

using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using i8 = int8_t;
using u8 = uint8_t;
using ish = int16_t;
using ush = uint16_t;

const ll MOD = 1e9 + 7;
const ll OMOD = 998'244'353;

inline namespace IO {
#define SFINAE(x, ...)                                                                                       \
    template <class, class = void> struct x : std::false_type {};                                            \
    template <class T> struct x<T, std::void_t<__VA_ARGS__>> : std::true_type {}

SFINAE(DefaultI, decltype(std::cin >> std::declval<T &>()));
SFINAE(DefaultO, decltype(std::cout << std::declval<T &>()));
SFINAE(IsTuple, typename std::tuple_size<T>::type);
SFINAE(Iterable, decltype(std::begin(std::declval<T>())));

template <auto &is> struct Reader {
    template <class T> void Impl(T &t) {
        if constexpr (DefaultI<T>::value)
            is >> t;
        else if constexpr (Iterable<T>::value) {
            for (auto &x : t)
                Impl(x);
        } else if constexpr (IsTuple<T>::value) {
            std::apply([this](auto &...args) { (Impl(args), ...); }, t);
        } else
            static_assert(IsTuple<T>::value, "No matching type for read");
    }
    template <class... Ts> void read(Ts &...ts) { ((Impl(ts)), ...); }
};

template <class... Ts> void re(Ts &...ts) { Reader<cin>{}.read(ts...); }
#define def(t, args...)                                                                                      \
    t args;                                                                                                  \
    re(args);

template <auto &os, bool debug, bool print_nd> struct Writer {
    string comma() const { return debug ? "," : ""; }
    template <class T> constexpr char Space(const T &) const {
        return print_nd && (Iterable<T>::value or IsTuple<T>::value) ? '\n' : ' ';
    }
    template <class T> void Impl(T const &t) const {
        if constexpr (DefaultO<T>::value)
            os << t;
        else if constexpr (Iterable<T>::value) {
            if (debug)
                os << '{';
            int i = 0;
            for (auto &&x : t)
                ((i++) ? (os << comma() << Space(x), Impl(x)) : Impl(x));
            if (debug)
                os << '}';
        } else if constexpr (IsTuple<T>::value) {
            if (debug)
                os << '(';
            std::apply(
                [this](auto const &...args) {
                    int i = 0;
                    (((i++) ? (os << comma() << " ", Impl(args)) : Impl(args)), ...);
                },
                t);
            if (debug)
                os << ')';
        } else
            static_assert(IsTuple<T>::value, "No matching type for print");
    }
    template <class T> void ImplWrapper(T const &t) const {
        if (debug)
            os << "\033[0;31m";
        Impl(t);
        if (debug)
            os << "\033[0m";
    }
    template <class... Ts> void print(Ts const &...ts) const { ((Impl(ts)), ...); }
    template <class F, class... Ts>
    void print_with_sep(const std::string &sep, F const &f, Ts const &...ts) const {
        ImplWrapper(f), ((os << sep, ImplWrapper(ts)), ...), os << '\n';
    }
    void print_with_sep(const std::string &) const { os << '\n'; }
};

template <class... Ts> void pr(Ts const &...ts) { Writer<cout, false, true>{}.print(ts...); }
template <class... Ts> void ps(Ts const &...ts) { Writer<cout, false, true>{}.print_with_sep(" ", ts...); }
} // namespace IO

inline namespace Debug {
template <typename... Args> void err(Args... args) {
    Writer<cerr, true, false>{}.print_with_sep(" | ", args...);
}
template <typename... Args> void errn(Args... args) {
    Writer<cerr, true, true>{}.print_with_sep(" | ", args...);
}

void err_prefix(string func, int line, string args) {
    cerr << "\033[0;31m\u001b[1mDEBUG\033[0m"
         << " | "
         << "\u001b[34m" << func << "\033[0m"
         << ":"
         << "\u001b[34m" << line << "\033[0m"
         << " - "
         << "[" << args << "] = ";
}

#ifdef LOCAL
#define clg(args...) err_prefix(__FUNCTION__, __LINE__, #args), err(args)
#else
#define clg(...)
#endif

} // namespace Debug

void solve() {
    int L, r;
    cin >> L >> r;
    string h = bitset<32>(r).to_string();
    string l = bitset<32>(L).to_string();
    char ans[33];
    ans[32] = '\0';
    bool f = true;
    bool f2 = true;
    bool f3 = false;
    for (int i = 0; i < 32; ++i) {
        if (f) {
            if (h[i] == l[i]) {
                ans[i] = h[i];
            } else {
                f = false;
                --i;
            }
        } else {
            if (f2) {
                if ((h[i + 1] == l[i + 1] && h[i + 1] == '1') || (h[i + 1] == '1' && l[i + 1] == '0')) {
                    ans[i] = '1';
                    ans[i + 1] = '0';
                    ++i;
                    f3 = true;
                    f2 = false;
                    continue;
                } else if (h[i + 1] == l[i + 1] && h[i + 1] == '0') {
                    ans[i] = '1';
                    ans[i + 1] = '0';
                    ++i;
                    f3 = true;
                    f2 = false;
                    continue;
                } else {
                    bool isallzero = true;
                    for (int j = i + 1; j < 32; ++j) {
                        if (h[j] == '0') {
                        } else {
                            isallzero = false;
                            break;
                        }
                    }
                    if (isallzero) {
                        ans[i] = '0';
                        for (int j = i + 1; j < 32; ++j) {
                            ans[j] = '1';
                        }
                        pr(l, " ");
                        pr(stol(ans, nullptr, 2));
                        pr(" ", r);
                        ps();
                        return;
                    } else {
                        ans[i] = '1';
                        for (int j = i + 1; j < 32; ++j) {
                            ans[j] = '0';
                        }
                        for (int j = i + 1; j < 32; ++j) {
                            l[j] = '1';
                        }
                        ps(stol(h, nullptr, 2), stol(ans, nullptr, 2), stol(l, nullptr, 2));
                        return;
                    }
                }
            }
            if (f3) {
                if (h[i] == l[i] && h[i] == '1') {
                    ans[i] = '0';
                } else {
                    ans[i] = '1';
                }
            }
        }
    }
    ps(L, stol(ans, nullptr, 2), r);
}

int main() {
#if defined(LOCAL)
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tc = 1;
    cin >> tc;
    while (tc--) {
        solve();
    }
    return 0;
}

Submission (verdict: Accepted):
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#include <bits/stdc++.h>
using namespace std;

using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using i8 = int8_t;
using u8 = uint8_t;
using ish = int16_t;
using ush = uint16_t;

const ll MOD = 1e9 + 7;
const ll OMOD = 998'244'353;

inline namespace IO {
#define SFINAE(x, ...)                                                                                       \
    template <class, class = void> struct x : std::false_type {};                                            \
    template <class T> struct x<T, std::void_t<__VA_ARGS__>> : std::true_type {}

SFINAE(DefaultI, decltype(std::cin >> std::declval<T &>()));
SFINAE(DefaultO, decltype(std::cout << std::declval<T &>()));
SFINAE(IsTuple, typename std::tuple_size<T>::type);
SFINAE(Iterable, decltype(std::begin(std::declval<T>())));

template <auto &is> struct Reader {
    template <class T> void Impl(T &t) {
        if constexpr (DefaultI<T>::value)
            is >> t;
        else if constexpr (Iterable<T>::value) {
            for (auto &x : t)
                Impl(x);
        } else if constexpr (IsTuple<T>::value) {
            std::apply([this](auto &...args) { (Impl(args), ...); }, t);
        } else
            static_assert(IsTuple<T>::value, "No matching type for read");
    }
    template <class... Ts> void read(Ts &...ts) { ((Impl(ts)), ...); }
};

template <class... Ts> void re(Ts &...ts) { Reader<cin>{}.read(ts...); }
#define def(t, args...)                                                                                      \
    t args;                                                                                                  \
    re(args);

template <auto &os, bool debug, bool print_nd> struct Writer {
    string comma() const { return debug ? "," : ""; }
    template <class T> constexpr char Space(const T &) const {
        return print_nd && (Iterable<T>::value or IsTuple<T>::value) ? '\n' : ' ';
    }
    template <class T> void Impl(T const &t) const {
        if constexpr (DefaultO<T>::value)
            os << t;
        else if constexpr (Iterable<T>::value) {
            if (debug)
                os << '{';
            int i = 0;
            for (auto &&x : t)
                ((i++) ? (os << comma() << Space(x), Impl(x)) : Impl(x));
            if (debug)
                os << '}';
        } else if constexpr (IsTuple<T>::value) {
            if (debug)
                os << '(';
            std::apply(
                [this](auto const &...args) {
                    int i = 0;
                    (((i++) ? (os << comma() << " ", Impl(args)) : Impl(args)), ...);
                },
                t);
            if (debug)
                os << ')';
        } else
            static_assert(IsTuple<T>::value, "No matching type for print");
    }
    template <class T> void ImplWrapper(T const &t) const {
        if (debug)
            os << "\033[0;31m";
        Impl(t);
        if (debug)
            os << "\033[0m";
    }
    template <class... Ts> void print(Ts const &...ts) const { ((Impl(ts)), ...); }
    template <class F, class... Ts>
    void print_with_sep(const std::string &sep, F const &f, Ts const &...ts) const {
        ImplWrapper(f), ((os << sep, ImplWrapper(ts)), ...), os << '\n';
    }
    void print_with_sep(const std::string &) const { os << '\n'; }
};

template <class... Ts> void pr(Ts const &...ts) { Writer<cout, false, true>{}.print(ts...); }
template <class... Ts> void ps(Ts const &...ts) { Writer<cout, false, true>{}.print_with_sep(" ", ts...); }
} // namespace IO

inline namespace Debug {
template <typename... Args> void err(Args... args) {
    Writer<cerr, true, false>{}.print_with_sep(" | ", args...);
}
template <typename... Args> void errn(Args... args) {
    Writer<cerr, true, true>{}.print_with_sep(" | ", args...);
}

void err_prefix(string func, int line, string args) {
    cerr << "\033[0;31m\u001b[1mDEBUG\033[0m"
         << " | "
         << "\u001b[34m" << func << "\033[0m"
         << ":"
         << "\u001b[34m" << line << "\033[0m"
         << " - "
         << "[" << args << "] = ";
}

#ifdef LOCAL
#define clg(args...) err_prefix(__FUNCTION__, __LINE__, #args), err(args)
#else
#define clg(...)
#endif

} // namespace Debug

void solve() {
    int L, r;
    cin >> L >> r;
    string h = bitset<32>(r).to_string();
    string l = bitset<32>(L).to_string();
    char ans[33];
    ans[32] = '\0';
    bool f = true;
    bool f2 = true;
    bool f3 = false;
    for (int i = 0; i < 32; ++i) {
        if (f) {
            if (h[i] == l[i]) {
                ans[i] = h[i];
            } else {
                f = false;
                --i;
            }
        } else {
            if (f2) {
                if ((h[i + 1] == l[i + 1] && h[i + 1] == '1') || (h[i + 1] == '1' && l[i + 1] == '0')) {
                    ans[i] = '1';
                    ans[i + 1] = '0';
                    ++i;
                    f3 = true;
                    f2 = false;
                    continue;
                } else if (h[i + 1] == l[i + 1] && h[i + 1] == '0') {
                    ans[i] = '0';
                    ans[i + 1] = '1';
                    ++i;
                    f3 = true;
                    f2 = false;
                    continue;
                } else {
                    bool isallzero = true;
                    for (int j = i + 1; j < 32; ++j) {
                        if (h[j] == '0') {
                        } else {
                            isallzero = false;
                            break;
                        }
                    }
                    if (isallzero) {
                        ans[i] = '0';
                        for (int j = i + 1; j < 32; ++j) {
                            ans[j] = '1';
                        }
                        pr(L, " ");
                        pr(stol(ans, nullptr, 2));
                        pr(" ", r);
                        ps();
                        return;
                    } else {
                        ans[i] = '1';
                        for (int j = i + 1; j < 32; ++j) {
                            ans[j] = '0';
                        }
                        for (int j = i + 1; j < 32; ++j) {
                            l[j] = '1';
                        }
                        ps(stol(h, nullptr, 2), stol(ans, nullptr, 2), stol(l, nullptr, 2));
                        return;
                    }
                }
            }
            if (f3) {
                if (h[i] == l[i] && h[i] == '1') {
                    ans[i] = '0';
                } else {
                    ans[i] = '1';
                }
            }
        }
    }
    ps(L, stol(ans, nullptr, 2), r);
}

int main() {
#if defined(LOCAL)
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tc = 1;
    cin >> tc;
    while (tc--) {
        solve();
    }
    return 0;
}