Ankit_132

Triplets Min

Jul 5th, 2023
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8. #define pb     push_back
  9.  
  10. int main()
  11. {
  12.     _test
  13.     {
  14.         int n, q;
  15.         cin>>n>>q;
  16.  
  17.         vector<int> a(n);
  18.         for(auto &e: a)        cin>>e;
  19.  
  20.         sort(a.begin(), a.end());
  21.  
  22.         vector<ll int> cnt(n);
  23.  
  24.         for(int i=0; i<n; i++)
  25.         {
  26.             ll int x = n-i-1;
  27.             cnt[i] = (x*(x-1))/2;
  28.         }
  29.  
  30.         for(int i=1; i<n; i++)
  31.             cnt[i] += cnt[i-1];
  32.  
  33.         while(q--)
  34.         {
  35.             ll int k;
  36.             cin>>k;
  37.  
  38.             int pos = lower_bound(cnt.begin(), cnt.end(), k) - cnt.begin();
  39.  
  40.             cout<<a[pos]<<"\n";
  41.         }
  42.     }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment