fork download
  1. /*
  2. * @Author: hungeazy
  3. * @Date: 2025-12-06 17:39:19
  4. * @Last Modified by: hungeazy
  5. * @Last Modified time: 2025-12-06 18:02:32
  6. */
  7. #include <bits/stdc++.h>
  8. #include <ext/pb_ds/assoc_container.hpp>
  9. #include <ext/pb_ds/tree_policy.hpp>
  10. // #pragma GCC optimize("O3")
  11. // #pragma GCC optimize("unroll-loops")
  12. // #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
  13. using namespace std;
  14. using namespace __gnu_pbds;
  15. bool M1;
  16. #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  17. #define int long long
  18. #define ll long long
  19. #define ull unsigned long long
  20. #define sz(x) x.size()
  21. #define sqr(x) (1LL * (x) * (x))
  22. #define all(x) x.begin(), x.end()
  23. #define fill(f,x) memset(f,x,sizeof(f))
  24. #define FOR(i,l,r) for(int i=l;i<=r;i++)
  25. #define FOD(i,r,l) for(int i=r;i>=l;i--)
  26. #define debug(x) cout << #x << " = " << x << '\n'
  27. #define ii pair<int,int>
  28. #define iii pair<int,ii>
  29. #define di pair<ii,ii>
  30. #define vi vector<int>
  31. #define vii vector<ii>
  32. #define mii map<int,int>
  33. #define fi first
  34. #define se second
  35. #define pb push_back
  36. #define MOD 1000000007
  37. #define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
  38. #define YES cout << "YES\n"
  39. #define NO cout << "NO\n"
  40. #define MASK(i) (1LL << (i))
  41. #define c_bit(i) __builtin_popcountll(i)
  42. #define BIT(x,i) ((x) & MASK(i))
  43. #define SET_ON(x,i) ((x) | MASK(i))
  44. #define SET_OFF(x,i) ((x) & ~MASK(i))
  45. #define oo 1e18
  46. #define name "TUONGDONG"
  47. #define endl '\n'
  48. #define memory() cerr << abs(&M2-&M1)/1024.0/1024 << " MB" << endl
  49. #define time() cerr << endl << "-------------Time:" << 1000.0 * clock() / CLOCKS_PER_SEC << "ms." << endl
  50. template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
  51. template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
  52. template <class T> using ordered_set = tree <T, null_type, less_equal <T>, rb_tree_tag,tree_order_statistics_node_update>;
  53. const int N = (int)2e5+10;
  54. int n,m,a[N];
  55. // 1748E
  56. namespace sub1 {
  57.  
  58. int b[N],ans = 0;
  59.  
  60. bool approved() {
  61. return n*m <= 16;
  62. }
  63.  
  64. void Try(int i)
  65. {
  66. FOR(j,1,m)
  67. {
  68. b[i] = j;
  69. if (i < n) Try(i+1);
  70. else
  71. {
  72. FOR(i,1,n)
  73. {
  74. int mxA = 0, posA = 0, mxB = 0, posB = 0;
  75. FOR(j,i,n)
  76. {
  77. if (maximize(mxA,a[j]))
  78. posA = j;
  79. if (maximize(mxB,b[j]))
  80. posB = j;
  81. if (posA != posB)
  82. goto li;
  83. }
  84. }
  85. ans++;
  86. li:;
  87. }
  88. }
  89. }
  90.  
  91. void solve(void)
  92. {
  93. Try(1);
  94. cout << ans;
  95. }
  96.  
  97. }
  98.  
  99. namespace sub3 {
  100.  
  101. vector<vi> dp;
  102. int par[N];
  103. vi g[N];
  104. /*
  105. Xây cây Cartesian Tree với định nghĩa sau:
  106. - Cha của mỗi node là phần tử lớn nhất gần nhất phía trái/phải tạo cùng max.
  107. - Node có giá trị lớn nhất là root.
  108. -> Dãy b hợp lệ chính là dãy b có cùng cấu trúc Catersian Tree với a
  109. dp[u][x]: số cách gán giá trị cho toàn bộ subtree u sao cho b[u] = x
  110. và giữ cấu trúc max như cây đã định sẵn.
  111. Các con của u nhận giá trị sau:
  112. - Con bên phải: nhận giá trị <= i
  113. - Con bên trái: nhận giá trị < i
  114. */
  115. void DFS(int u)
  116. {
  117. for (int v : g[u]) DFS(v);
  118. FOR(i,1,m)
  119. {
  120. (dp[u][i] += dp[u][i-1]) %= MOD;
  121. int ans = 1;
  122. for (int v : g[u])
  123. if (v > u) (ans *= dp[v][i]) %= MOD;
  124. else (ans *= dp[v][i-1]) %= MOD;
  125. (dp[u][i] += ans) %= MOD;
  126. }
  127. }
  128.  
  129. void solve(void)
  130. {
  131. dp.resize(n+2,vi(m+2,0));
  132. stack<int> st;
  133. FOR(i,1,n)
  134. {
  135. while (!st.empty() and a[st.top()] < a[i])
  136. st.pop();
  137. par[i] = (st.empty() ? 0 : st.top());
  138. st.push(i);
  139. }
  140. while (!st.empty()) st.pop();
  141. FOD(i,n,1)
  142. {
  143. while (!st.empty() and a[st.top()] <= a[i])
  144. st.pop();
  145. if (!st.empty())
  146. {
  147. if (!par[i]) par[i] = st.top();
  148. else if (a[par[i]] >= a[st.top()])
  149. par[i] = st.top();
  150. }
  151. st.push(i);
  152. }
  153. int root = 0;
  154. FOR(i,1,n)
  155. if (!par[i]) root = i;
  156. else g[par[i]].pb(i);
  157. DFS(root);
  158. cout << dp[root][m] << endl;
  159. }
  160.  
  161. }
  162.  
  163. bool M2;
  164. signed main()
  165. {
  166. fast;
  167. if (fopen(name".inp","r"))
  168. {
  169. freopen(name".inp","r",stdin);
  170. freopen(name".out","w",stdout);
  171. }
  172. cin >> n >> m;
  173. FOR(i,1,n) cin >> a[i];
  174. if (sub1::approved()) return sub1::solve(), time(), memory(), 0;
  175. sub3::solve();
  176. time();
  177. memory();
  178. return 0;
  179. }
  180. // ██░ ██ █ ██ ███▄ █ ▄████
  181. //▓██░ ██▒ ██ ▓██▒ ██ ▀█ █ ██▒ ▀█▒
  182. //▒██▀▀██░▓██ ▒██░▓██ ▀█ ██▒▒██░▄▄▄░
  183. //░▓█ ░██ ▓▓█ ░██░▓██▒ ▐▌██▒░▓█ ██▓
  184. //░▓█▒░██▓▒▒█████▓ ▒██░ ▓██░░▒▓███▀▒
  185. // ▒ ░░▒░▒░▒▓▒ ▒ ▒ ░ ▒░ ▒ ▒ ░▒ ▒
  186. // ▒ ░▒░ ░░░▒░ ░ ░ ░ ░░ ░ ▒░ ░ ░
  187. // ░ ░░ ░ ░░░ ░ ░ ░ ░ ░ ░ ░ ░
  188. // ░ ░ ░ ░ ░ ░
Success #stdin #stdout #stderr 0.01s 10912KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
-------------Time:5.085ms.
9.15585 MB