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 s = 0, e = n-3;
  69.  
  70. while(s<e){
  71. if(a[s] + a[e] <= k1){
  72. s++;
  73. }
  74. else{
  75. int left = e-s;
  76. int right = 0;
  77.  
  78. int l = e+1;
  79. int r = n-1;
  80.  
  81. while(l<r){
  82. if(a[l]+a[r] <= k2){
  83. l++;
  84. }
  85. else{
  86. right += r-l;
  87. r--;
  88. }
  89. }
  90.  
  91. ans += left*right;
  92. e--;
  93. }
  94. }
  95.  
  96.  
  97. return ans;
  98.  
  99. }
  100.  
  101.  
  102.  
  103.  
  104. //* O(n^2)
  105. int consistency2(int n, int k1, int k2) {
  106.  
  107. int ans = 0;
  108. for(int j=1; j<n-2; j++){
  109.  
  110. int i=j-1;
  111. while(i>=0 && a[i]+a[j] > k1) i--;
  112.  
  113. int left = j-i-1;
  114. int right = 0;
  115.  
  116. int s= j+1, e = n-1;
  117.  
  118. while(s<e){
  119. if(a[s]+a[e] > k2){
  120. right += (e-s);
  121. e--;
  122. }
  123. else{
  124. s++;
  125. }
  126. }
  127. ans += left * right;
  128. }
  129.  
  130. return ans;
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. int practice(int n, int k1, int k2) {
  161.  
  162. int ans = 0;
  163.  
  164.  
  165. return ans;
  166.  
  167. }
  168.  
  169.  
  170. void solve() {
  171.  
  172. int n, k1, k2;
  173. cin >> n >> k1 >> k2;
  174.  
  175. a.resize(n);
  176. for(int i=0; i<n; i++) cin >> a[i];
  177.  
  178. cout << bruteforce(n, k1, k2) << " -> ";
  179. cout << consistency1(n, k1, k2) << " ";
  180. cout << consistency2(n, k1, k2) << " ";
  181. cout << endl;
  182.  
  183. // cout << bruteforce(n, k1, k2) << " -> ";
  184. // cout << practice(n, k1, k2) << endl;
  185.  
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192. int32_t main() {
  193. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  194.  
  195. int t = 1;
  196. cin >> t;
  197. while (t--) {
  198. solve();
  199. }
  200.  
  201. return 0;
  202. }
Success #stdin #stdout 0.01s 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 -> 2 2 
3 -> 3 3 
6 -> 6 6 
5 -> 5 5