Chef is long been involved in cooking recipes. He has now got bored with it, and has to decided to play with food instead of cooking it up.
He has already selected N spaghetti strands for his dish. He has to select just one extra strand such that this strand's length makes a non-degenerate triangle with some of the two other strands already present in the dish. A triangle formed by three collinear points is called a degenerate triangle. The length of the strand to be selected must be an integer in the range [L, R] (both inclusive).
Formally, let array L denote the lengths of N strands, then the extra strand of length X can be selected if there exists two different indices i, j (i ≠ j) such that there can be made a triangle of side lengths Li, Lj and X.
Can you help him find the number of possible valid lengths of the extra strand?
Note again that three collinear points are not considered to form a triangle.
Input
First line of the input contains three space separated N, L and R, denoting the number of strands already present in the dish and the range of the length of the new strand to be selected respectively.
The next line contains N space separated integers, where the i-th integer Li denotes the length of the i-th strand.
Output
Output a single line corresponding to the answer of the problem.
Constraints
- 2 ≤ N ≤ 106
- 1 ≤ L ≤ R ≤ 1018
- 1 ≤ Li ≤ 1018
Subtasks
Subtask #1 (10 points):
- 2 ≤ N ≤ 100
- 1 ≤ L ≤ R ≤ 106
Subtask #2 (30 points):
- 2 ≤ N ≤ 1000
- 1 ≤ L ≤ R ≤ 1018
Subtask #3 (60 points):
- 2 ≤ N ≤ 106
- 1 ≤ L ≤ R ≤ 1018
Example
Input: 5 1 4 1 2 3 4 5 Output: 3
Explanation
If Chef chooses the strand of length 1, he can't form a triangle using this strand and the any of the pair of strands of length 1, 2, 3, 4, 5. However, for each of the extra strand of length 2, 3, 4, one can find at least one pair of strands such that those pair of strands and the extra strand makes a triangle. So, the answer will be 3
如果两条边是确定的,那么第三条边是在一个范围之内的。考虑当较短的边确定时,第二条边越长,第三条边的最小值越大;同理,当较长边确定时,第二条边月端,第三条边的最大值越小。因此只需将原序列排序,统计相邻两边确定时的最大值和最小值,然后再将所有区间合并,求与[l,r]的交集即可。当然,后续处理较难,细节较多。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<algorithm> #include<cstring> #include<string> #include<vector> #include<map> #include<set> #include<queue> using namespace std; int n; struct ss { long long mi,ma; }; long long l,r; long long a[1000001]; ss p[1000001]; inline bool cmp(ss a,ss b) { return a.mi<b.mi||a.mi==b.mi&&a.ma>b.ma; } int main() { scanf("%d%lld%lld",&n,&l,&r); int i; for (i=1;i<=n;i++) scanf("%lld",&a[i]); sort(a+1,a+n+1); for (i=1;i<n;i++) { p[i].mi=a[i+1]-a[i]+1; p[i].ma=a[i]+a[i+1]-1; } sort(p+1,p+n,cmp); //for (i=1;i<n;i++) printf("%d %d ",p[i].mi,p[i].ma); i=1; int po=1; long long ans=0; while (i<=n-1) { long long ma=p[po].ma; while (p[i].mi<=ma&&i<=n-1) { if (p[i].ma>ma) ma=p[i].ma; i++; //if (p[i].ma>ma) ma=p[i].ma; } if (p[i].mi>ma) i--; long long ma1=ma; long long ma2=p[po].mi; //cout<<ma1<<" "<<ma2<<" "<<i<<endl; if (min(r,ma1)>=max(l,ma2)) ans+=min(r,ma1)-max(l,ma2)+1; po=i+1; i++; } printf("%lld ",ans); return 0; }