Codeforces Round #776 (Div. 3) Editorial-B题遇到的问题

问题遇到的现象和发生背景

img

问题相关代码,请勿粘贴截图

#include <bits/stdc++.h>

using namespace std;
int main()
{
long long int t, l, r, a, m, t1, m1, t2;
cin >> t;

for (int i = 0; i < t; i++)
{
    cin >> l >> r >> a;

     t2 = r / a + r % a;
    t1= r- r%a -1;
    m=t1/a + t1%a;
    if(l<=t1&& t1<=r)
    cout<<max(t2,m);
    else
    cout<<t2;
   
   

    cout <<  endl;
}

}

运行结果及报错内容

想问一下,为什么这样处理数据
t2 = r / a + r % a;
t1= r- r%a -1;
m=t1/a + t1%a;
if(l<=t1&& t1<=r)
cout<<max(t2,m);
else
cout<<t2;

我的解答思路和尝试过的方法
我想要达到的结果
#include <iostream>
using namespace std;

int fa_x(int x, int a)
{
    return x / a + x % a;
}

int main() 
{
    int l, r, a;
    cout << "请输入l:";
    cin >> l;
    cout << "请输入r:";
    cin >> r;
    cout << "请输入a:";
    cin >> a;
    int max = -1;
    for (int i = l; i <= r; i++) {
        int temp = fa_x(i, a);
        if (temp > max) max = temp;
    }
    cout << max;
    return 0;
}

img