题解:
#include<iostream>
#include<vector>
using namespace std;
#define ll long long
int maxn;
vector<ll>dp,dpsum;
int bitOrd(int n){//二分查找
int l=0,r=maxn;
while(l<=r){
int mid=(l+r)/2;
if(dp[mid]>=n)r=mid-1;
else l=mid+1;
}
return l;
}
ll nsum(ll l,ll r){//l到r的累加和
return (r-l+1)*(l+r)/2;
}
ll getRes(ll l,ll r,ll st,ll end){
if(l==r)return nsum(l,r);
else{
ll mid=dpsum[r-1]-dpsum[l];//中间部分的总和
return nsum(st,l)+mid+nsum(1,end);//左边+中间部分+右边
}
}
int main(){
dp.resize(20000000);
dpsum.resize(20000000);
dp[0]=0;dpsum[0]=0;
for(int i=1;1;i++){
dp[i]=dp[i-1]+i;
dpsum[i]=dpsum[i-1]+dp[i];
if(dpsum[i]>=1e12){
maxn=i;dp.resize(maxn+1);dpsum.resize(maxn+1);
break;
}
}
int t;cin>>t;
while(t--){
ll l,r;cin>>l>>r;
ll loc1=bitOrd(l),loc2=bitOrd(r),st=l-dp[loc1-1],end=r-dp[loc2-1];
cout<<getRes(loc1,loc2,st,end);
}
return 0;
}