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

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

int log_base_2(int n){
    if(n<=0)return -1;
    int ans = 0;
    while(n != 1){
        n /= 2;
        ans++;
    }
    return ans;
}

int pow_2(int n){
    if(n<=0)return -1;
    int ans = 1;
    while(n){
        ans *= 2;
        n--;
    }
    return ans;
}

int main() {
	// your code goes here
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--){
	    int l, r;
	    cin>>l>>r;
	    if(r == l+2){
	        cout<<l<<" "<<l+1<<" "<<r<<endl;
	        continue;
	    }
	    if(l == 0 && r == 3){
	        cout<<0<<" "<<1<<" "<<2<<endl;
	        continue;
	    }
	    int i, j, i_prime, j_prime;
	    i = log_base_2(l);
	    i_prime = i;
	    j = log_base_2(r);
	    j_prime = j;
	   // cout<<i<<" "<<j<<endl;
	   // int p = pow_2(i);
	    int l_prime = l;
	    int r_prime = r;
	   // cout<<pow_2(i_prime)<<" "<<j_prime<<endl;
	   // cout<<l_prime<<" "<<r_prime<<endl;
	   // int m = log_base_2(r_prime);
	   // cout<<i<<" "<<j<<" "<<m<<endl;
	   while(i_prime == j_prime && l_prime > 0 && r_prime > 0){
	       l_prime -= pow_2(i_prime);
	       r_prime -= pow_2(i_prime);
	       i_prime = log_base_2(l_prime);
	       j_prime = log_base_2(r_prime);
	   }
	   //cout<<i_prime<<" "<<j_prime<<endl;
	    if(j > i+1){
	        cout<<pow_2(j-1)<<" "<<pow_2(j)-1<<" "<<pow_2(j)<<endl;
	        continue;
	    }
	    if(j == i+1){
	        if(i == 1 && l == 3){
	            cout<<l<<" "<<pow_2(j)<<" "<<r<<endl;
	            continue;
	        }
	        cout<<l<<" "<<pow_2(j)-1<<" "<<pow_2(j)<<endl;
	        continue;
	    }
	    if(j == i){
	       // l_prime to r_prime
	       if(j_prime > i_prime + 1){
	           cout<<l-l_prime+pow_2(j_prime-1)<<" "<<l-l_prime+pow_2(j_prime)-1<<" "<<l-l_prime+pow_2(j_prime)<<endl;
	           continue;
	       }
	       else if(j_prime == i_prime + 1){
	           cout<<l<<" "<<l-l_prime+pow_2(j_prime)-1<<" "<<l-l_prime+pow_2(j_prime)<<endl;
	           continue;
	       }
	    }
	}
	return 0;
}


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

void init_code(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

string bin(int n){
    string str;
    while(n){
        int dig = n%2;
        dig += '0';
        str += dig;
        n /= 2;
    }
    int m = str.size();
    string s;
    for(int i = m-1; i > -1; i--){
        s += str[i];
    }

    return s;
}

int log_2(int n){
    if(n<=0)return 0;
    int ans = 0;
    while(n != 1){
        ans++;
        n /= 2;
    }
    return ans;
}

int pow_2(int n){
    int ans = 1;
    for(int i = 0; i < n; i++){
        ans *= 2;
    }
    return ans;
}

int main(){
    init_code();
    int t;
    cin>>t;
    while(t--){
        int l,r;
        cin>>l>>r;
        if(r == l+2){
            cout<<l<<" "<<l+1<<" "<<r<<endl;
            continue;
        }
        if(l == 0){
            int j = log_2(r);
            if(j == 1){
                cout<<0<<" "<<1<<" "<<2<<endl;
                continue;
            }
            cout<<pow_2(j-1)<<" "<<pow_2(j)-1<<" "<<pow_2(j)<<endl;
            continue;
        }
        if(l == 1){
            int j = log_2(r);
            if(j == 1){
                cout<<1<<" "<<2<<" "<<3<<endl;
                continue;
            }
            cout<<pow_2(j-1)<<" "<<pow_2(j)-1<<" "<<pow_2(j)<<endl;
            continue;
        }
        int i = log_2(l);
        int j = log_2(r);
        // cout<<i<<" "<<j<<endl;
        if(j > i+1){
            cout<<pow_2(j)-2<<" "<<pow_2(j)-1<<" "<<pow_2(j)<<endl;
            continue;
        }
        if(j == i+1){
            if(l == pow_2(j)-1){
                cout<<l<<" "<<pow_2(j)<<" "<<r<<endl;
                continue;
            }
            cout<<l<<" "<<pow_2(j)-1<<" "<<pow_2(j)<<endl;
            continue;
        }
        // cout<<pow_2(i)<<endl;
        if(j == i){
            if(l == pow_2(i)){
                int j_prime = log_2(r-l);
                if(j_prime == 1){
                    cout<<l<<" "<<l+1<<" "<<l+2<<endl;
                    continue;
                }
                cout<<l+pow_2(j_prime-1)<<" "<<l+pow_2(j_prime)-1<<" "<<l+pow_2(j_prime)<<endl;
                continue;
            }
            // int j_prime = log_2(r-pow_2(i));
            string s, k;
            s = bin(l);
            k = bin(r);
            // cout<<s<<endl<<k<<endl;
            int m = s.size();
            // cout<<m<<endl;
            int idx = -1;
            int left = 0;
            for(int p = 0; p < m; p++){
                if(s[p] == '1'){
                    left += pow_2(m-p-1);
                }
                if(s[p] != k[p]){
                    idx = p;
                    break;
                }
            }
            // cout<<left<<" "<<idx<<endl;
            int re = 1, id = -1;
            for(int p = idx+1; p < m; p++){
                if(k[p] != '0'){
                    re = 0;
                    id = p;
                    break;
                }
            }
            int j_prime = m - idx - 1;
            // cout<<j_prime<<" "<<pow_2(j_prime)<<" "<<re<<" "<<id<<endl;
            if(re == 1){
                cout<<left+pow_2(j_prime)-2<<" "<<left+pow_2(j_prime)-1<<" "<<left+pow_2(j_prime)<<endl;
                continue;
            }
            cout<<left+pow_2(j_prime)-1<<" "<<left+pow_2(j_prime)<<" "<<left+pow_2(j_prime)+1<<endl;
            continue;
        }
    }
}