fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<int> a;
  26.  
  27. //* O(n^2)
  28. void consistency1(int n, int k1, int k2) {
  29.  
  30. int ans = 0;
  31. for(int j=1; j<n-2; j++){
  32. int i=0;
  33. while(i<j){
  34. if(a[i] + a[j] >= k1) break;
  35. i++;
  36. }
  37. int leftCt = j-i;
  38.  
  39. int s= j+1, e = n-1;
  40. int rightCt = 0;
  41. while(s<e){
  42. int sum = a[s] + a[e];
  43. if(sum > k2){
  44. rightCt += (e-s);
  45. e--;
  46. }
  47. else{
  48. s++;
  49. }
  50. }
  51. ans += leftCt * rightCt;
  52. }
  53.  
  54. cout << ans << endl;
  55. }
  56.  
  57.  
  58.  
  59.  
  60. //* O(n^2)
  61. void consistency2(int n, int k1, int k2) {
  62.  
  63. int ans = 0;
  64.  
  65. vector<int> p(n);
  66. for(int i=0; i<n-1; i++){
  67. int target = k2 - a[i];
  68. int j = lower_bound(a.begin() + i + 1, a.end(), target) - a.begin();
  69. p[i] = n-j;
  70. }
  71. vector<int> rightSufix(n);
  72. for(int i=n-2; i>=0; i--){
  73. rightSufix[i] = rightSufix[i+1] + p[i];
  74. }
  75.  
  76.  
  77. for(int j=1; j<n-2; j++){
  78. int target = k1 - a[j];
  79. int i = lower_bound(a.begin(), a.begin()+j, target) - a.begin();
  80. int leftCt = j-i;
  81.  
  82. int rightCt = rightSufix[j+1];
  83.  
  84. ans += leftCt * rightCt;
  85.  
  86. }
  87.  
  88.  
  89. cout << ans << endl;
  90.  
  91. }
  92.  
  93.  
  94. void solve() {
  95.  
  96. int n, k1, k2;
  97. cin >> n >> k1 >> k2;
  98.  
  99. a.resize(n);
  100. for(int i=0; i<n; i++) cin >> a[i];
  101.  
  102. consistency1(n, k1, k2);
  103. consistency2(n, k1, k2);
  104.  
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. int32_t main() {
  112. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  113.  
  114. int t = 1;
  115. // cin >> t;
  116. while (t--) {
  117. solve();
  118. }
  119.  
  120. return 0;
  121. }
Success #stdin #stdout 0.01s 5292KB
stdin
8 8 7
1 2 3 5 6 8 10 12
stdout
20
20