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.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. //* Piviot j (in i<j>k)
  33.  
  34. int consistency1(int n){
  35.  
  36. int ans = 0;
  37. for(int j=1; j<=n-2; j++){
  38. int i = j-1;
  39. while(i>=0 && a[i] < a[i+1]) i--;
  40. int ct1 = j-i-1;
  41.  
  42. int k = j+1;
  43. while(j<n && a[k] < a[k-1]) k++;
  44. int ct2 = k-j-1;
  45.  
  46.  
  47. ans += ct1*ct2;
  48.  
  49. }
  50.  
  51. return ans;
  52.  
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. int consistency2(int n){
  63.  
  64. int ans = 0;
  65.  
  66. //* prefix Longest increasing subarray ending at i
  67. vector<int> pLIS(n, 0);
  68.  
  69. //* Suffix Longest decreasing subarray starting at i
  70. vector<int> sLIS(n, 0);
  71.  
  72. for(int i=1; i<n; i++){
  73. if(a[i] > a[i-1]){
  74. pLIS[i] = pLIS[i-1] + 1;
  75. }
  76. }
  77.  
  78. for(int i=n-2; i>=0; i--){
  79. if(a[i] > a[i+1]){
  80. sLIS[i] = sLIS[i+1] + 1;
  81. }
  82. }
  83.  
  84. for(int j=1; j<=n-2; j++){
  85. ans += pLIS[j] * sLIS[j];
  86. }
  87.  
  88. return ans;
  89.  
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. int practice(int n){
  114.  
  115.  
  116. return 0;
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. void solve() {
  124.  
  125. int n;
  126. cin>> n;
  127.  
  128. a.resize(n);
  129. for(int i=0; i<n; i++) cin >> a[i];
  130.  
  131. cout << consistency1(n) << " " << consistency2(n) << endl;
  132.  
  133.  
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140. int32_t main() {
  141. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  142.  
  143. int t = 1;
  144. cin >> t;
  145. while (t--) {
  146. solve();
  147. }
  148.  
  149. return 0;
  150. }
Success #stdin #stdout 0.01s 5300KB
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