fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <string>
  6. #include<bits/stdc++.h>
  7. #include<queue>
  8. #include<deque>
  9. #include<stack>
  10. #include<set>
  11. using namespace std;
  12. void fast()
  13. {
  14. ios::sync_with_stdio(false);
  15. cin.tie(nullptr);
  16. cout.tie(nullptr);
  17.  
  18.  
  19. }
  20.  
  21. void george_dictator_2()
  22. {
  23. int n,k;
  24. cin>>n>>k;
  25. vector<int>v(n);
  26. vector<int>freq(2000001,0),freq_ne(2000001,0);
  27. for(int i=0; i<n; i++)
  28. {
  29. cin>>v[i];
  30. if(v[i]<0)
  31. {
  32. freq_ne[v[i]*-1]++;
  33. }
  34. else
  35. {
  36. freq[v[i]]++;
  37. }
  38. }
  39. sort(v.begin(),v.end());
  40. long long cont=0;
  41.  
  42.  
  43. for(int i=0; i<n; i++)
  44. {
  45. int x=k-v[i];
  46.  
  47. if(x<0)
  48. {
  49. if(x==v[i])
  50. {
  51. cont+=freq_ne[x*-1]-1;
  52. }
  53.  
  54. else{
  55. cont+=freq_ne[x*-1];
  56. }
  57. }
  58. else
  59. {
  60. if(x==v[i])
  61. {
  62. cont+=freq[x]-1;
  63. }
  64. else
  65. {
  66. cont+=freq[x];
  67. }
  68. }
  69. if(v[i]<0)
  70. {
  71. if(freq_ne[v[i]*-1]>0)
  72. {
  73. freq_ne[v[i]*-1]--;
  74.  
  75. }
  76.  
  77. }
  78. else
  79. {
  80. if(freq[v[i]]>0)
  81. {
  82. freq[v[i]]--;
  83. }
  84. }
  85.  
  86. }
  87.  
  88. cout<<cont;
  89.  
  90. }
  91.  
  92.  
  93. int main()
  94. {
  95. fast();
  96.  
  97.  
  98. george_dictator_2();
  99.  
  100.  
  101. return 0;
  102. }
  103.  
Success #stdin #stdout 0.01s 18888KB
stdin
Standard input is empty
stdout
145530