題目連接:https://codeforces.com/contest/1355/problem/Cios
第一種作法:優化
咱們假設三角形的三條邊 x,y,z x + y > z spa
咱們考慮枚舉 x + y 的和 【枚舉的時候能夠優化一下,枚舉 [max(c+1,a+b) , b+c] 】,若是知道了 x 咱們就能夠知道 ycode
因此咱們考慮 x ,單純的考慮 x 的範圍 [a,b] 或者針對 y 從而考慮 x 的範圍 [i-c,i-b]blog
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #define ll long long #define ull unsigned long long #define ls nod<<1 #define rs (nod<<1)+1 #define pii pair<int,int> #define mp make_pair #define pb push_back #define INF 0x3f3f3f3f #define max(a, b) (a>b?a:b) #define min(a, b) (a<b?a:b) const double eps = 1e-8; const int maxn = 2e5 + 10; const ll MOD = 998244353; const int mlog=20; int sgn(double a) { return a < -eps ? -1 : a < eps ? 0 : 1; } using namespace std; int main() { ll a,b,c,d; ll ans = 0; cin >> a >> b >> c >> d; for (ll i = max(c+1,a+b);i <= b+c;i++) { ans += (min(d+1,i)-c) * (min(i-b,b)-max(a,i-c)+1); } cout << ans << endl; return 0; }
第二種方法:ci
咱們直接考慮差分 + 前綴和的方法,get
針對 x 咱們能夠直接枚舉出 x + y 的範圍string
而後再反過來前綴和能夠獲得 sum[i] 表明 >= i 的個數it
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #define ll long long #define ull unsigned long long #define ls nod<<1 #define rs (nod<<1)+1 #define pii pair<int,int> #define mp make_pair #define pb push_back #define INF 0x3f3f3f3f #define max(a, b) (a>b?a:b) #define min(a, b) (a<b?a:b) const double eps = 1e-8; const int maxn = 1e6 + 10; const ll MOD = 998244353; const int mlog=20; int sgn(double a) { return a < -eps ? -1 : a < eps ? 0 : 1; } using namespace std; ll sum[maxn]; int main() { ll a,b,c,d; ll ans = 0; cin >> a >> b >> c >> d; for (int i = a;i <= b;i++) { sum[i+b]++; sum[i+c+1]--; } for (int i = 1;i < maxn;i++) sum[i] += sum[i-1]; for (int i = maxn-1;i >= 1;i--) sum[i] += sum[i+1]; for (int i = c;i <= d;i++) ans += sum[i+1]; cout << ans << endl; return 0; }