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. void consistency1(int n, int k) {
  27.  
  28. int s= 0, e = n-1;
  29. int ans = 0;
  30.  
  31. //* keep in mind s<e (Not s<=e )
  32. while(s<e){
  33. int sum = a[s] + a[e];
  34. if(sum > k){
  35. e--;
  36. }
  37. else{
  38. ans += (e-s);
  39. s++;
  40. }
  41. }
  42.  
  43. cout << ans << " ";
  44.  
  45. }
  46.  
  47.  
  48. //* Template 2 :
  49. //* Shrink invalid window (if it can be)
  50. //* Invalid window => sum > k
  51. //* Valid window => sum <= k
  52.  
  53. void consistency2(int n, int k) {
  54.  
  55. int s = 0, e = n-1;
  56. int ans = 0;
  57.  
  58. while(s<n){
  59. int sum = a[s] + a[e];
  60.  
  61. while(e>s && sum>k){
  62. e--;
  63. sum = a[s] + a[e];
  64. }
  65.  
  66. if(e == s) break;
  67.  
  68. ans += (e-s);
  69. s++;
  70. }
  71.  
  72. cout << ans << " ";
  73.  
  74. }
  75.  
  76.  
  77. void solve() {
  78.  
  79. int n, k;
  80. cin >> n >> k;
  81.  
  82. a.resize(n);
  83.  
  84. for(int i=0; i<n; i++) cin >> a[i];
  85.  
  86. consistency1(n, k);
  87. consistency2(n, k);
  88.  
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95. int32_t main() {
  96. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  97.  
  98. int t = 1;
  99. // cin >> t;
  100. while (t--) {
  101. solve();
  102. }
  103.  
  104. return 0;
  105. }
Success #stdin #stdout 0s 5324KB
stdin
5 16
1 5 10 15 20
stdout
4 4