fork download
  1. // author : Nguyễn Trọng Nguyễn - ITK22 NBK
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int maxn = (int)1e6;
  6. typedef long long ll;
  7. const ll INF = (int)1e18;
  8.  
  9. int n;
  10. int a[maxn + 5];
  11. ll k;
  12.  
  13. void Gentest () {
  14. srand(time(NULL));
  15.  
  16. n = rand() % (int)5e3;
  17. k = rand() % INF;
  18.  
  19. for (int i = 1; i <= n; i++) {
  20. int t = rand() % 2;
  21. a[i] = rand() % (int)1e9;
  22. if (t == 0) a[i] = -a[i];
  23. }
  24. }
  25.  
  26. namespace SUBTASK1 {
  27. ll cnt = 0;
  28.  
  29. void Sub1 () {
  30. for (int i = 1; i <= n; i++)
  31. for (int j = i + 1; j <= n; j++)
  32. if (abs(a[i] + a[j]) == k)
  33. cnt++;
  34.  
  35. cout << cnt << '\n';
  36. }
  37. }
  38.  
  39. namespace SUBTASK2 {
  40. ll cnt = 0;
  41.  
  42. ll get (int t, int i) {
  43. int l = lower_bound(a + 1, a + 1 + n, t - a[i]) - a;
  44. int r = upper_bound(a + 1, a + 1 + n, t - a[i]) - a;
  45.  
  46. return r - l;
  47. }
  48.  
  49. void Sub2 () {
  50. sort(a + 1, a + 1 + n);
  51.  
  52. for (int i = 1; i <= n; i++)
  53. cnt += get(k, i) + get(-k, i);
  54.  
  55. cout << cnt / 2;
  56. }
  57. }
  58.  
  59. signed main () {
  60. cin.tie(0)->sync_with_stdio(false);
  61.  
  62. #ifndef ONLINE_JUDGE
  63. freopen("main.inp","r",stdin);
  64. freopen("main.out","w",stdout);
  65. #endif
  66.  
  67. cin >> n >> k;
  68. for (int i = 1; i <= n; i++) cin >> a[i];
  69.  
  70. Gentest();
  71.  
  72. if (n <= (int)5e3) SUBTASK1 :: Sub1();
  73. else SUBTASK2 :: Sub2();
  74.  
  75. return 0;
  76. }
  77.  
Success #stdin #stdout 0.01s 5252KB
stdin
Standard input is empty
stdout
0