参考代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll temp[1500000], sum[1500000];
ll cal(ll n)
{
return (n+1)*n/2;
}
int main()
{
ios::sync_with_stdio(false);
ll index = 1;
ll endL;
while(1)
{
temp[index] = temp[index-1] + index;
sum[index] = sum[index-1] + temp[index];
if(temp[index] > 1e12)
{
endL = index;
break;
}
index++;
}
ll T;
cin >> T;
ll l, r, res;
for(ll i = 0; i < T; i++)
{
cin >> l >> r;
ll leftL = lower_bound(temp, temp+endL, l) - temp;
ll rightL = lower_bound(temp, temp+endL, r) - temp;
res = sum[rightL-1] - sum[leftL-1];
res -= cal(l-temp[leftL-1]-1);
res += cal(r-temp[rightL-1]);
cout << res << endl;
}
return 0;
}
|