D. 夹缝中求和

的数据只能是以下的复杂度,排序已经到,那么查找数就得是或者为

  • 就是最容易想到的二分法,用STL的即可,不等式即为
    #include<bits/stdc++.h>
    using namespace std;
    #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define ll long long
    const int N = 1e5 + 10;
    ll n, cnt;
    ll x, y;
    ll a[N];
    int main()
    {
      IOS;
      cin >> n >> x >> y;
      for(int i = 1; i <= n; i++) cin >> a[i];
      sort(a + 1, a + 1 + n);
      for(int i = 1; i <= n; i++){
          int l = lower_bound(a + i + 1, a + n + 1, x - a[i]) - a;
          int r = upper_bound(a + i + 1, a + n + 1, y - a[i]) - a;
          cnt += (r - l);
      }
      cout << cnt << endl;
    }