fork download
  1. #include <bits/stdc++.h>
  2. #define N 100005
  3. using namespace std;
  4.  
  5. long long n, a[N];
  6.  
  7. void sub1()
  8. {
  9. long long cnt = 0;
  10. for(int i = 1; i <= n; i++)
  11. {
  12. if(a[i] + a[i+1] > a[n]) break;
  13. for(int j = i + 1; j <= n; j++)
  14. {
  15. if(a[i] + a[j] > a[n]) break;
  16. if(binary_search(a + j + 1, a + 1 + n, a[i] + a[j]))
  17. {
  18. //cout << a[i] << ' ' << a[j] << '\n';
  19. cnt++;
  20. }
  21. }
  22. }
  23. cout << cnt * 2;
  24. }
  25.  
  26. int main()
  27. {
  28. cin.tie(0) -> sync_with_stdio(false);
  29. freopen("BAI2.INP", "r", stdin);
  30. freopen("BAI2.OUT", "w", stdout);
  31.  
  32. cin >> n;
  33. for(int i = 1; i <= n; i++) cin >> a[i];
  34. sort(a + 1, a + 1 + n);
  35. if(n <= 3000) sub1();
  36. else
  37. {
  38. long long cnt = 0;
  39. for(int i = 1; i <= n; i++)
  40. {
  41. int j = lower_bound(a + 1, a + 1 + n, a[n] - a[i]) - a;
  42. if(a[j] <= a[i]) break;
  43. cnt += j - i;
  44. }
  45. cout << cnt * 2;
  46. }
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty