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

Previous submission (verdict: Time limit exceeded on test 3):
#include <bits/stdc++.h>
#include <iostream>

const long long mod = 1e9 + 7;
const long long cmod = 998244353;
#define F first

#define S second
#define pb push_back
#define YES cout << "YES\n"
#define NO cout << "NO\n"
using namespace std;
#define ll long long
#define Sort(A) sort(A.begin(), A.end())
#define modq(x) x = ((x % mod) + mod) % mod
#define loop(x, n) for (int i = x; i < n; i++)
#define V vector<int>
#define VV vector<V>
#define rep(i, n) for (ll i = 0; i < (n); ++i)

typedef pair<ll, ll> iPair;

vector<pair<int, int>> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<pair<int, int>> dir2 = {{1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
vector<vector<int>> adj;
vector<vector<iPair>> adjw;
vector<bool> vis;
vector<bool> prime;
vector<int> primenumbers;
vector<bool> inrec;

class SegmentTree
{
    vector<int> tree, lazy;
    int n;

public:
    SegmentTree(const vector<int> &arr)
    {
        n = arr.size();
        tree.resize(4 * n);
        lazy.resize(4 * n, 0);
        build(arr, 0, 0, n - 1);
    }

    void build(const vector<int> &arr, int node, int start, int end)
    {
        if (start == end)
        {
            tree[node] = arr[start];
        }
        else
        {
            int mid = start + (end - start) / 2;
            build(arr, 2 * node + 1, start, mid);
            build(arr, 2 * node + 2, mid + 1, end);
            tree[node] = tree[2 * node + 1] ^ tree[2 * node + 2];
        }
    }

    void propagate(int node, int start, int end)
    {
        if (lazy[node] != 0)
        {
            tree[node] ^= lazy[node] * ((end - start + 1) % 2);

            if (start != end)
            {
                lazy[2 * node + 1] ^= lazy[node];
                lazy[2 * node + 2] ^= lazy[node];
            }

            lazy[node] = 0;
        }
    }

    void rangeUpdate(int l, int r, int val, int node, int start, int end)
    {
        propagate(node, start, end);

        if (r < start || end < l)
        {
            return;
        }

        if (l <= start && end <= r)
        {
            lazy[node] ^= val;
            propagate(node, start, end);
            return;
        }

        int mid = start + (end - start) / 2;
        rangeUpdate(l, r, val, 2 * node + 1, start, mid);
        rangeUpdate(l, r, val, 2 * node + 2, mid + 1, end);
        tree[node] = tree[2 * node + 1] ^ tree[2 * node + 2];
    }

    int rangeQuery(int l, int r, int node, int start, int end)
    {
        propagate(node, start, end);

        if (r < start || end < l)
        {
            return 0;
        }

        if (l <= start && end <= r)
        {
            return tree[node];
        }

        int mid = start + (end - start) / 2;
        return rangeQuery(l, r, 2 * node + 1, start, mid) ^ rangeQuery(l, r, 2 * node + 2, mid + 1, end);
    }

    void updateRange(int l, int r, int val)
    {
        rangeUpdate(l, r, val, 0, 0, n - 1);
    }

    int queryRange(int l, int r)
    {
        return rangeQuery(l, r, 0, 0, n - 1);
    }
};

struct DSU
{
    int n;
    vector<int> parent;
    vector<int> rank;
    vector<int> size;

    DSU(int n)
    {
        parent.resize(n + 1);
        size.resize(n + 1, 1);

        for (int i = 1; i <= n; i++)
        {

            parent[i] = i;
        }
    }

    int find_set(int x)
    {
        if (parent[x] != x)
        {
            parent[x] = find_set(parent[x]);
        }

        return parent[x];
    }

    void union_set(int x, int y)
    {
        int px = find_set(x);
        int py = find_set(y);
        if (px != py)
        {
            if (size[px] < size[py])
            {
                swap(px, py);
            }
            parent[py] = px;
            size[px] += size[py];
        }
    }
};

int leftmost(ll n)
{

    int pos = 0;
    while (n > 0)
    {
        n >>= 1;
        pos++;
    }
    return pos - 1;
}

int findMSBPosition(long long n)
{
    if (n == 0)
        return -1;
    return (int)(log2(n));
}

ll binpow(ll a, ll n, ll mod)
{
    ll res = 1;
    while (n != 0)
    {
        if (n & 1)
        {
            res *= a;
            res %= mod;
        }
        a *= a;
        a %= mod;
        n >>= 1;
    }
    return res % mod;
}

ll inv(ll x, ll mod)
{
    if (x % mod == 0)
        return 0;
    return binpow(x, mod - 2, mod);
}

int query(int a, int b)
{
    int cnt;
    cout << "?" << " " << a << " " << b << endl;
    cin >> cnt;
    return cnt;
}

bool compare(iPair &a, iPair &b)
{
    if (a.first != b.first)
    {
        return a.first < b.first;
    }

    return a.second < b.second;
}

void initprime(int n)
{
    prime.resize(n + 1, true);

    prime[1] = prime[0] = false;

    for (int i = 2; i * i <= n; i++)
    {

        if (prime[i])
        {
            for (int j = i * i; j <= n; j += i)
            {
                prime[j] = false;
            }
        }
    }

    // for (int i = 2; i <= n; i++)
    // {
    //     if (prime[i])
    //     {
    //         primenumbers.push_back(i);
    //     }
    // }
}

bool isPerfectSquare(long long int x)
{
    if (x >= 0)
    {
        int sr = sqrt(x);
        return (sr * sr == x);
    }
    return false;
}
int countPS(string str)
{
    int N = str.length();

    int cps[N + 1][N + 1];
    memset(cps, 0, sizeof(cps));

    for (int i = 0; i < N; i++)
        cps[i][i] = 1;

    for (int L = 2; L <= N; L++)
    {
        for (int i = 0; i <= N - L; i++)
        {
            int k = L + i - 1;
            if (str[i] == str[k])
                cps[i][k] = cps[i][k - 1] + cps[i + 1][k] + 1;
            else
                cps[i][k] = cps[i][k - 1] + cps[i + 1][k] - cps[i + 1][k - 1];
        }
    }

    return cps[0][N - 1];
}

void dfs(int node)
{
    vis[node] = true;
    for (int neighbor : adj[node])
    {
        if (!vis[neighbor])
        {
            dfs(neighbor);
        }
    }
}

void calculate(int n, string &s, vector<int> &lps)
{

    for (int i = 1; i < n; i++)
    {

        int j = lps[i - 1];

        while (j > 0 && s[i] != s[j])
        {
            j = lps[j - 1];
        }

        if (s[i] == s[j])
        {
            j++;
        }

        lps[i] = j;
    }
}

vector<int> dijkstra(int n, int src)
{
    vector<int> dist(n + 1, INT_MAX);

    priority_queue<iPair, vector<iPair>, greater<iPair>> pq;

    dist[src] = 0;
    pq.push({0, src});

    while (!pq.empty())
    {
        int u = pq.top().second;
        ll currDist = pq.top().first;
        pq.pop();

        for (const auto &neighbor : adjw[u])
        {
            int v = neighbor.first;
            ll weight = neighbor.second;

            ll newDist = dist[u] ^ weight;

            if (newDist < dist[v])
            {
                dist[v] = newDist;
                pq.push({dist[v], v});
            }
        }
    }

    return dist;
}
int log_a_to_base_b(int a, int b)
{
    return log2(a) / log2(b);
}

ll prim(int n, ll modk)
{
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
    set<int> s;
    ll total = 0;

    pq.push({0, 1});

    while (!pq.empty())
    {
        auto [weight, u] = pq.top();
        pq.pop();

        if (s.count(u))
        {
            continue;
        }

        total = (weight + total);
        total %= modk;
        s.insert(u);

        for (auto [v, w] : adjw[u])
        {
            if (!s.count(v))
            {
                pq.push({(ll)w, v});
            }
        }
    }

    return (s.size() == n) ? total : -1;
}

vector<int> divc(int n)
{

    n = sqrt(n);
    vector<int> divisor;
    for (int i = 1; i <= n; i++)
    {
        if (n % i == 0)
        {
            divisor.push_back(i);
        }
    }

    return divisor;
};

int findMEX(set<int> &s)
{
    int mex = 0;
    while (s.count(mex))
        mex++;
    return mex;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    // tips
    //  always check with 4 if een odd parity is involved somewhere
    // think as graph
    // think as line
    // see if monotonic
    // see if dp
    int t;
    cin >> t;
    while (t--)
    {

        ll l, r;
        cin >> l >> r;

        ll b;

        ll maxi = 0;
        auto calc = [&](int mid)
        {
            return (ll)((l ^ mid) + (mid ^ r) + (l ^ r));
        };
        for (ll i = l + 1; i < r; i++)
        {
            if (calc(i) > maxi)
            {
                maxi = calc(i);
                b = i;
            }
            i++;
        }

        cout << l << " " << b << " " << r  << endl;
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <iostream>

const long long mod = 1e9 + 7;
const long long cmod = 998244353;
#define F first

#define S second
#define pb push_back
#define YES cout << "YES\n"
#define NO cout << "NO\n"
using namespace std;
#define ll long long
#define Sort(A) sort(A.begin(), A.end())
#define modq(x) x = ((x % mod) + mod) % mod
#define loop(x, n) for (int i = x; i < n; i++)
#define V vector<int>
#define VV vector<V>
#define rep(i, n) for (ll i = 0; i < (n); ++i)

typedef pair<ll, ll> iPair;

vector<pair<int, int>> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<pair<int, int>> dir2 = {{1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
vector<vector<int>> adj;
vector<vector<iPair>> adjw;
vector<bool> vis;
vector<bool> prime;
vector<int> primenumbers;
vector<bool> inrec;

class SegmentTree
{
    vector<int> tree, lazy;
    int n;

public:
    SegmentTree(const vector<int> &arr)
    {
        n = arr.size();
        tree.resize(4 * n);
        lazy.resize(4 * n, 0);
        build(arr, 0, 0, n - 1);
    }

    void build(const vector<int> &arr, int node, int start, int end)
    {
        if (start == end)
        {
            tree[node] = arr[start];
        }
        else
        {
            int mid = start + (end - start) / 2;
            build(arr, 2 * node + 1, start, mid);
            build(arr, 2 * node + 2, mid + 1, end);
            tree[node] = tree[2 * node + 1] ^ tree[2 * node + 2];
        }
    }

    void propagate(int node, int start, int end)
    {
        if (lazy[node] != 0)
        {
            tree[node] ^= lazy[node] * ((end - start + 1) % 2);

            if (start != end)
            {
                lazy[2 * node + 1] ^= lazy[node];
                lazy[2 * node + 2] ^= lazy[node];
            }

            lazy[node] = 0;
        }
    }

    void rangeUpdate(int l, int r, int val, int node, int start, int end)
    {
        propagate(node, start, end);

        if (r < start || end < l)
        {
            return;
        }

        if (l <= start && end <= r)
        {
            lazy[node] ^= val;
            propagate(node, start, end);
            return;
        }

        int mid = start + (end - start) / 2;
        rangeUpdate(l, r, val, 2 * node + 1, start, mid);
        rangeUpdate(l, r, val, 2 * node + 2, mid + 1, end);
        tree[node] = tree[2 * node + 1] ^ tree[2 * node + 2];
    }

    int rangeQuery(int l, int r, int node, int start, int end)
    {
        propagate(node, start, end);

        if (r < start || end < l)
        {
            return 0;
        }

        if (l <= start && end <= r)
        {
            return tree[node];
        }

        int mid = start + (end - start) / 2;
        return rangeQuery(l, r, 2 * node + 1, start, mid) ^ rangeQuery(l, r, 2 * node + 2, mid + 1, end);
    }

    void updateRange(int l, int r, int val)
    {
        rangeUpdate(l, r, val, 0, 0, n - 1);
    }

    int queryRange(int l, int r)
    {
        return rangeQuery(l, r, 0, 0, n - 1);
    }
};

struct DSU
{
    int n;
    vector<int> parent;
    vector<int> rank;
    vector<int> size;

    DSU(int n)
    {
        parent.resize(n + 1);
        size.resize(n + 1, 1);

        for (int i = 1; i <= n; i++)
        {

            parent[i] = i;
        }
    }

    int find_set(int x)
    {
        if (parent[x] != x)
        {
            parent[x] = find_set(parent[x]);
        }

        return parent[x];
    }

    void union_set(int x, int y)
    {
        int px = find_set(x);
        int py = find_set(y);
        if (px != py)
        {
            if (size[px] < size[py])
            {
                swap(px, py);
            }
            parent[py] = px;
            size[px] += size[py];
        }
    }
};

int leftmost(ll n)
{

    int pos = 0;
    while (n > 0)
    {
        n >>= 1;
        pos++;
    }
    return pos - 1;
}

int findMSBPosition(long long n)
{
    if (n == 0)
        return -1;
    return (int)(log2(n));
}

ll binpow(ll a, ll n, ll mod)
{
    ll res = 1;
    while (n != 0)
    {
        if (n & 1)
        {
            res *= a;
            res %= mod;
        }
        a *= a;
        a %= mod;
        n >>= 1;
    }
    return res % mod;
}

ll inv(ll x, ll mod)
{
    if (x % mod == 0)
        return 0;
    return binpow(x, mod - 2, mod);
}

int query(int a, int b)
{
    int cnt;
    cout << "?" << " " << a << " " << b << endl;
    cin >> cnt;
    return cnt;
}

bool compare(iPair &a, iPair &b)
{
    if (a.first != b.first)
    {
        return a.first < b.first;
    }

    return a.second < b.second;
}

void initprime(int n)
{
    prime.resize(n + 1, true);

    prime[1] = prime[0] = false;

    for (int i = 2; i * i <= n; i++)
    {

        if (prime[i])
        {
            for (int j = i * i; j <= n; j += i)
            {
                prime[j] = false;
            }
        }
    }

    // for (int i = 2; i <= n; i++)
    // {
    //     if (prime[i])
    //     {
    //         primenumbers.push_back(i);
    //     }
    // }
}

bool isPerfectSquare(long long int x)
{
    if (x >= 0)
    {
        int sr = sqrt(x);
        return (sr * sr == x);
    }
    return false;
}
int countPS(string str)
{
    int N = str.length();

    int cps[N + 1][N + 1];
    memset(cps, 0, sizeof(cps));

    for (int i = 0; i < N; i++)
        cps[i][i] = 1;

    for (int L = 2; L <= N; L++)
    {
        for (int i = 0; i <= N - L; i++)
        {
            int k = L + i - 1;
            if (str[i] == str[k])
                cps[i][k] = cps[i][k - 1] + cps[i + 1][k] + 1;
            else
                cps[i][k] = cps[i][k - 1] + cps[i + 1][k] - cps[i + 1][k - 1];
        }
    }

    return cps[0][N - 1];
}

void dfs(int node)
{
    vis[node] = true;
    for (int neighbor : adj[node])
    {
        if (!vis[neighbor])
        {
            dfs(neighbor);
        }
    }
}

void calculate(int n, string &s, vector<int> &lps)
{

    for (int i = 1; i < n; i++)
    {

        int j = lps[i - 1];

        while (j > 0 && s[i] != s[j])
        {
            j = lps[j - 1];
        }

        if (s[i] == s[j])
        {
            j++;
        }

        lps[i] = j;
    }
}

vector<int> dijkstra(int n, int src)
{
    vector<int> dist(n + 1, INT_MAX);

    priority_queue<iPair, vector<iPair>, greater<iPair>> pq;

    dist[src] = 0;
    pq.push({0, src});

    while (!pq.empty())
    {
        int u = pq.top().second;
        ll currDist = pq.top().first;
        pq.pop();

        for (const auto &neighbor : adjw[u])
        {
            int v = neighbor.first;
            ll weight = neighbor.second;

            ll newDist = dist[u] ^ weight;

            if (newDist < dist[v])
            {
                dist[v] = newDist;
                pq.push({dist[v], v});
            }
        }
    }

    return dist;
}
int log_a_to_base_b(int a, int b)
{
    return log2(a) / log2(b);
}

ll prim(int n, ll modk)
{
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
    set<int> s;
    ll total = 0;

    pq.push({0, 1});

    while (!pq.empty())
    {
        auto [weight, u] = pq.top();
        pq.pop();

        if (s.count(u))
        {
            continue;
        }

        total = (weight + total);
        total %= modk;
        s.insert(u);

        for (auto [v, w] : adjw[u])
        {
            if (!s.count(v))
            {
                pq.push({(ll)w, v});
            }
        }
    }

    return (s.size() == n) ? total : -1;
}

vector<int> divc(int n)
{

    n = sqrt(n);
    vector<int> divisor;
    for (int i = 1; i <= n; i++)
    {
        if (n % i == 0)
        {
            divisor.push_back(i);
        }
    }

    return divisor;
};

int findMEX(set<int> &s)
{
    int mex = 0;
    while (s.count(mex))
        mex++;
    return mex;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    // tips
    //  always check with 4 if een odd parity is involved somewhere
    // think as graph
    // think as line
    // see if monotonic
    // see if dp
    int t;
    cin >> t;
    while (t--)
    {

        ll l, r;
        cin >> l >> r;

        auto calc = [&](ll mid)
        {
            return (l ^ mid) + (mid ^ r) + (l ^ r);
        };

        ll left = l + 1, right = r - 1;
        while (right - left > 2)
        {
            ll mid1 = left + (right - left) / 3;
            ll mid2 = right - (right - left) / 3;

            if (calc(mid1) < calc(mid2))
                left = mid1;
            else
                right = mid2;
        }

        ll best_mid = left, max_val = calc(left);
        for (ll i = left + 1; i <= right; i++)
        {
            if (calc(i) > max_val)
            {
                max_val = calc(i);
                best_mid = i;
            }
        }

        cout << l << " " << best_mid << " " << r << endl;
    }
    return 0;
}