玄学代码!代码求调!悬关!开大一点就MLE,开小一点就RE,有时还万紫千红
查看原帖
玄学代码!代码求调!悬关!开大一点就MLE,开小一点就RE,有时还万紫千红
685839
gxcgjy楼主2023/9/30 11:19
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef unsigned long long ll;
ll A[8500500],B[8500500],x1,x2,ty,tty,n;
void find(ll x,ll flag){
	ll l,r,mid;
	l = sqrt(x);
	r = x + 1;
	while(l < r){
		mid = (l + r) / 2;
		if(A[mid] <= x)	l = mid + 1;
		else r = mid;
	}
//	cout<<l<<' '<<r<<endl;
	if(A[l - 1] >= x) l--;
	if(flag == 1){
		x1 = l;
	//	cout<<x1<<' ';
		if(x == 0){
			x2 = 1;
			cout<<x2<<' ';
			return;
		}
		x2 = A[l] % x;
		x2 = l - x2;
	//	cout<<x2<<' ';
		return;
	}
	else{
		ty = l;
	//	cout<<ty<<' ';
		if(x == 1){
			tty = 2;
	//		cout<<tty<<' ';
			return;
		}
		tty = A[l] % x;
//		if(tty == 0){
//			ty = 0;
//			cout<<tty<<' ';
//			return;
//		}
		tty = l - tty;
		tty++;
	//	cout<<tty<<' ';
		return;
	}
}
ll add(){
//	cout<<endl;
	ll ans,k,t;
	ans = B[ty] - B[x1 - 1];
	t = (1 + x2) * x2 / 2;
	k = (tty + ty) * (ty - tty + 1) / 2;
//	cout<<ans<<' '<<t<<' '<<k<<endl;
	if(tty == 0){
		return ans;
	}
	if(x1 == 0){
		ans -= k;
		return ans;
	}
	ans = ans - t;
	ans = ans - k;
	return ans;
}
int main(){
	ll i,j,k,l,r;
	cin>>n;
	for(i = 1;i <= 8500000;i++){
		A[i] = A[i - 1] + i;
		B[i] = B[i - 1] + A[i];
	}
	//for(i = 1;i <= 6;i++) cout<<A[i]<<' ';
	for(i = 1;i <= n;i++){
//		cout<<1;	
		cin>>l>>r;
		find(l - 1,1);
		find(r,2);
		cout<<add()<<endl;
	}
	return 0;
} 
//5 8
//4 8
//2 16 9
//2 9 5
//2 5 3
//6 / 4 = 1...2   10 / 8 = 1...2
//1				1 			1			1		1
//1 2			 	3 			4			2		6	
//1 2 3			6			10			3		21
//1 2 3 4			10 			20			4		55
//1 2 3 4 5		15 			35			5		120
//1 2 3 4 5 6 	21 			56			6		231
2023/9/30 11:19
加载中...