枚举x 每次符合条件y的个数 即为对数求和 x<y<=2x upper_bound第一个大于2x 位置pos pos-1为<=2x i为x位置 i+1为>x pos-1-(i+1)+1
#include<bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false) #define ll long long #define mp make_pair #define fi first #define se second #define pb emplace_back #define pii pair<int,int> #define all(n) (n).begin(),(n).end() #define rep(i,a,n) for (int i=a;i<=n;i++) #define QAQ sort(co+1,co+1+n) const int maxn = 2e5 + 5; const ll mod = 998244353; const int inf = 0x3f3f3f3f; int a[maxn]; int main() { int n,t; cin>>n; for(int i=1;i<=n;i++) { cin>>t; a[i]=abs(t); } sort(a+1,a+n+1); ll ans=0; //全都符合有多少对 2e5*(2e5-1)/2=1e5*2e5=2e10 for(int i=1;i<=n;i++) //x<y<=2*x 枚举每一个x符合条件的y有多少个 { int pos=upper_bound(a+1,a+n+1,2*a[i])-a; // cout<<pos-1<<endl;//<=2*x的位置 //从i+1开始大于x的数 ans+=(pos-1-(i+1)+1); } cout<<ans<<endl; return 0; } /* */