问题描述
黎明时,Venus为Psyche定下了第二个任务。她要渡过河,收集对岸绵羊身上的金羊毛。 那些绵羊狂野不驯,所以Psyche一直往地上丢树枝来把它们吓走。地上现在有n n n根树枝,第i i i根树枝的长度是ai a_i ai. 如果她丢的下一根树枝可以和某两根树枝形成三角形,绵羊就会被激怒而袭击她。 现在Psyche手中只有长度不小于L L L且不大于R R R的树枝。请你帮忙计算,她下一根可以丢多少种不同长度的树枝而不会把绵羊激怒呢?
输入描述
第一行,一个整数T(1≤T≤10) T (1 \le T \le 10) T(1≤T≤10),代表数据组数。 对于每组数据,第一行有三个整数n,L,R n,L,R n,L,R (2≤n≤105,1≤L≤R≤1018) (2 \le n \le 10 ^ 5, 1 \le L \le R \le 10 ^ {18}) (2≤n≤105,1≤L≤R≤1018)。 第二行,n n n个整数,第i i i个整数为ai a_i ai (1≤ai≤1018) (1 \le a_i \le 10 ^ {18}) (1≤ai≤1018),代表第i i i根树枝的长度。
输出描述
输出T T T行,对于每组数据,输出选取方式总数。
输入样例
2
2 1 3
1 1
4 3 10
1 1 2 4
输出样例
2
5
Hint
对于第一组数据,可以选用长度为2,3 2, 3 2,3的树枝。 对于第二组数据,可以选用长度为6,7,8,9,10 6, 7, 8, 9, 10 6,7,8,9,10的树枝。 一般懒得解释题意就直接放题目!
首先关于三角形的三边判断是任意一边要小于另外两边的和大于另外两边的差,所以就是找出范围就好。将地上的枝条排序,只要求出相邻的两两边的长度限制的范围就好,因为其他任意两两边的范围会被这个所包括
然后将这些范围去重,形成几个互不相交的区间范围,然后用总的范围一个一个减去就好(当然,减得时候要判断这些范围与总的范围的相交
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
const int M = 1e5 + ;
ll a[M]; struct node{
bool dis;
ll l,r;
}b[M],c[M]; bool cmp(node a,node b){return a.l<b.l;} bool judge(ll a,ll b,ll c)
{
if (a>=b&&a<=c) return true;
return false;
} int main()
{
int t,n;
ll l,r;
scanf("%d",&t);
while (t--)
{
scanf("%d%I64d%I64d",&n,&l,&r);
for (int i= ; i<=n ; i++)
scanf("%I64d",&a[i]);
sort(a+,a+n+);
int ans=;
for (int i= ; i<=n ; i++)
{
b[++ans].l=a[i]-a[i-]+;
b[ans].dis=false;
b[ans].r=a[i]+a[i-]-;
b[ans].dis=true;
}
sort(b+,b+ans,cmp);
int cas=;c[++cas].l=b[].l;c[cas].r=b[].r;
for (int i= ; i<=ans ; i++){
if (judge(b[i].l,c[cas].l,c[cas].r)){
if (!judge(b[i].r,c[cas].l,c[cas].r))
c[cas].r=b[i].r;
}
else{
c[++cas].l=b[i].l;c[cas].r=b[i].r;
}
}
ll res=r-l+;
for (int i= ; i<=cas ; i++){
if (judge(c[i].l,l,r)){
if (judge(c[i].r,l,r))
res-=(c[i].r-c[i].l+);
else
res-=(r-c[i].l+);
}
else {
if (c[i].l<l){
if (judge(c[i].r,l,r))
res-=(c[i].r-l+);
else if (c[i].r>r)
res-=(r-l+);
}
}
}
if (res<) res=;
printf("%I64d\n",res);
}
return ;
}
关系)