fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27. // 2
  28. // 15
  29. // 1 2 4 5 3 2 7 8 2 9 1 0 3 4 2
  30. // 4
  31. // 1 2 1 0
  32.  
  33.  
  34.  
  35. vector<int> a;
  36.  
  37. //* Piviot j (in i<j>k)
  38.  
  39. int consistency1(int n){
  40.  
  41. int ans = 0;
  42. for(int j=1; j<=n-2; j++){
  43. int i = j-1;
  44. while(i>=0 && a[i] < a[i+1]) i--;
  45. int ct1 = j-i;
  46.  
  47. int k = j+1;
  48. while(j<n && a[k] < a[k-1]) k++;
  49. int ct2 = k-j;
  50.  
  51.  
  52. ans += (ct1-1)*(ct2-1);
  53.  
  54. }
  55.  
  56. return ans;
  57.  
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. int consistency2(int n){
  68.  
  69. int ans = 0;
  70.  
  71. //* prefix Longest increasing subarray ending at i
  72. vector<int> pLIS(n, 1);
  73.  
  74. //* Suffix Longest decreasing subarray starting at i
  75. vector<int> sLIS(n, 1);
  76.  
  77. for(int i=1; i<n; i++){
  78. if(a[i] > a[i-1]){
  79. pLIS[i] = pLIS[i-1] + 1;
  80. }
  81. }
  82.  
  83. for(int i=n-2; i>=0; i--){
  84. if(a[i] > a[i+1]){
  85. sLIS[i] = sLIS[i+1] + 1;
  86. }
  87. }
  88.  
  89. for(int j=1; j<=n-2; j++){
  90. ans += (pLIS[j]-1) * (sLIS[j]-1);
  91. }
  92.  
  93. return ans;
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. int practice(int n){
  119.  
  120.  
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127. void solve() {
  128.  
  129. int n;
  130. cin>> n;
  131.  
  132. a.resize(n);
  133. for(int i=0; i<n; i++) cin >> a[i];
  134.  
  135. cout << consistency1(n) << " " << consistency2(n) << endl;
  136. // cout << practice(n) << endl;
  137.  
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144. int32_t main() {
  145. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  146.  
  147. int t = 1;
  148. cin >> t;
  149. while (t--) {
  150. solve();
  151. }
  152.  
  153. return 0;
  154. }
Success #stdin #stdout 0s 5320KB
stdin
2
15
1 2 4 5 3 2 7 8 2 9 1 0 3 4 2 
4
1 2 1 0
stdout
12 12
2 2