题目链接-C. Count Triangles
题目大意
给你四个数 A , B , C , D A,B,C,D A,B,C,D,求有多少个三边为 x , y , z ( A ≤ x ≤ B ≤ y ≤ C ≤ z ≤ D ) x,y,z (A ≤ x ≤ B ≤ y ≤ C ≤ z ≤ D) x,y,z(A≤x≤B≤y≤C≤z≤D)的三角形
解题思路
组 合 数 学 组合数学 组合数学
for
循环枚举可行的 x + y x+y x+y值,对于每个 i i i,满足条件的 z z z值范围为 [ C , i − 1 ] [C,i-1] [C,i−1],又因为 z m a x = D z_{max}=D zmax=D,所以可取 z z z的个数为min(D,i-1)-C+1
,即min(D+1,i)-C
x+y==i
的 x , y x,y x,y对数min(D+1,i)-C*min(i-B,B)-max(i-C,A)+1
,即可得到答案附上代码
#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=2e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
inline void read(int &x){
char t=getchar();
while(!isdigit(t)) t=getchar();
for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int a,b,c,d,ans=0;
cin>>a>>b>>c>>d;
for(int i=max(c+1,a+b);i<=b+c;i++)
ans+=(min(d+1,i)-c)*(min(i-b,b)-max(i-c,a)+1);
cout<<ans<<endl;
return 0;
}