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.  
  28. int bruteforce(int n, int k1, int k2){
  29. int ans = 0;
  30.  
  31. for(int i=0; i<n-2; i++){
  32. for(int j=i+1; j<n-2; j++){
  33. int leftSum = a[i] + a[j];
  34. if(leftSum <= k1) continue;
  35.  
  36. for(int k=j+1; k<n-1; k++){
  37. for(int l=k+1; l<n; l++){
  38. int rightSum = a[k] + a[l];
  39. if(rightSum > k2 ) ans++;
  40. }
  41. }
  42. }
  43. }
  44. return ans;
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. //* Left Count Pre Computation :
  53.  
  54. //* Eg : [ 1 , 2 , 3 , 4 , 5 , 6 , 8 ] k1 = 9
  55.  
  56. //* Ending at analogy :
  57. //* We are finding pairs (x,y) with sum x+y == k1 ending at y
  58.  
  59. //* Observation : if we drew out patterns for each pairs , it looks like this :
  60. //* [ (1 , 2 , (3 , (4 , 5) , 6) , 8) ]
  61. //* y = 1,2,3,4 can't form pair with any x such that x+y >= 9
  62. //* y = 5 forms pair with x = 4 only => ct = 1
  63. //* y = 6 forms pair with x = (3, 4, 5) => ct = 3
  64. //* y = 8 forms pair with x = (1, 2, 3, 4, 5, 6) => ct = 6
  65.  
  66. //* Now we can use 2 ptr from 1st pair [x, x+1] such that x + x + 1 >= k1
  67. //* i j
  68. //* [ 1 , 2 , 3 , (4 , 5) , 6 , 8) ]
  69. //* p[5] = max(0, 2) = 2
  70. //* Now we want to maximize p[5] cts so move i left => i--
  71.  
  72. //* i j
  73. //* [ 1 , 2 , 3 , (4 , 5) , 6 , 8) ]
  74. //* 5+3 < k1
  75. //* Invalid so move to next y => j++
  76.  
  77. //* i j
  78. //* [ 1 , 2 , 3 , (4 , 5 , 6) , 8) ]
  79. //* p[6] = max(0, 3) = 3
  80.  
  81. //* i j
  82. //* [ 1 , 2 , (3 , 4 , 5 , 6) , 8) ]
  83. //* p[6] = max(3, 4) = 4
  84.  
  85. //* i j
  86. //* [ 1 , (2 , 3 , 4 , 5 , 6) , 8) ]
  87. //* Invalid => j++
  88.  
  89. //* O(n + n + n ) = O(n)
  90. int consistency1(int n, int k1, int k2) {
  91.  
  92. int ans = 0;
  93.  
  94. vector<int> p(n);
  95.  
  96. vector<int> leftPrefix(n);
  97. int i=1;
  98. while(i<n){
  99. if(a[i] + a[i-1] >= k1) break;
  100. i++;
  101. }
  102. int left = i-1;
  103. int right = i;
  104.  
  105. while(left>=0 && right<n){
  106. if(a[left] + a[right] > k1){
  107. p[right] = max(p[right], right-left);
  108. left--;
  109. }
  110. else {
  111. right++;
  112. }
  113. }
  114. for(int i=1; i<n; i++){
  115. leftPrefix[i] = leftPrefix[i-1] + p[i];
  116. }
  117.  
  118.  
  119.  
  120. vector<int> q(n);
  121.  
  122. vector<int> rightSufix(n);
  123. // for(int i=n-1; i>=0; i--){
  124. // rightSufix[i] = rightSufix[i+1] + q[i];
  125. // }
  126.  
  127. for(int j=1; j<n-2; j++){
  128. int leftCt = leftPrefix[j];
  129. int rightCt = rightSufix[j+1];
  130.  
  131. ans += leftCt * rightCt;
  132. }
  133.  
  134.  
  135. return ans;
  136.  
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. int practice(int n, int k1, int k2) {
  158.  
  159. return 0;
  160.  
  161. }
  162.  
  163.  
  164. void solve() {
  165.  
  166. int n, k1, k2;
  167. cin >> n >> k1 >> k2;
  168.  
  169. a.resize(n);
  170. for(int i=0; i<n; i++) cin >> a[i];
  171.  
  172. cout << bruteforce(n, k1, k2) << " ";
  173. cout << consistency1(n, k1, k2) << endl;
  174.  
  175.  
  176. // cout << bruteforce(n, k1, k2) << " -> ";
  177. // cout << practice(n, k1, k2) << endl;
  178.  
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185. int32_t main() {
  186. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  187.  
  188. int t = 1;
  189. cin >> t;
  190. while (t--) {
  191. solve();
  192. }
  193.  
  194. return 0;
  195. }
Success #stdin #stdout 0s 5320KB
stdin
4
8 9 7
1 2 3 4 5 6 8 12
6 1 3
0 1 1 1 2 2
6 1 3
1 1 1 1 2 2
6 3 3
1 2 2 2 2 3 
stdout
2 0
3 0
6 0
5 0