fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. namespace std {
  6. #ifndef LOCAL
  7. #define cerr \
  8.   if (0) cerr
  9. #endif
  10. } // namespace std
  11.  
  12. int a[200005];
  13. int dp[200005][2];
  14. int n, q;
  15.  
  16. int32_t main() {
  17. ios_base::sync_with_stdio(0);
  18. cin.tie(0);
  19. #ifdef LOCAL
  20. #define task "a"
  21. #else
  22. #define task "CLIMBING"
  23. #endif
  24. if (fopen(task ".inp", "r")) {
  25. freopen(task ".inp", "r", stdin);
  26. freopen(task ".out", "w", stdout);
  27. }
  28. cin >> n >> q;
  29. for (int i = 1; i <= n; i++) {
  30. cin >> a[i];
  31. }
  32. while (q--) {
  33. int l, r;
  34. cin >> l >> r;
  35. // dp[i, last_sign]
  36. // sign = 0: <
  37. // sign = 1: >
  38. int ret = 0;
  39. for (int i = l; i <= r; i++) {
  40. dp[i][1] = 1;
  41. dp[i][0] = -1e9;
  42. for (int j = l; j < i; j++) {
  43. if (a[j] < a[i]) {
  44. dp[i][0] = max(dp[i][0], dp[j][1] + 1);
  45. }
  46. if (a[j] > a[i]) {
  47. dp[i][1] = max(dp[i][1], dp[j][0] + 1);
  48. }
  49. }
  50. ret = max(ret, dp[i][1]);
  51. }
  52. cout << ret << "\n";
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty