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. //* Global 2 ptr * local 2 ptr
  51.  
  52. //* O(n^2)
  53.  
  54. //* [ 1 2 2 2 3 ] , k1 = 3, k2 = 3
  55.  
  56. //* i j | s e
  57. //* [ ( 1 2 2 2 ) |(2 3) ]
  58. //* 2 * 1
  59.  
  60. //* i j | s e
  61. //* [ 1 ( 2 2 ) |(2 2 3) ]
  62. //* 1 * 3
  63.  
  64. int consistency1(int n, int k1, int k2) {
  65.  
  66. int ans = 0;
  67.  
  68. int i = 0, j = n-3;
  69.  
  70. while(i<j){
  71. if(a[i] + a[j] <= k1){
  72. i++;
  73. }
  74. else{
  75. int right = 0;
  76. int s = j+1;
  77. int e = n-1;
  78. while(s<e){
  79. if(a[s]+a[e] <= k2){
  80. s++;
  81. }
  82. else{
  83. right += e-s;
  84. e--;
  85. }
  86. }
  87.  
  88. int left = j-i;
  89.  
  90. ans += left*right;
  91.  
  92. j--;
  93. }
  94. }
  95.  
  96.  
  97. return ans;
  98.  
  99. }
  100.  
  101.  
  102.  
  103. //* template 2
  104. int consistency2(int n, int k1, int k2) {
  105.  
  106. int ans = 0;
  107.  
  108. for(int j=n-3, i=0; j>=1; j--){
  109. int left = 0;
  110. while(i<j && a[i]+a[j] <= k1) i++;
  111. if(i==j) break;
  112. left += j-i;
  113.  
  114. int right = 0;
  115. int s = j+1, e = n-1;
  116. while(s<e){
  117. if(a[s] + a[e] <= k2) s++;
  118. else {
  119. right += e-s;
  120. e--;
  121. }
  122. }
  123.  
  124. ans += (left * right);
  125.  
  126. }
  127.  
  128. return ans;
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. //* O(n^2)
  139. int consistency3(int n, int k1, int k2) {
  140.  
  141. int ans = 0;
  142. for(int j=1; j<n-2; j++){
  143. int i=0;
  144. while(i<j && a[i]+a[j] <= k1) i++;
  145. int left = j-i;
  146.  
  147. int s= j+1, e = n-1;
  148. int right = 0;
  149. while(s<e){
  150. if(a[s]+a[e] > k2){
  151. right += (e-s);
  152. e--;
  153. }
  154. else{
  155. s++;
  156. }
  157. }
  158. ans += left * right;
  159. }
  160.  
  161. return ans;
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. int practice(int n, int k1, int k2) {
  192.  
  193. int ans = 0;
  194.  
  195.  
  196. return ans;
  197.  
  198. }
  199.  
  200.  
  201. void solve() {
  202.  
  203. int n, k1, k2;
  204. cin >> n >> k1 >> k2;
  205.  
  206. a.resize(n);
  207. for(int i=0; i<n; i++) cin >> a[i];
  208.  
  209. cout << bruteforce(n, k1, k2) << " ";
  210. cout << consistency1(n, k1, k2) << " ";
  211. cout << consistency2(n, k1, k2) << " ";
  212. cout << consistency3(n, k1, k2) << endl;
  213.  
  214.  
  215. // cout << bruteforce(n, k1, k2) << " -> ";
  216. // cout << practice(n, k1, k2) << endl;
  217.  
  218. }
  219.  
  220.  
  221.  
  222.  
  223.  
  224. int32_t main() {
  225. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  226.  
  227. int t = 1;
  228. cin >> t;
  229. while (t--) {
  230. solve();
  231. }
  232.  
  233. return 0;
  234. }
Success #stdin #stdout 0s 5316KB
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 2 2 2
3 3 3 3
6 6 6 6
5 5 5 5